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


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


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

Jtable insert new inserted row at the top

I never ever see that

havnt u use addRow of Default Model Class......................... try it.

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();
        addDataButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

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

            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);

    public JComponent getComponent() {
        return mainPanel;

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

            public void run() {
                JFrame frame = new JFrame("DefaultTableModelDemo");
                frame.getContentPane().add(new DefaultTableModelDemo().getComponent());

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 ;-)

hmmm I'm not professional, I'm Java fan, that worldless difference :-)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.