In my application I use a class that extends a JDialog. When my application encounters certain errors this diaglog should popup with the appropriate message. The problem I have is that after the message appears, when I click the OK button, the message reappears one or sometimes two more times before going back to the main screen. Below is some of the code that should explain it better.
Code that calls the jdialog. Just press the button to bring up the dialog.
package testb;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
//VS4E -- DO NOT REMOVE THIS LINE!
public class TestB extends JFrame {
private static final long serialVersionUID = 1L;
private JComboBox fromComboBox;
private JTextField jTextField0;
private JButton okButton;
private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
public TestB() {
initComponents();
}
private void initComponents() {
setLayout(new GroupLayout());
add(getJComboBox0(), new Constraints(new Leading(83, 148, 10, 10), new Leading(56, 10, 10)));
add(getJTextField0(), new Constraints(new Leading(85, 136, 10, 10), new Leading(109, 29, 10, 10)));
add(getJButton0(), new Constraints(new Leading(105, 10, 10), new Leading(185, 10, 10)));
setSize(320, 240);
}
private JButton getJButton0() {
if (okButton == null) {
okButton = new JButton();
okButton.setText("OK");
okButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
okButtonMouseMouseClicked(event);
}
});
}
return okButton;
}
private JTextField getJTextField0() {
if (jTextField0 == null) {
jTextField0 = new JTextField();
jTextField0.setText("jTextField0");
}
return jTextField0;
}
private JComboBox getJComboBox0() {
if (fromComboBox == null) {
fromComboBox = new JComboBox();
fromComboBox.setEditable(true);
fromComboBox.setModel(new DefaultComboBoxModel(new Object[] { "" }));
fromComboBox.setDoubleBuffered(false);
fromComboBox.setBorder(null);
fromComboBox.addItem("oranges");
fromComboBox.addItem("lemons");
fromComboBox.addItem("limes");
}
return fromComboBox;
}
private static void installLnF() {
try {
String lnfClassname = PREFERRED_LOOK_AND_FEEL;
if (lnfClassname == null)
lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lnfClassname);
} catch (Exception e) {
System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
+ " on this platform:" + e.getMessage());
}
}
/**
* Main entry of the class.
* Note: This class is only created so that you can easily preview the result at runtime.
* It is not expected to be managed by the designer.
* You can modify it as you like.
*/
public static void main(String[] args) {
installLnF();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TestB frame = new TestB();
frame.setDefaultCloseOperation(TestB.EXIT_ON_CLOSE);
frame.setTitle("TestCancelKeyStroke");
frame.getContentPane().setPreferredSize(frame.getSize());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private void okButtonMouseMouseClicked(MouseEvent event) {
//WarehouseTracer.sMessage = "Testing error message.";
new TestMsgDialog().setVisible(true);
jTextField0.requestFocus();
}
}
Diaglog code:
package testb;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
import org.dyno.visual.swing.layouts.Trailing;
//VS4E -- DO NOT REMOVE THIS LINE!
public class TestMsgDialog extends JDialog {
private static final long serialVersionUID = 1L;
private JButton okButton;
private JTextPane msgTextPane;
private JScrollPane jScrollPane0;
private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
public TestMsgDialog() {
initComponents();
msgTextPane.setText("Testing the dialog message.");
setDefaultCloseOperation(TestMsgDialog.DISPOSE_ON_CLOSE);
setTitle("Message Center");
setLocationRelativeTo(null);
getContentPane().setPreferredSize(getSize());
pack();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
okButton.requestFocusInWindow();
}
});
setAlwaysOnTop(true);
setModal(true);
setVisible(true);
}
public TestMsgDialog(Frame parent) {
super(parent);
initComponents();
}
public TestMsgDialog(Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
public TestMsgDialog(Frame parent, String title) {
super(parent, title);
initComponents();
}
public TestMsgDialog(Frame parent, String title, boolean modal) {
super(parent, title, modal);
initComponents();
}
public TestMsgDialog(Frame parent, String title, boolean modal,
GraphicsConfiguration arg) {
super(parent, title, modal, arg);
initComponents();
}
public TestMsgDialog(Dialog parent) {
super(parent);
initComponents();
}
public TestMsgDialog(Dialog parent, boolean modal) {
super(parent, modal);
initComponents();
}
public TestMsgDialog(Dialog parent, String title) {
super(parent, title);
initComponents();
}
public TestMsgDialog(Dialog parent, String title, boolean modal) {
super(parent, title, modal);
initComponents();
}
public TestMsgDialog(Dialog parent, String title, boolean modal,
GraphicsConfiguration arg) {
super(parent, title, modal, arg);
initComponents();
}
public TestMsgDialog(Window parent) {
super(parent);
initComponents();
}
public TestMsgDialog(Window parent, ModalityType modalityType) {
super(parent, modalityType);
initComponents();
}
public TestMsgDialog(Window parent, String title) {
super(parent, title);
initComponents();
}
public TestMsgDialog(Window parent, String title, ModalityType modalityType) {
super(parent, title, modalityType);
initComponents();
}
public TestMsgDialog(Window parent, String title,
ModalityType modalityType, GraphicsConfiguration arg) {
super(parent, title, modalityType, arg);
initComponents();
}
private void initComponents() {
setTitle("Message Center");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setFont(new Font("Dialog", Font.PLAIN, 12));
setBackground(Color.white);
setModal(true);
setForeground(Color.black);
setAlwaysOnTop(true);
setLayout(new GroupLayout());
add(getOKButton(), new Constraints(new Leading(386, 69, 10, 10), new Trailing(12, 40, 44, 337)));
add(getJScrollPane0(), new Constraints(new Leading(9, 869, 10, 10), new Bilateral(10, 70, 22)));
setSize(885, 505);
}
private JScrollPane getJScrollPane0() {
if (jScrollPane0 == null) {
jScrollPane0 = new JScrollPane();
jScrollPane0.setViewportView(getMsgTextPane0());
}
return jScrollPane0;
}
private JTextPane getMsgTextPane0() {
if (msgTextPane == null) {
msgTextPane = new JTextPane();
msgTextPane.setText("jTextPane0");
msgTextPane.setBackground(null);
msgTextPane.setEditable(false);
msgTextPane.setBorder(null);
msgTextPane.setFont(new Font("Arial", 1,52));
}
return msgTextPane;
}
private JButton getOKButton() {
if (okButton == null) {
okButton = new JButton();
okButton.setText("OK");
okButton.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
okButtonKeyKeyPressed(event);
}
});
okButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
okButtonMouseMouseClicked(event);
}
});
okButton.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent event) {
okButtonFocusFocusGained(event);
}
public void focusLost(FocusEvent event) {
okButtonFocusFocusLost(event);
}
});
}
return okButton;
}
private static void installLnF() {
try {
String lnfClassname = PREFERRED_LOOK_AND_FEEL;
if (lnfClassname == null)
lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lnfClassname);
} catch (Exception e) {
System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
+ " on this platform:" + e.getMessage());
}
}
/**
* Main entry of the class.
* Note: This class is only created so that you can easily preview the result at runtime.
* It is not expected to be managed by the designer.
* You can modify it as you like.
*/
public static void main(String[] args) {
installLnF();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TestMsgDialog dialog = new TestMsgDialog();
dialog.setDefaultCloseOperation(TestMsgDialog.DISPOSE_ON_CLOSE);
dialog.setTitle("Message Center");
dialog.setLocationRelativeTo(null);
dialog.getContentPane().setPreferredSize(dialog.getSize());
dialog.pack();
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
}
});
}
private void okButtonKeyKeyPressed(KeyEvent e) {
if ((e.getKeyCode() == 54) && (e.isShiftDown())){
setVisible(false);
dispose();
}
}
private void okButtonMouseMouseClicked(MouseEvent event) {
setVisible(false);
dispose();
}
private void okButtonFocusFocusGained(FocusEvent event) {
// TODO Auto-generated method stub
}
private void okButtonFocusFocusLost(FocusEvent event) {
// TODO Auto-generated method stub
}
}