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 8671299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:55:00+00:00 2026-06-12T18:55:00+00:00

I would like to add the data entered in the text box to the

  • 0

I would like to add the data entered in the text box to the table and keep on incremented ID. the data is added when the button “Add” is clicked.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ex1 extends JFrame {
    GridLayout experimentLayout = new GridLayout(3, 3);
    private javax.swing.JButton jButton1,jButton2,jButton3;
    private javax.swing.JLabel jLabel1, jLabel2, jLabel3;

    private javax.swing.JTextField jTextField1,jTextField2,jTextField3;

    String columns[] = {"ID","Name","Age","Gender"};
    Object data[][] = {
            {"0","Tom",new Integer(20),"Male"},
            {"1","Tina", new Integer(18), "Female"},
            {"2","Raj",new Integer(19),"Male"}

    };

    public void addComponentsToPane(final Container pane) {
        final JPanel compsToExperiment = new JPanel();

        compsToExperiment.setLayout(experimentLayout);
        JPanel controls = new JPanel();
        controls.setLayout(new GridLayout(8, 3));

        jLabel1 = new javax.swing.JLabel("Name: ");
        jLabel2 = new javax.swing.JLabel("Age: ");
        jLabel3 = new javax.swing.JLabel("Gender: ");
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton("Add");
        jButton2 = new javax.swing.JButton("Next");
        jButton3 = new javax.swing.JButton("Per");

        compsToExperiment.add(jLabel1);
        compsToExperiment.add(jLabel2);
        compsToExperiment.add(jLabel3);

        compsToExperiment.add(jTextField1);
        compsToExperiment.add(jTextField2);
        compsToExperiment.add(jTextField3);
        compsToExperiment.add(jButton1);

        JTable table = new JTable(data,columns);
        controls.add(table.getTableHeader());
        controls.add(table);
            pane.add(compsToExperiment, BorderLayout.NORTH);
        pane.add(controls, BorderLayout.CENTER);
    }

    /**
     * Create the GUI and show it. For thread safety, this method is invoked
     * from the event dispatch thread.
     */
    private static void createAndShowGUI() {        
        ex1 frame = new ex1();      
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.addComponentsToPane(frame.getContentPane());
        frame.setSize(500,500);
        //frame.pack();     
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        try {           
            UIManager.setLookAndFeel(
                "javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);

        // Schedule a job for the event dispatch thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

I know the the design is all over the place I’ve just started learning java, feel free to point out improvement that can made.
Thank you in advance 🙂

  • 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-12T18:55:01+00:00Added an answer on June 12, 2026 at 6:55 pm

    To add the data entered in the JTextFields you will need to register an ActionListener to your add button, in this case jButton1.

    To add entries to your table model you could use a mutable model such as DefaultTableModel:

    DefaultTableModel model = new DefaultTableModel(data, columns);
    

    In the actionPerformed method of your ActionListener you will have:

    String nextRowId = Integer.toString(model.getRowCount());
    model.addRow(new Object[] { 
                  nextRowId, 
                  jTextField1.getText(), 
                  jTextField2.getText(), 
                  jTextField3.getText() });
    

    The following are recommended:

    • Use more meaningful names for variables.
    • Allocate more space to the JTable which could potentially hold a large amount of data. Also place inside a JScrollPane.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add a data table to bottom of a (line) chart
I would like to add some data to the body of a http request
I would like to add some custom data to an image the user generates
I would like to add a column to every data frame in my R
I would like to add a column from behind code to a data bound
I have a empty table that I would like to populate based on data
I would like to add some data validation on a cell in the latest
I have own project and i would like add for this class same as
I have a ListView and each item have a TextView. I would like add
I would like to add a GestureDetector to all views (view groups) of an

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.