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!