Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:16:30+00:00 2026-06-12T02:16:30+00:00

First, let me point out that I’m new to programming Java Desktop GUI’s. I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:16:31+00:00Added an answer on June 12, 2026 at 2:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't figure this one out. At first let me say that my cache
Before describing the problem, let me first point out that this is a distinct
First, let me point out that I read all the posts regarding database versioning,
First let me state that, despite being a fairly new practitioner of TDD, I'm
First let me explain. I have several addresses on the page that I put
First of all I want to point out that I could translate the error
First let me say that I am new to C so my approach is
Hello and let me first explain that I have not used jQuery very much
Let me just first point out to any IE users right now (this is
I have two different domains (let's say www.site1.com and www.site2.com) that point to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.