Swing object: first setText() gets "stuck" when using Mac Java SE 6
I am a Java newbie trying to maintain an application that works fine under J2SE 5.0 (32- and 64-bit) but has a very specific problem when run under Java SE 6 64-bit:
[Tims-MPB:~] tlynch% java -version
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03-226)
Java HotSpot(TM) 64-Bit Server VM (build 14.1-b02-92, mixed mode)
The application is cross-platform and reportedly works correctly on Java SE 6 under Windows, though I haven't been able to verify that myself.
The program uses a JTextField for some text entry and a JLabel to indicate the text to be entered. The first time the showDialog() method is called to set the label text and display the dialog, it works correctly, but subsequent calls all result in the display of the label from the initial invocation rather than the one most recently specified via setText().
public void showDialog(String msgText)
{
System.out.println("set ChatDialog: " + msgText);
jLabel1.setText(msgText);
jLabel1.repaint(); // I added this; it didn't help
System.out.println("get ChatDialog: " + jLabel1.getText());
super.setVisible(true);
}
[the full text of the class is provided below]
The added printlns validate that expected text is passed to the label's setText() metho开发者_StackOverflow社区d and is confirmed by retrieving it using getText(), but what shows up on the screen/GUI is always the text from the very first time the method was called for the object.
A similar issue is observed with a JTextArea used to label another dialog box.
These problem are consistent across multiple Mac systems running Java SE 6 under OS 10.5.x and 10.6.x, but they are never observed when one reverts to J2SE 5.0.
If there is some background information pertinent to this problem that I have omitted, please let me know. Any insights or advice appreciated.
package gui;
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
// Referenced classes of package gui:
// MyJPanel, ChatDialog_jTextField1_keyAdapter, WarWindow
public class ChatDialog extends JDialog
{
public ChatDialog(JFrame parent, WarWindow w)
{
super(parent, true);
text = "";
borderLayout1 = new BorderLayout();
jPanel1 = new MyJPanel();
borderLayout2 = new BorderLayout();
jPanel2 = new MyJPanel();
jPanel3 = new MyJPanel();
jLabel1 = new JLabel();
jTextField1 = new JTextField();
warWindow = w;
try
{
jbInit();
}
catch(Exception exception)
{
System.out.println("Problem with ChatDialog init");
exception.printStackTrace();
}
return;
}
public String getText()
{
return text;
}
void jTextField1_keyPressed(KeyEvent e)
{
int id = e.getKeyCode();
switch(id)
{
case 10: // '\n'
text = jTextField1.getText();
setVisible(false);
break;
}
}
private void jbInit()
throws Exception
{
setLocation(232, 450);
setSize(560, 60);
setModal(true);
setResizable(false);
setUndecorated(true);
getContentPane().setLayout(borderLayout1);
jPanel1.setLayout(borderLayout2);
jPanel2.setMinimumSize(new Dimension(10, 20));
jPanel2.setPreferredSize(new Dimension(10, 20));
jLabel1.setPreferredSize(new Dimension(380, 15));
jLabel1.setHorizontalAlignment(0);
jLabel1.setText("Chat Message");
jTextField1.setPreferredSize(new Dimension(520, 21));
jTextField1.setRequestFocusEnabled(false);
jTextField1.addKeyListener(new ChatDialog_jTextField1_keyAdapter(this));
getContentPane().add(jPanel1, "Center");
jPanel1.add(jPanel2, "North");
jPanel2.add(jLabel1, null);
jPanel1.add(jPanel3, "Center");
jPanel3.add(jTextField1, null);
}
public void setVisible(boolean b)
{
jTextField1.setText("");
super.setVisible(b);
}
public void showDialog(String msgText)
{
System.out.println("set ChatDialog: " + msgText);
jLabel1.setText(msgText);
jLabel1.repaint(); // I added this; it didn't help
System.out.println("get ChatDialog: " + jLabel1.getText());
super.setVisible(true);
}
void this_keyPressed(KeyEvent e)
{
int id = e.getKeyCode();
switch(id)
{
case 10: // '\n'
System.exit(88);
break;
}
}
BorderLayout borderLayout1;
BorderLayout borderLayout2;
JLabel jLabel1;
JPanel jPanel1;
JPanel jPanel2;
JPanel jPanel3;
JTextField jTextField1;
String text;
WarWindow warWindow;
}
Visual components are supposed to be manipulated only by the event-handling thread.
Perhaps you should do something like this:
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
System.out.println("set ChatDialog: " + msgText);
jLabel1.setText(msgText);
System.out.println("get ChatDialog: " + jLabel1.getText());
super.setVisible(true);
}
});
Sorry for the late follow-up. This problem was fixed when Apple released Java for Mac OS X 10.6 Update 1:
http://support.apple.com/downloads/Java_for_Mac_OS_X_10_6_Update_1
Thanks all for the suggestions and advice.
I have used JLabel.setText() successfully in 10.6.x in several swing apps, i suggest there is possibly something else at play here.
精彩评论