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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:35:47+00:00 2026-05-26T15:35:47+00:00

I seem to be having trouble getting to grips with implementing a simple JTable

  • 0

I seem to be having trouble getting to grips with implementing a simple JTable that when a button is pressed on a GUI it simply updates the contents of the table

So far I seem to be getting mixed ideas of what type of table to implement to do this but from searching around it seems to be DefaultTable model.
So far I have declared the table

private JTable table;
static Object[] columnNames = new Object[]{"Column 1","Column 2"};
static Object[][] rowData = { {"1", "2"} };    

public TestTables() { 
    DefaultTableModel tableModel; 
    tableModel = new DefaultTableModel(rowData, columnNames);
}

I’m not even sure if I have declared this correctly, but when I run the GUI it shows the table data correctly. I just dont understand how I can update it using an action performed

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == X1btn) {
    // not sure how to set table to be {"3", "4"} instead
}
  • 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-05-26T15:35:47+00:00Added an answer on May 26, 2026 at 3:35 pm

    In your actionPerformed method, get a reference to the JTable’s model (the details of this will depend on your program’s structure — something we don’t know at present) which will be a DefaultTableModel, and then you can easily do whatever needs to be done with this object including adding rows, removing rows, changing values held by cells…

    If you’re still stuck, consider creating and posting a minimal compilable and runnable example that demonstrates your problem, an sscce.

    For example:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    public class TestTables {
       static Object[] columnNames = new Object[] { "Column 1", "Column 2" };
       static Integer[][] rowData = { {1, 2}, {3, 4} };
    
       private JPanel mainPanel = new JPanel();
       private DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);  
       private JTable table = new JTable(tableModel);   
    
       public TestTables() {
          JButton timesTwoBtn = new JButton("Multiply By 2");
          timesTwoBtn.addActionListener(new ActionListener() {
    
             public void actionPerformed(ActionEvent e) {
                for (int row = 0; row < tableModel.getRowCount(); row++) {
                   for (int col = 0; col < tableModel.getColumnCount(); col++) {
                      Integer value = (Integer) tableModel.getValueAt(row, col);
                      value *= 2;
                      tableModel.setValueAt(value, row, col);
                   }
                }
             }
          });
          JPanel btnPanel = new JPanel();
          btnPanel.add(timesTwoBtn);      
    
    
          mainPanel.setLayout(new BorderLayout());
          mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
          mainPanel.add(btnPanel, BorderLayout.SOUTH);
       }
    
       public JPanel getMainPanel() {
          return  mainPanel;
       }
    
       private static void createAndShowGui() {
          TestTables testTables = new TestTables();
    
          JFrame frame = new JFrame("TableFoo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(testTables.getMainPanel());
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble getting my head around algorithm analysis. I seem to be okay
I'm having trouble getting an INSERT query to execute properly, and I can't seem
I seem to be having trouble getting a 'proper' connection between my Java server
This may be something common and trivial, but I seem to be having trouble
I'm having trouble with MySQL or Python and can't seem to isolate the problem.
I am having a bit of trouble. It does not seem to be a
Seem to be having an issue with std::auto_ptr and assignment, such that the object
I am having an issue that I can't seem to figure out. Hopefully somebody
I'm having trouble getting the following JQuery script to function properly - its functionality
I'm having trouble getting regular expressions with leading / trailing $'s to match in

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.