Skip to main content

repainting of components not running after jdialog opened

No replies
gabriel.pelmus
Offline
Joined: 2012-10-31
Points: 0

Hi!
I have an application that has a JTable and a button. When I press the button a JDialog opens with some fields to enter data. When I close the dialog I want the data to populate the JTable. Although the data is saved in a DefeultTableModel variable using the .addRow() method, the view in the JTable does not update. Here is a sample of the code:

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(GUIDateClient.getGui());
    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) {
            //JFrame frame=(JFrame)panelRezervare.getTopLevelAncestor();
            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();
}

}

I can't understand why this is happening. Could someone please help me? Thanks!