Question

First, let me point out that I'm new to programming Java Desktop GUI's. I have created a desktop pane that can hold multiple JInternalFrames. For now, each JinternalFrame contains the same GUI components and a new jInternalFrame (Herein referred to as Internal Frame) can be created via a menu item click.

The elements on each internal Frame are as follows: 3 Jlabels, 3 JTextFields, a Jbutton and a Jtable. After rendering the first Internal Frame I populate the 3 text fields with information and then click the button and a new row is added to the table. The row contains the information used to populate the text fields.

Opening a second Internal Frame gives me, visually, the exact interface of the first. I populate this frame in the same manner,click the button and voila, the table in the second frame is populated.

The issue arises when I return to the previous internal frame. I can type into the text fields without issue but clicking the button to populate the table causes the table in the second Internal Frame to be populated. I suspect that I'm sharing a datamodel but am uncertain as to how to create distinct datamodels for jInternalFrame jTables when the GUI for each frame is the same.

Below is the code (as I am new to Java Desktop GUI development I followed one of the Oracle How-To's to render the internal Frame itself - very simple example):

public class InternalFrames extends JFrame 
                        implements ActionListener  {
private static final long serialVersionUID = 1L;
JDesktopPane desktop;

JLabel label1;
JLabel label2;
JLabel label3;
JLabel label4;

JTextField text1;
JTextField text2;
JTextField text3;

JTable table1;

public InternalFrames(){
    super("Practice Internal Frames");

    int inset = 50;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(inset,inset,screenSize.width - (inset*2),screenSize.height - (inset*2));
    desktop = new JDesktopPane();

    createFrame();
    setContentPane(desktop);

    setJMenuBar(setMenuBar());


}

protected JMenuBar setMenuBar(){

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Document");
    menu.setMnemonic(KeyEvent.VK_D);
    menuBar.add(menu);

    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setMnemonic(KeyEvent.VK_N);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("New");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem("Quit");
    menuItem.setMnemonic(KeyEvent.VK_Q);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
    menuItem.setActionCommand("Quit");
    menuItem.addActionListener(this);
    menu.add(menuItem);



    return menuBar;
}

public void actionPerformed(ActionEvent e){
    if ("New".equals(e.getActionCommand())){
        createFrame();
    } else if ("Quit".equals(e.getActionCommand())){

    }

}

protected void createFrame(){
    InternalFrame internalFrame = new InternalFrame();

    label1 = new JLabel("Name");
    label2 = new JLabel("Email Address:");
    label3 = new JLabel("Mobile Number:");
    label4 = new JLabel("Test Frames");

    label1.setSize(100,10);
    label2.setSize(100,10);
    label3.setSize(100,10);
    label4.setSize(200,10);

    text1 = new JTextField(40);
    text2 = new JTextField(40);
    text3 = new JTextField(40);

    internalFrame.setLayout(new MigLayout());
    internalFrame.getContentPane().add(label1);
    internalFrame.getContentPane().add(text1, "wrap");
    internalFrame.getContentPane().add(label2);
    internalFrame.getContentPane().add(text2, "wrap");
    internalFrame.getContentPane().add(label3);
    internalFrame.getContentPane().add(text3,"wrap");
    internalFrame.getContentPane().add(label4, "span 2, wrap");
    internalFrame.getContentPane().add(new JScrollPane(createTable()), "span 2 2, wrap");
    internalFrame.getContentPane().add(createButton());


    internalFrame.setVisible(true);
    desktop.add(internalFrame);

    try {

        internalFrame.setSelected(true);

    } catch (java.beans.PropertyVetoException pve){ }


}

protected JTable createTable(){
    DefaultTableModel dModel = new DefaultTableModel();
    table1 = new JTable(dModel);
    dModel.addColumn("Name");
    dModel.addColumn("Email Address");
    dModel.addColumn("Mobile Number");

    return table1;
}

protected JButton createButton(){
    JButton button1 = new JButton("Add New List Member");
    button1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            System.out.println("You clicked the button");
            populateTable(table1);
        }
    });
    return button1;
}

protected void populateTable(JTable theTable){

    if (validateEntry() == 0)
    {
        ListMembers listMember = new ListMembers();
        listMember.setName(text1.getText());
        listMember.setEmailAddress(text2.getText());
        listMember.setMobilePhone(text3.getText());
        Object[] data = {listMember.getName(),listMember.getEmailAddress(),listMember.getMobilePhone()};
        DefaultTableModel dm = (DefaultTableModel) table1.getModel();
        dm.addRow(data);


    }

}

private static void createAndShowGUI(){
    JFrame.setDefaultLookAndFeelDecorated(true);
    InternalFrames internalFrame = new InternalFrames();
    internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    internalFrame.setVisible(true);

}


public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });

}
}

Any help would be greatly appreciated.

Was it helpful?

Solution

Each time you create a new frame, you initialize a single field (table1) with this newly created table. And the listener of every button you create populates the table referenced by this unique table1 variable. The same goes for your text fields, etc.

Either only use local variables, and pass them between methods (for example, you need to pass the table you just created to the method creating the button, in order for this button to populate this table. Or you extract all the code to a new class, MyInternalFrame (choose a better name), make the table, textfields and buttons instance fields of this new class, and create a new instance of this MyInternalFrame class each time you need a new one.

This second solution looks like the best one to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top