Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners How to insert rows in jTable?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


somya gupta
How to insert rows in jTable?
1 Answer(s)      3 years and 6 months ago
Posted in : Java Beginners


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


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


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com