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
JTables or JTextAreas
How to sort the rows in SQL?
insert rows from Excel sheet into a database by browsing the file
how to insert image into server
how to insert check box
Mysql Insert
HOW TO DISPLAY 2 ROWS OF CONTROLS
fetch and insert multiple rows into mysql database using jsp servlet
How to insert data into MySQL Table?
Inserting Rows in a JTable
how to search the string arraylist contains database rows?
How to insert clob data??
how to insert a summary values in grid
How to insert a car in the picture, insert a car in the picture, insert a car
how to insert value in dynamic table
How can we find the number of rows in a result set using PHP?
how to select random rows from database through servlet
Insert Records in Table
how to insert excel file into mysql datbase in servlet
how to insert values from jsp into ms access
how to insert one table to anothere table
how to insert data from netbeans into databse
how to mark different colors for diffent rows - Java Beginners
How to add dynamically rows into database ?Need help pls
how to insert the physical path of an image in database - JDBC
how to insert date&time coloumn value in mssql - JDBC
how to insert the selected item of combobox in mysql - XML

Ads