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]
# 1

The quickest way to solving your problem would be for you to post a sample code of your problem. Without it, all we can do is to make wild guesses since your problem is not a general one.

And what do you mean by you are using Vectors? You should be using the TableModel to add your data to the JTable. Hence, you will be using the following methods from the TableModel interface, setValue( ), and addRow( ).

ICE

icewalker2ga at 2007-7-8 1:28:47 > top of Java-index,Desktop,Core GUI APIs...
# 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

zerocooola at 2007-7-8 1:28:47 > top of Java-index,Desktop,Core GUI APIs...
# 3

This is the particular section that adds to the Vectors. There were four Vectors used, one for each column(an account number, date, amount) and one to hold all four.

depositPanel.acctNumVector.add(acctNum);

depositPanel.dateVector.add(date);

depositPanel.amountVector.add(amount);

depositPanel.dtm.addRow(depositPanel.depositsVector);

java_tonya at 2007-7-8 1:28:47 > top of Java-index,Desktop,Core GUI APIs...
# 4
I managed to get this example working wonderfully. Obviously I was doing something wrong but still don't know what that was, lol. Thanks
java_tonya at 2007-7-8 1:28:47 > top of Java-index,Desktop,Core GUI APIs...
# 5
u ru using simpel table not the table model. that y
zerocooola at 2007-7-8 1:28:48 > top of Java-index,Desktop,Core GUI APIs...
# 6

> P.S. I'm using Vectors, but I'm sure that was already known.

No, nothing was known because you did not post your code showing the problem.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-8 1:28:48 > top of Java-index,Desktop,Core GUI APIs...