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