Java: Adding Row in JTable

Java: Adding Row in JTable

how about if we already have the JTAble created earlier.

And i just found nothing to get its DefaultTableModel, thus, I can't call insertRow() method.

Is there any work around for this?

I found that we need to re-create the table as a whole using AbstractModel, (what??)

(--__--)" uh oh....

View Answers

February 9, 2011 at 4:37 PM

Java add row in JTable

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

public class InsertRows{
  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);
    JButton b=new JButton("Add Row");
    b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        model.insertRow(1,new Object[]{"Roseindia","Delhi"});
        }
    });
    table.setBounds(10,10,300,100);
    b.setBounds(10,120,100,20);
    panel.add(table);
    panel.add(b);
    frame.add(panel);
    frame.setSize(500,200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

You can also visit the following link:

Inserting rows in JTable









Related Tutorials/Questions & Answers:
jtable insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
JTable duplicate values in row
JTable duplicate values in row  JTable duplicate values in row
Advertisements
jtable-adding a row dynamically
jtable-adding a row dynamically  hi..i am doing a project for pharmacy .. *pblm:* when i want to enter the details in jtable while running... to add one more row dynamically as a new row 4th if the details are large.but every
Java: Adding Row in JTable
Java: Adding Row in JTable   how about if we already have the JTAble created earlier. And i just found nothing to get its DefaultTableModel, thus, I can't call insertRow() method. Is there any work around for this? I found
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...  Hello Sir, I am developing a desktop application in which i have to display database records in jtable .now I want
Removing a Row from a JTable
Removing a Row from a JTable       After inserting the data in a JTable, if you wish... the JTable. For removing the data of row from JTable, you will remove it from
Remove JTable row that read txt file records
Remove JTable row that read txt file records  Hi every one. i have a jtable that correctly read data frome file and show them in own. I want to add a "Delete" button that when select a row and clicked button, row must deleted
how update JTable after adding a row into database
how update JTable after adding a row into database  J have two... in JTable, and it's OK, but after adding a row into database table does't update. How update JTable after adding a row into database? package djile pak.java
Enabling Row, Column and Cell Selections in a JTable
Enabling Row, Column and Cell Selections in a JTable... to describe how to enable the row, column and cell selections in a JTable... will see the enabling row, column and cell selections in a JTable. When you
JTable
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
JTable
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
JTable
JTable   how to select a definite cell which containing a similar text containg to the one which the user entering from a jtable at runtime in java
JTable
JTable  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
jtable
jtable  how to get the values from database into jtable and also add a checkbox into it and then when selected the checkbox it should again insert into database the selected chewckbox.plzz help
jtable
jtable  hi Sir i am working netbeans IDE,I have a jtable when i insert values in jtable then i am unable to print all inserted values,For eg if i insert 1,2,3,4,5,6,7,8 values then , i am getting output
jtable
jtable  hey i have build a form and i m also able to add data from database to jtable along with checkbox.the only problem is that if i select multiple checkboxes the data doesnt get inserted into new database and if only one
JTable
JTable  Hi I have problems in setting values to a cell in Jtable which is in a jFrame which implements TableModelListener which has a abstract method tableChanged(TableModelEvent e) . I'll be loading values from data base when
JTable
"}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane
JTable - Swing AWT
JTable row selection event   Hi, it will be great if someone can share an example of row selection event in JTable
jtable
jtable  i have build an application and i retrieve data from the database and store it in jtable.now i have to make a checkbox column in each row and also a select all option in header but i am not able to do so.i am getting
JTable - Java Beginners
JTable  can we merge two cells of one row in jtable
regarding jtable...
regarding jtable...  sir, im working with jtables. i wanted to populate a jtable from the database and when i click any row it should add a container below that row itself and show the required details of the row
java jtable
in which i have to display database records in jtable .now I want to read only the values of particular row at which the jtextfield has been set. and display that in jtable. plz help me with the code
restrict jtable editing
restrict jtable editing  How to restrict jtable from editing or JTable disable editing?   public class MyTableModel extends AbstractTableModel { public boolean isCellEditable(int row, int column
REPORT WITH JTABLE
(rs.next()) { Vector row = new Vector(columns); for (int i = 1; i <= columns; i++) { row.addElement( rs.getObject(i) ); } data.addElement( row ); } } catch(Exception e){} JTable table = new JTable(data, columnNames); JScrollPane scrollPane
Jbutton in JTable cells
Jbutton in JTable cells  I am a Java Beginner... I want a code in which I display data in Jtable from database and correspondingly a JButton... with that respective row's data details.. Please help me
How to insert rows in jTable?
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
JTable - JDBC
JTable   Hello..... I have Jtable with four rows and columns and i have also entered some records in MsSql database. i want to increase Jtable's... { JFrame f; JPanel p; JLabel l; JTextField tf; JButton btn; JTable tb
jtable problem
jtable problem  how to make a cell text hypertext
JTable - Swing AWT
JTable  Hi Deepak, i want to display the Jtable data... how could i show jtable data on the console. Thanks, Prashant   Hi...*; public class JTableToConsole extends JFrame { public Object GetData(JTable
JTABLE OF JAVA
JTABLE OF JAVA  i have a jtable in java,i have used checkbox in jtable. now i want to add(submit) only those records that i have checked by checkbox how? i want small example with coding
Jtable-Java
Jtable-Java  Hi all,I have a Jtable And i need to clear the data in the table .I only Need to remove the data in the table.not the rows.Please help me
JTable populate with resultset.
JTable populate with resultset.  How to diplay data of resultset using JTable?   JTable is component of java swing toolkit. JTable class is helpful in displaying data in tabular format. You can also edit data. JTable
Swings JTable
Swings JTable  add values to JTable with four coloums,two of them are comboboxes
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
JTABLE Issue
JTABLE Issue  Hi Eveyone, I am developing a small application on Swing-AWT. I have used JTABLE to show data. There is "input field" and "search... on basis of input data provided in input field. For JTABLE is on some other
JTable - Swing AWT
JTable  Hi Deepak, i m facing a problem with jtable. i am able to display the values from the database into the jtable. but not able to modifying multiple cell values in a row. also i want to store those modified
jtable query
jtable query  I need a syntax...where i could fetch the whole data from the database once i click the cell in jtable...and that must be displayed in the nearby text field which i have set in the same frame
JTable Pagination
JTable Pagination  Hello , I have the following Code. I am able... renderer, int row, int column) { Component comp = super.prepareRenderer(renderer, row, column); comp.setBackground(row % 2 == 0
ABOUT Jtable
ABOUT Jtable  My Project is Exsice Management in java swing Desktop Application. I M Use Netbeans & Mysql . How can retrive Data in Jtable from Mysql Database in Net Beans
row_id
row_id  sir i have created a table my_table with two rows n two column with smillar name in each cell without any primary key , how can i change the first row and third column's name in sql server 2005 , kindly send me the query
row_id
row_id  sir i have created a table my_table with two rows n two column with smillar name in each cell without any primary key , how can i change the first row and third column's name in sql server 2005 , kindly send me the query
How to add JTable in JPanel
How to add JTable in JPanel  How to add JTable in JPanel
delete row
delete row  how to delete row using checkbox and button in php...("sourabh", $link); $rows=mysql_query("select * from sonu"); $row=mysql...;/tr> <?php while($row=mysql_fetch_array($rows)) { ?> <tr>
problem with JTable - Swing AWT
to swings.i was having an assignment like i need to create a JTable... an action event such that if i check the checkbox outside the JTable,all the checkboxes inside the JTable should be checked. details: for creating JTable
java swing (jtable)
java swing (jtable)  hii..how to get values of a particular record in jtable from ms access database using java swing in netbeans..?? please help... from database and display it in jtable. import java.awt.*; import java.sql.
update a JTable - Java Beginners
update a JTable   how to update a JTable with Mysql data through user... in an updatable JTable You would create the table as follows: conn... = new ResultSetTableModel(rs); table = new JTable(model
Jtable Question - Java Beginners
Jtable Question  Hello Sir, I have Created Database in MS access 2007 ,I want show the table data in to Swing JTable, How I can Show it, plz Help...()) { Vector row = new Vector(columns); for (int i = 1; i <= columns; i
jtable in java swing
jtable in java swing  hai friends... i am creating 1 GUI having 1 table, that table contain 3 fields(columns) and 10 rows.. user can enter values... should reach to 2nd row 1st column. user can not able to edit the values present

Ads