Jtable insert new inserted row at the top.... I want the new inserted row below.

Problem

10 james microsoft c.e.o
9 peter linux mgr


requirement

9 peter linux mgr
10 james microsoft c.e.o

JTable is really my favorite JComponents(JComboBox and JButton too, but...), let's talking about that, but without your code is only on the Academic level :-)

if(e.getSource() == insertButton)
{
model.addRow(rowNumber,new Object(){"1","2","3"});
}
//--------- Model is Default Table Model Object ----------------
//---------Model is used in table which is JTable Object ---------------

when i press insert button a new row is inserted and it remain at top rather then below.

and what do you see now, or are your tried ... me

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class DefaultTableModelDemo {

    public static final String[] COLUMN_NAMES = {
        "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
    };
    private DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0);
    private JTable table = new JTable(model);
    private JPanel mainPanel = new JPanel(new BorderLayout());
    private Random random = new Random();

    public DefaultTableModelDemo() {
        JButton addDataButton = new JButton("Add Data");
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addDataButton);
        addDataButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addDataActionPerformed();
            }
        });
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if (isRowSelected(row) && isColumnSelected(column)) {
                    ((JComponent) c).setBorder(new LineBorder(Color.red));
                }

                return c;
            }
        };
        mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    }

    private void addDataActionPerformed() {
        for (int i = 0; i < 5; i++) {
            Object[] row = new Object[COLUMN_NAMES.length];
            for (int j = 0; j < row.length; j++) {
                row[j] = random.nextInt(5);
            }
            model.addRow(row);
        }
    }

    public JComponent getComponent() {
        return mainPanel;
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("DefaultTableModelDemo");
                frame.getContentPane().add(new DefaultTableModelDemo().getComponent());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

Amazing.... it worked like u said... Thanx a bunch.... i m not lieing also no intention to try a master poster..... I know u are professional ;-)

Use netbean IDE 7.0/6.9.1.
This task will be much easier.
Here you do not have to write so much.
This softwear is a developed field for Java.
Here you need to open a jframe window and then from the swing object you can choose a jtable and give the row number.

Anuradha, The problem was i wasnt using addRow.. But thanx to mKorbel it corrected. I already used Net Beans , Problem is visual tools dont provide enough controls so its better to code in case of performance matter s/w project's.

This article has been dead for over six months. Start a new discussion instead.