Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JLabel.getText doesn't work properly

843806May 8 2008 — edited May 10 2008
I have a main JFrame to build up the application GUI (using NetBeans 6.0.1). This mainPanel contains a JSplitPane and a StatusBar beneath. In the right side of the JSplitPane I put a JPanel with JComboBoxes and JButtons. After selecting an item in the JComboBox and clicking a JButton, the text in the StatusBar (implemented using a simple JLabel) shall be faded out by simply removing the first character until the text is erased and I use a Timer for this. This works well for itself.
The problem now is that I want to start the 'fading' only if the StatusBar (the JLabel) contains a text at all. Checking this with:
if ( !targetLabel.getText().equals("") ) {
<start fading>
}
the getText() method returns "" (an empty string) ever. Even although I initialized this JLabel with a dummy text during initialization. Why?

Here's the code for trying to set a JLabel in the StatusBar (this part works well) and fading a probably displayed text prior:
class StatusMessageSetter implements Runnable{
String myText;
Color color;
JLabel target;

public StatusMessageSetter(JLabel target, Color color, String text) {
myText = text;
this.color = color;
this.target = target;
}

public void run() {
//try {
System.out.println("StatusMessageSetter: target=" + target);
//SwingUtilities.invokeLater(new Runnable(){
//   public void run() {
System.out.println("StatusMessageSetter: target.getText()=" + target.getText());
//   }
// });
if ( !target.getText().trim().equals("") ) {
try {
MessageFader fader = new MessageFader(target);
Thread th = new Thread(fader);
th.start();
th.join();
} catch(InterruptedException intEx){}
}//end if

Thread t = new Thread(new Runnable(){
public void run(){
try {
SwingUtilities.invokeAndWait(new Runnable() {
//SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("Setting label...: " + myText);
target.setForeground(color);
target.setText(myText);
}//end run
});
} catch(InterruptedException intEx) {}
catch(InvocationTargetException invocEx) {}
}
});    //end new Runnable
t.start();
}//end run
};
+(Even when I only request the text from the JLabel when the JButton is clicked (in the JButton's actionPerformed() method) and doing nothing else the result is an empty String!!! And it doesn't matter if I do this directly or using SwingUtilities.invokeLater(); Why? I don't undertand this. I am asking: parentView.getStatusMessageLabel().getText(); (No NullPointerException is thrown. Thus, parentView as well as statusMessageLabel exist and I can see the set text in the statusBar.)+

What I want to do (and I didn't managed this for the time being) is, that I want to display a message to the user what the app is going to do and inform the user afterwards about the result (failure/success) using the JLabel in the StatusBar. (Saying: "I'm going to do this...", executing a time consuming action (parsing a website) and the display the result in the StatusBar. Of course, the messages MUST be displayed well synchronized which means:
Displaying what the app is currently doing and after the action returns this messagfe shall fade and the new message will be displayed.

Maybe anyone could give me some code to solve my problem?!!?

Thanks in advance
Dirk

Edited by: dirku on May 8, 2008 5:05 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 7 2008
Added on May 8 2008
8 comments
512 views