help with Java Table

Hi

Does anyone have a code sample for JTable, i need to have following functions

1, This table has only 1 column

2, When user double clicks on row, he must be able to edit, with maximum length of 60 characters

3, A button which the user can click to say add row, this will add a emply row and user can double click to edit it

4, When user clicks on apply button, read all the rows and get the data from there and update a database

Any help would be appreciated

Ashish

[517 byte] By [kulkarni_asha] at [2007-11-26 17:09:06]
# 1
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Djaunla at 2007-7-8 23:36:55 > top of Java-index,Java Essentials,Java Programming...
# 2

1. Easy to do, just check the api and tutorial.

2. Read the tutorial to see how to set editors for cells

3. The button won't be part of your JTable, it will have to be a separate button with an action listener that works on the table. If you want, add the table and button to a panel to make adding the whole thing to other components easier.

4. Again, the apply button will be separate from the JTable. Just create the apply button wherever you want, and have it's action listener work on the table's data.

hunter9000a at 2007-7-8 23:36:55 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi

I have this code, but after i add new row, i am not able to edit the new row, any suggestion to get it working

import java.util.*;

import javax.swing.*;

import javax.swing.table.*;

import java.awt.*;

import java.awt.event.*;

public class TestAddRowInTable extends JFrame

{

private JTable table;

private MyModel model;

private Vector allData;

public TestAddRowInTable()

{

super("my Table");

buildData();

Container cont = this.getContentPane();

cont.setLayout(new BorderLayout());

cont.add(getTablePane(), BorderLayout.NORTH);

cont.add(getButtonPanel(), BorderLayout.SOUTH);

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

this.setSize(550,400);

this.show();

}

private JScrollPane getTablePane()

{

table = getTable();

JScrollPane scrollPane = new JScrollPane(table);

table.setPreferredScrollableViewportSize(new Dimension(550, 310));

scrollPane

.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

scrollPane

.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

return scrollPane;

}

private JTable getTable()

{

Vector header = new Vector();

header.add("Notes");

model = new MyModel(allData, header);

JTable table = new JTable(model);

table.getTableHeader().setReorderingAllowed(false);

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

for (int i = 0; i < model.getColumnCount(); i++)

{

TableColumn column1 = table.getColumnModel().getColumn(i);

switch (i)

{

case 0:

column1.setPreferredWidth(530);

column1.setCellEditor(new JTextFieldEditor(new JTextField(60)));

continue;

}

}

return table;

}

private JPanel getButtonPanel()

{

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

buttonPanel.setPreferredSize(new Dimension(550, 30));

JButton addButton = new JButton("Add Row");

JButton changeButton = new JButton("Apply");

addButton.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

model.addRow(new Vector());

}

}

);

buttonPanel.add(addButton);

buttonPanel.add(changeButton);

return buttonPanel;

}

class JTextFieldEditor extends DefaultCellEditor implements ItemListener

{

JComponent component;

int row, column;

public JTextFieldEditor(JTextField textField)

{

super(textField);

component = textField;

}

public Component getTableCellEditorComponent(JTable table,

Object value, boolean isSelected, int row, int column)

{

this.row = row;

this.column = column;

((JTextField) component).setText(value.toString());

return component;

}

public Object getCellEditorValue()

{

Vector data1 = (Vector) allData.get(row);

data1.set(column, ((JTextField) component).getText());

return ((JTextField) component).getText();

}

public void itemStateChanged(ItemEvent e)

{

super.fireEditingStopped();

}

}

class MyModel extends DefaultTableModel

{

public MyModel(Vector allData, Vector header)

{

super(allData, header);

}

public Object getValueAt(int row, int col)

{

// if (row==0)

//return prop_names[row];

return super.getValueAt(row,col);

}

public boolean isCellEditable(int row, int col)

{

// if (col==0)

// {

///return false;

// }

return true;

}

};

private void buildData()

{

allData = new Vector();

Vector data = new Vector();

data.add("This is note one");

allData.add(data);

}

public static void main(String args[])

{

new TestAddRowInTable();

}

}

kulkarni_asha at 2007-7-8 23:36:55 > top of Java-index,Java Essentials,Java Programming...
# 4

In the future, Swing releated questions should be posted in the Swing forum.

> i am not able to edit the new row, any suggestion to get it working

Maybe getting rid of the NullPointerException would help.

You are adding a Vector without any data. See how you added data initially. You added a text string to the Vector.

> column1.setCellEditor(new JTextFieldEditor(new JTextField(60)));

This will not limit the number of characters to 60. Read the JTextField API. The 60 is only used to determined the preferred size of the text field when it is added directly to the GUI. If you read the API you will find a link to the Swing tutorial that will show you how to limit the number of characters added to a Document of a text component.

camickra at 2007-7-8 23:36:55 > top of Java-index,Java Essentials,Java Programming...