JTables
This actually a post from a previous forum, but I was informed it should be here. I'm having a problem, well two actually, with my table and I can't seem to figure it out. A user enters information in text fields and it is added to a table at runtime. The first problem is it displays the information correctly but the information is displayed inside square brackets. The second problem is when the user continues to enter information into the text fields, the new information goes into the table making a new row, but the previous row and the new row both display the new information. Any ideas? Thanks
P.S. I'm using Vectors, but I'm sure that was already known.
[682 byte] By [
java_tonya] at [2007-11-26 13:51:29]

# 2
HI
To solve ur prblem use tablemodel which will add new row runtime along with previous row here a little code example...will help u
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class SimpleTable extends JFrame implements ActionListener{
private JTextField name,address,phone;
private JLabel namelbl,addresslbl,phonelbl;
private JButton addbtn,insertbtn;
private JPanel mainPane, subPane,tbPane;
private JTable table;
private JScrollPane scPane;
private DefaultTableModel model;
private dbControlar dbctrl = new dbControlar();
public int rc ;
public SimpleTable(){
super("Table Frame...",true,true,true,true);
String[] colname = {"Name","Address","Phone"};
String[][] data = {{"","",""}};
int rows = 0;
name = new JTextField();
address = new JTextField();
phone = new JTextField();
namelbl = new JLabel("Name :");
addresslbl = new JLabel("Address :");
phonelbl = new JLabel("Phone :");
addbtn = new JButton("Add Row");
addbtn.addActionListener(this);
insertbtn = new JButton("Insert data");
insertbtn.addActionListener(this);
subPane = new JPanel(new GridLayout(4,2));
subPane.add(namelbl);
subPane.add(name);
subPane.add(addresslbl);
subPane.add(address);
subPane.add(phonelbl);
subPane.add(phone);
subPane.add(addbtn);
subPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//Table data here
model = new DefaultTableModel(colname,rows);
rc = model.getRowCount();
table = new JTable(model);
//table = new JTable(data,colname);
table.setPreferredScrollableViewportSize(new Dimension(500,70));
tbPane = new JPanel(new GridLayout(2,1));
scPane = new JScrollPane(table);
tbPane.add(scPane);
tbPane.add(insertbtn);
mainPane = new JPanel(new BorderLayout());
mainPane.add(subPane,BorderLayout.NORTH);
mainPane.add(tbPane,BorderLayout.CENTER);
add(mainPane);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
//if((name.getText() != "") && (address.getText() != "") && (phone.getText() != "")){
if(command.equals("Add Row")){
System.out.println("Row coutn is "+rc);
model.insertRow(rc, new Object[]{name.getText(),address.getText(),phone.getText()});
name.setText("");
address.setText("");
phone.setText("");
rc++;
}
}
}
hope this code will help u alot
null