How to insert rows in jTable?

Ads
 

How to insert rows in jTable?

Hi,

I need to take input from user using JTable. I want an empty row to appear after clicking a insert button. The values will be entered in this empty row. I have searched on this but could not figure out how to. I used DefaultTableModel but wasnt able to insert a row. Please help me.

Thanks and Regards, Somya

View Answers

June 30, 2011 at 3:09 PM

import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;

public class InsertRows{
    int i=0;
    public static void main(String[] args){
    new InsertRows();
  }
  public InsertRows(){

    JFrame frame = new JFrame("Inserting rows in the table!");
    JPanel panel = new JPanel();
    panel.setLayout(null);
    String data[][] = {{"Name","Address"}};
    String col[] = {"",""};
    final DefaultTableModel model = new DefaultTableModel(data,col);
    JTable table = new JTable(model);
    JScrollPane pane=new JScrollPane(table);
    JButton b=new JButton("Add Row");
    b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            i=i+1;
        model.insertRow(i,new Object[]{"",""});
        }
    });
    pane.setBounds(10,10,300,100);
    b.setBounds(10,120,100,20);
    panel.add(pane);
    panel.add(b);
    frame.add(panel);
    frame.setSize(500,200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Ads









Related Tutorials/Questions & Answers:
How to insert rows in jTable?
How to insert rows from Excel spreadsheet into database by browsing the excel file?
Advertisements
how to Insert Array Values Into Separate Rows using java?
insert rows from browsed file to sql database
insert rows from Excel sheet into a database by browsing the file
how to insert check box
Mysql Insert
How to insert data into MySQL Table?
JTables or JTextAreas
HOW TO DISPLAY 2 ROWS OF CONTROLS
How to insert clob data??
fetch and insert multiple rows into mysql database using jsp servlet
how to search the string arraylist contains database rows?
how to insert value in dynamic table
How to insert a car in the picture, insert a car in the picture, insert a car
how to insert excel file into mysql datbase in servlet
Insert Records in Table
how to insert one table to anothere table
how to insert values from jsp into ms access
how to select random rows from database through servlet
how to insert data from netbeans into databse
How to add dynamically rows into database ?Need help pls
how to insert data into database using jsp & retrive
how to insert checkbox value into database using jsp
how to insert, retrieve data from,to db(code)....
how to insert checkbox value into database using jsp
how to insert checkbox value into database using jsp
how to insert data in database using html+jsp
how to insert data into databasse by using jdbc
how to insert data into databasse by using jdbc
how to insert data into databasse by using jdbc

Ads