2

I have a Java application that uses a JTable to display some data. I want to enter the data from a JDialog.

The problem that I am facing is that the main window GUI won't refresh anymore after the JDialog opens. I have tried to change it to a JFrame and I get the same behavior. The same happens if I change the JTable with a JPanel. Everything works fine until a window opens on top of the main window. After this, the GUI elements won't refresh but the buttons will still react to clicks.

Could someone please help me understand what is wrong.

Sorry for not sharing any code. It was 5 in the morning and I totally forgot. I have a frame with a JTable and a JButton. The button opens the JDialog. The JDialog has a panel nested with a JTextField and a JButton. When I press the button in the JDialog it sends the data to the main frame. There I want to populate the table with the data received. I can see the data in the debuger. The problem is that it won't show in the table until I restart the application. I also added a button in the same frame as the table to test the data insertion and it worked just fine.

public class GUIRezervari {

private static JButton butonNew;
private static JTable tabelRezervari;
public static DefaultTableModel dtm;
private static JScrollPane scrollPaneTabel;
private static JPanel panelRezervari;

private GUIRezervari(){

}

public static JPanel getGui(){
    setPanelRezervari();

    return panelRezervari;
}

private static void setPanelRezervari(){
    panelRezervari=new JPanel();
    panelRezervari.setLayout(new BoxLayout(panelRezervari, 0));

    Box hBox=Box.createHorizontalBox();
    hBox.add(Box.createHorizontalGlue());
    hBox.add(boxRezervari());
    hBox.add(Box.createHorizontalGlue());

    panelRezervari.add(hBox);
}
private static Box boxRezervari(){
    Box vBox=Box.createVerticalBox();

    vBox.add(Box.createVerticalStrut(20));
    vBox.add(boxButoane());
    vBox.add(Box.createHorizontalStrut(10));
    vBox.add(boxTabel());
    vBox.add(Box.createVerticalStrut(20));

    return vBox;
}

private static Box boxButoane(){
    Box hBox=Box.createHorizontalBox();

    butonNew=new JButton("New");
    butonNew.addActionListener(new RezervareNouaAL());

    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(butonNew);
    hBox.add(Box.createHorizontalGlue());
    hBox.add(Box.createHorizontalStrut(10));

     return hBox;
}

private static Box boxTabel(){
    Box hBox=Box.createHorizontalBox();

    dtm=new DefaultTableModel();
    dtm.addColumn("Date");
    dtm.addColumn("Name");

    tabelRezervari=new JTable(dtm);
    scrollPaneTabel=new JScrollPane(tabelRezervari);

    hBox.add(scrollPaneTabel);

    return hBox;
}
}

public class RezervareNouaAL implements ActionListener{

@Override
public void actionPerformed(ActionEvent ae) {

    GUIRezervare.getFrame((JFrame)GUIRezervari.getGui().getParent());
}

}

public class GUIRezervare {

private static JLabel labelName;
private static JLabel labelDate;
private static JTextField fieldName;
private static JTextField fieldDate;
private static JButton butonSalveaza;
private static JButton butonAnuleaza;
private static JPanel panelRezervare;
private static JDialog frame;

private GUIRezervare(){

}

public static void getFrame(JFrame panel) {

    setGui();
    frame = new JDialog(panel, "Rezervare", true);
    frame.add(panelRezervare);
    frame.pack();
    frame.setVisible(true);

    frame.setTitle("Adauga rezervare");

}

public static JPanel getGui(){
    setGui();

    return panelRezervare;
}

private static void setGui(){
    panelRezervare=new JPanel();
    panelRezervare.setLayout(new BoxLayout(panelRezervare, 0));
    panelRezervare.setBorder(BorderFactory.createTitledBorder("Rezervare"));
    panelRezervare.setBackground(new Color(242,197,61));

    Box vBox=Box.createVerticalBox();

    vBox.add(Box.createVerticalStrut(10));
    vBox.add(panelRezervare());
    vBox.add(Box.createVerticalStrut(10));
    vBox.add(boxButoane());
    vBox.add(Box.createVerticalStrut(10));

    panelRezervare.add(vBox);
}
private static JPanel panelRezervare(){
    JPanel panelRezervareT=new JPanel();
    panelRezervareT.setBorder(BorderFactory.createTitledBorder("Date rezervare"));
    panelRezervareT.add(boxRezervare());
    panelRezervareT.setBackground(new Color(169,217,190));

    return panelRezervareT;
}
private static Box boxRezervare(){
    Box hBox=Box.createHorizontalBox();

    hBox.add(Box.createHorizontalGlue());
    hBox.add(boxStanga());
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(Box.createHorizontalGlue());

    return hBox;
}

private static Box boxStanga(){
    Box vBox=Box.createVerticalBox();

    vBox.add(boxName());
    vBox.add(Box.createVerticalStrut(5));
    vBox.add(boxDate());
    vBox.add(Box.createVerticalStrut(5));

    return vBox;
}

private static Box boxDate(){
    Box hBox=Box.createHorizontalBox();

    labelDate=new JLabel("Date :");
    fieldDate=new JTextField();

    hBox.add(labelDate);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(Box.createHorizontalGlue());
    hBox.add(fieldDate);

    return hBox;
}

private static Box boxName(){
    Box hBox=Box.createHorizontalBox();

    labelName=new JLabel("Name :");
    fieldName=new JTextField();

    hBox.add(labelName);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(Box.createHorizontalGlue());
    hBox.add(fieldName);

    return hBox;
}


private static Box boxButoane(){
    Box hBox=Box.createHorizontalBox();

    butonSalveaza=new JButton("Salveaza");
    butonSalveaza.addActionListener(new SalveazaClientAL());
    butonAnuleaza=new JButton("Anulaeza");
    butonAnuleaza.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent ae) {

            frame.dispose();
        }

    });

    hBox.add(butonSalveaza);
    hBox.add(Box.createHorizontalStrut(10));
    hBox.add(butonAnuleaza);

    return hBox;
}

public static String getFieldName() {
    return fieldName.getText();
}

public static String getFieldDate() {
    return fieldDate.getText();
}

public static void inchideFrame(){
    frame.dispose();
}
}

public class SalveazaClientAL implements ActionListener{

@Override
public void actionPerformed(ActionEvent ae) {

    GUIRezervari.dtm.addRow(new Object[]{GUIRezervare.getFieldName(),GUIRezervare.getFieldDate()});
GUIRezervare.inchideFrame();
}

}

This is the code. Thank you for your help!

Gabriel
  • 77
  • 2
  • 12
  • 2
    Could you please post some code. Also, please explain what you mean by 'refresh' - are you saying that you can no longer interact with the main JFrame, or that the JFrame isn't repainted (just appears as a plain grey color), or are you expecting that the JTable would automatically update with your JDialog data? Saying that it "doesn't refresh" isn't really specific enough. – wattostudios Oct 26 '12 at 01:50
  • 1
    *"Has anyone any idea why this strange behavior?"* - Several, none of which probably effect you. Show us the code – MadProgrammer Oct 26 '12 at 01:52
  • 1
    should work, so seems like something wrong with the code you are not showing ... – kleopatra Oct 26 '12 at 13:51
  • I added the code. I am expecting the `JTable` to update with the data from the `JDialog`. Thanks! – Gabriel Oct 29 '12 at 07:21

1 Answers1

2

This may happen if your JDialog is modal. Instead, create a modeless dialog, and let your main window register as aPropertyChangeListener to the dialog. In this example, a JPanel in the main window listens to an instance of ObservedPanel in the dialog.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • hmm ... can't reproduce. Something special in macOS? – kleopatra Oct 26 '12 at 13:50
  • No, I misread _won't refresh_ as _not responsive to user gestures_, as might happen with a _modal_ dialog. On reflection, the former suggests something blocking or restarting the EDT. More reason to see the code. – trashgod Oct 26 '12 at 13:54
  • Your example runs great. Can you have a look at my code and tell me why the table is not repainting? Thanks! – Gabriel Oct 31 '12 at 09:55