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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:26:44+00:00 2026-06-08T11:26:44+00:00

i have one jtable with listselectionlistener i can dynamically added the new rows in

  • 0

i have one jtable with listselectionlistener i can dynamically added the new rows in to my table,when i select the row the selected row content will be appear in text box i can able to edit and remove the data ,for my application i stored the table data into xml file,when i add new row,that will be added into table successfully. but when i select a row and update means table doesn’t get update(here i call load table()).( but updated values changed in xml file correctly)
this my sample code for creating table*

    ListSelectionModel selectionModel;
    JTable table1;
    model = new DefaultTableModel();
    table = new JTable(model);       table.setRowHeight(20);
    selectionModel = table.getSelectionModel();                  
    selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

 selectionModel.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {

            stxtBox.setText("");
            ptxtBox.setText("");
            ntxtBox.setText("");

            if (!e.getValueIsAdjusting()) {



             model1 = table.getSelectionModel();
                int lead = model1.getLeadSelectionIndex();
                int columns = table.getColumnCount();
                String sip = "";
                String sport = "";
                String snoq = "";
                for (int col = 0; col < columns; col++) {
                    Object o = table.getValueAt(lead, col);

                    if (col == 0) {
                        sip += o.toString();
                        stxtBox.setText(sip);
                        selectedip = sip;

                    } else if (col == 1) {

                        sport += o.toString();
                        ptxtBox.setText(sport);
                        selectedport = sport;
                    } else {

                        snoq += o.toString();
                        ntxtBox.setText(snoq);
                    }
                    selectedreq = snoq;

                }



            }table.clearSelection();
        }
    });

i load the table content like this

  int rowCount=0;

            File file = new File("serverconfig.xml");

            if (file.exists())

            {

                System.out.print("in load");
                int e = table.getRowCount();

                if(e> 0)
                {

                while (table.getRowCount() > 0) {
                    ((DefaultTableModel) table.getModel()).removeRow(0);
                }

                }

//here i will load table content from my xml file (that's working fine)

the problem is when i update my table content i will call loadtable() function ever time i will get this error

java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
    at java.util.Vector.elementAt(Unknown Source)
    at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
    at javax.swing.JTable.getValueAt(Unknown Source)
    at Testsample$18.valueChanged(Testsample.java:1810)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.insertIndexInterval(Unknown Source)
    at javax.swing.JTable.tableRowsInserted(Unknown Source)
    at javax.swing.JTable.tableChanged(Unknown Source)
    at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
    at javax.swing.table.AbstractTableModel.fireTableRowsInserted(Unknown Source)
    at javax.swing.table.DefaultTableModel.insertRow(Unknown Source)
    at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
    at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
    at Testsample.loadtable(Testsample.java:577)
    at Testsample$10.actionPerformed(Testsample.java:1551)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
  • 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-08T11:26:45+00:00Added an answer on June 8, 2026 at 11:26 am

    for example

    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

I have one JTable with vertical scroll bar on it,when i added new row
I have a JTable where I can select one or more cells. I also
I have created one jTable. I want to display the data into table from
I have a ScrollPane and JTable that should only have one row of data.
I have a JTable and I need to remove a row, namely, the selected
how can I get a JTable to layout some rows with just one column
I have one JTable which show list of book and it can be filtered
I have a JTable which contains a one column, cell render of each table
I have a JTable inside a JScrollPane. In one of the columns in the
My question like this: I have a JTable, I put a JProgressBar in one

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.