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

  • Home
  • SEARCH
  • 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 7981065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:15:39+00:00 2026-06-04T10:15:39+00:00

Every example I can find either goes through creating a table without a gui

  • 0

Every example I can find either goes through creating a table without a gui or explains how to populate from a database. I must be missing something simple but here’s what I have so far based off of different tutorials and at How to Use Tables: Creating a Table Model.

private void btnAddRecordToTableActionPerformed(java.awt.event.ActionEvent evt) {
    String strPledgeArray[][] = 
    {
        {"1","2","3","4","5"}
    };
String columnNames[] = {"Column 1", "Column 2", "Column 3","Column 4","Column 5"};
    JTable tblDataGridResults = new JTable(strPledgeArray, columnNames);
}

After pressing the button nothing happens however. What am I doing wrong?

From my reading I think it’s because there’s no model but every time I see anything about models they are in different classes or using constructors that just create errors in my program.

  • 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-04T10:15:40+00:00Added an answer on June 4, 2026 at 10:15 am

    1) code line

    JTable tblDataGridResults = new JTable(strPledgeArray, columnNames);
    

    create a new JTable that isn’t added to the GUI

    2) better would be to add ColumnNames and Rows to the DefaultTableModel, please read in the tutorial how TableModel works

    EDIT

    still don’t understand ???

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class RemoveAddRows extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private Object[] columnNames = {"Type", "Company", "Shares", "Price"};
        private Object[][] data = {
            {"Buy", "IBM", new Integer(1000), new Double(80.50)},
            {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)},
            {"Sell", "Apple", new Integer(3000), new Double(7.35)},
            {"Buy", "Nortel", new Integer(4000), new Double(20.00)}
        };
        private JTable table;
        private DefaultTableModel model;
    
        public RemoveAddRows() {
    
            model = new DefaultTableModel(data, columnNames) {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public Class getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
            };
            table = new JTable(model) {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                    Component c = super.prepareRenderer(renderer, row, column);
                    int firstRow = 0;
                    int lastRow = table.getRowCount() - 1;
                    int width = 0;
                    if (row == lastRow) {
                        ((JComponent) c).setBackground(Color.red);
                    } else if (row == firstRow) {
                        ((JComponent) c).setBackground(Color.blue);
                    } else {
                        ((JComponent) c).setBackground(table.getBackground());
                    }
                    /*if (!isRowSelected(row)) {
                    String type = (String) getModel().getValueAt(row, 0);
                    c.setBackground("Buy".equals(type) ? Color.GREEN : Color.YELLOW);
                    }
                    if (isRowSelected(row) && isColumnSelected(column)) {
                    ((JComponent) c).setBorder(new LineBorder(Color.red));
                    }*/
                    return c;
                }
            };
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);
            JButton button1 = new JButton("Remove all rows");
            button1.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent arg0) {
                    if (model.getRowCount() > 0) {
                        for (int i = model.getRowCount() - 1; i > -1; i--) {
                            model.removeRow(i);
                        }
                    }
                    System.out.println("model.getRowCount() --->" + model.getRowCount());
                }
            });
            JButton button2 = new JButton("Add new rows");
            button2.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent arg0) {
                    Object[] data0 = {"Buy", "IBM", new Integer(1000), new Double(80.50)};
                    model.addRow(data0);
                    Object[] data1 = {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)};
                    model.addRow(data1);
                    Object[] data2 = {"Sell", "Apple", new Integer(3000), new Double(7.35)};
                    model.addRow(data2);
                    Object[] data3 = {"Buy", "Nortel", new Integer(4000), new Double(20.00)};
                    model.addRow(data3);
                    System.out.println("model.getRowCount() --->" + model.getRowCount());
                }
            });
            JPanel southPanel = new JPanel();
            southPanel.add(button1);
            southPanel.add(button2);
            add(southPanel, BorderLayout.SOUTH);
        }
    
        public static void main(String[] args) {
            RemoveAddRows frame = new RemoveAddRows();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does almost every example I can find (including this question from about a
Every example I can find is in C++, but I'm trying to keep my
I've tried every example I can find on the web but I cannot get
It seems like every example I can find has to do with using DirectX
Provide an example for the pseudo-regex: Match every url except those from example.com and
I've tried every example i could google, and every solution i could find on
Im trying to find a way either with ajax or jquery that can angle
I am trying to find a function that can fade an element from a
Every example I've seen on the web, e.g. http://www.codeproject.com/KB/docview/jms_to_jms_bridge_activem.aspx , creates a publisher and
Every example I see seems to be for recursively getting files in subdirectories uses

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.