I have a strange problem: From a MenuBar I (or the user) can call two actions. One does importing some data, the other deletes some data. Both are presented through a JDialog. Now the weird thing is that both share effectively the same layout and yet while the import dialogue works perfectly, the delete dialogue vanished before any interaction can happen. I do call them seperately.
For comparison consider these parts:
- the Import dialogue
JDialog ImportFenster= new JDialog(Fenster,
"Import von Nutzern");
JTextField Name= new JTextField("Vorname [Leer] Name",30);
JFormattedTextField ID =new JFormattedTextField(
NumberFormat.getIntegerInstance());
ID.setColumns(8);
JButton Enter = new JButton("OK");
JButton Abort =new JButton("Abbruch");
JLabel InfoID= new JLabel("Wird keine ID angegeben, wird eine automatisch erzeugt");
GridBagLayout ImportLayout= new GridBagLayout();
GridBagConstraints LayoutConstraints=new GridBagConstraints();
ImportFenster.setLayout(ImportLayout);
LayoutConstraints.fill=GridBagConstraints.VERTICAL;
LayoutConstraints.gridwidth=3;
LayoutConstraints.gridx=0;
LayoutConstraints.gridy=0;
LayoutConstraints.ipadx=5;
LayoutConstraints.ipady=5;
ImportFenster.add(InfoID,LayoutConstraints);
LayoutConstraints.gridwidth=1;
LayoutConstraints.ipadx=10;
LayoutConstraints.ipady=25;
LayoutConstraints.gridx=1;
LayoutConstraints.gridy=1;
ImportFenster.add(ID,LayoutConstraints);
LayoutConstraints.ipadx=10;
LayoutConstraints.ipady=25;
LayoutConstraints.gridx=1;
LayoutConstraints.gridy=2;
ImportFenster.add(Name,LayoutConstraints);
LayoutConstraints.ipadx=10;
LayoutConstraints.ipady=25;
LayoutConstraints.gridx=3;
LayoutConstraints.gridy=3;
ImportFenster.add(Enter,LayoutConstraints);
LayoutConstraints.ipadx=10;
LayoutConstraints.ipady=25;
LayoutConstraints.gridx=0;
LayoutConstraints.gridy=3;
ImportFenster.add(Abort,LayoutConstraints);
LayoutConstraints.gridx=0;
LayoutConstraints.gridy=1;
ImportFenster.add(new JLabel("Nutzer-ID:"),LayoutConstraints);
LayoutConstraints.gridx=0;
LayoutConstraints.gridy=2;
ImportFenster.add(new JLabel("Nutzer-Name:"),LayoutConstraints);
ImportFenster.setSize(750,350);
ImportFenster.setVisible(true);
- the delete dialogue
JDialog EntfernenFenster= new JDialog(Fenster,
"Löschen von Nutzern");
JFormattedTextField ID =new JFormattedTextField(
NumberFormat.getIntegerInstance());
ID.setColumns(8);
System.out.println("Dialog:"+EntfernenFenster.isShowing());
JButton Enter = new JButton("OK");
JButton Abort =new JButton("Abbruch");
System.out.println("Knöppe sind da:"+Enter.isShowing()+"\t"+Abort.isShowing());
GridBagLayout EntfernenLayout= new GridBagLayout();
EntfernenFenster.setLayout(EntfernenLayout);
GridBagConstraints LayoutConstraints=new GridBagConstraints();
LayoutConstraints.fill=GridBagConstraints.VERTICAL;
LayoutConstraints.gridwidth=1;
LayoutConstraints.ipadx=10;
LayoutConstraints.ipady=25;
LayoutConstraints.gridx=1;
LayoutConstraints.gridy=1;
EntfernenFenster.add(ID,LayoutConstraints);
LayoutConstraints.ipadx=10;
LayoutConstraints.ipady=25;
LayoutConstraints.gridx=0;
LayoutConstraints.gridy=1;
EntfernenFenster.add(new JLabel("Nutzer-ID:"));
LayoutConstraints.ipadx=10;
LayoutConstraints.ipady=25;
LayoutConstraints.gridx=2;
LayoutConstraints.gridy=2;
EntfernenFenster.add(Enter,LayoutConstraints);
LayoutConstraints.ipadx=10;
LayoutConstraints.ipady=25;
LayoutConstraints.gridx=0;
LayoutConstraints.gridy=2;
EntfernenFenster.add(Abort,LayoutConstraints);
EntfernenFenster.setSize(500, 200);
EntfernenFenster.setVisible(true);
I would like to know where my mistake is. And of course how to fix this.
You could try to move the code of the ActionListenerer's actionPerformed to a new method
void showEntfernenFenster()and doFor instance in:
actionPerformedhappens on the single thread handling GUI events in the AWT queue. Then little else for the GUI can be done.AWTEventQueue.invokeLaterorSwingUtilities.invokeLater(identical) will indeed call the passed "Runnable" after theactionPerformedis processed. And then again the GUI is responsive, redrawing etcetera, In general do not do long things in actionPerformed.The why:
In order to redraw, invalidate multiple screen regions, and all in a non-parallel manner - sequentially -, many libraries across programming languages and even on operating system level use this simplifying technique: a queue of events and one thread handling them.