I want to delete all the rows of DefaultTable. I found two common ways to delete them on internet, but none of them works in my case because those methods does not exist in my DefaultTableModel. I wonder why. My code for using DefaultTableModel is
DefaultTableModel Table = (DefaultTableModel) Table.getModel();
One way to delete is
Table.removeRow(Table.getRowCount() - 1);
but this removerow method does not exist in my DefaultTableModel.
You can set the row count to 0.
setRowCount(0)
Quote from documentation:
public void setRowCount(int rowCount)
Sets the number of rows in the model. If the new size is greater than
the current size, new rows are added to the end of the model If the
new size is less than the current size, all rows at index rowCount and
greater are discarded.
But as you can't find removeRow either I suspect you haven't typed you model variable as DefaultTableModel perhaps, maybe just TableModel?
In that case cast your TableModel to DefaultTableModel like this:
DefaultTableModel model = (DefaultTableModel) table.getModel();
Why complicating simple things, but removes must be iterative,
if (myTableModel.getRowCount() > 0) {
for (int i = myTableModel.getRowCount() - 1; i > -1; i--) {
myTableModel.removeRow(i);
}
}
Code example
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.*;
public class RemoveAddRows extends JFrame {
private static final long serialVersionUID = 1L;
private Object[] columnNames = {"Type", "Company", "Shares", "Price"};
private Object[][] data = {
{"Buy", "IBM", new Integer(1000), new Double(80.50)},
{"Sell", "MicroSoft", new Integer(2000), new Double(6.25)},
{"Sell", "Apple", new Integer(3000), new Double(7.35)},
{"Buy", "Nortel", new Integer(4000), new Double(20.00)}
};
private JTable table;
private DefaultTableModel model;
public RemoveAddRows() {
model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table = new JTable(model) {
private static final long serialVersionUID = 1L;
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
int firstRow = 0;
int lastRow = table.getRowCount() - 1;
int width = 0;
if (row == lastRow) {
((JComponent) c).setBackground(Color.red);
} else if (row == firstRow) {
((JComponent) c).setBackground(Color.blue);
} else {
((JComponent) c).setBackground(table.getBackground());
}
/*if (!isRowSelected(row)) {
String type = (String) getModel().getValueAt(row, 0);
c.setBackground("Buy".equals(type) ? Color.GREEN : Color.YELLOW);
}
if (isRowSelected(row) && isColumnSelected(column)) {
((JComponent) c).setBorder(new LineBorder(Color.red));
}*/
return c;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
JButton button1 = new JButton("Remove all rows");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (model.getRowCount() > 0) {
for (int i = model.getRowCount() - 1; i > -1; i--) {
model.removeRow(i);
}
}
System.out.println("model.getRowCount() --->" + model.getRowCount());
}
});
JButton button2 = new JButton("Add new rows");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Object[] data0 = {"Buy", "IBM", new Integer(1000), new Double(80.50)};
model.addRow(data0);
Object[] data1 = {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)};
model.addRow(data1);
Object[] data2 = {"Sell", "Apple", new Integer(3000), new Double(7.35)};
model.addRow(data2);
Object[] data3 = {"Buy", "Nortel", new Integer(4000), new Double(20.00)};
model.addRow(data3);
System.out.println("model.getRowCount() --->" + model.getRowCount());
}
});
JPanel southPanel = new JPanel();
southPanel.add(button1);
southPanel.add(button2);
add(southPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
RemoveAddRows frame = new RemoveAddRows();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Have you tried this
This works for me..
defaultTableModel.setRowCount(0);
Why don't you read the javadoc of DefaultTableModel?
public void removeRow(int row)
Removes the row at row from the model. Notification of the row being
removed will be sent to all the listeners.
public void setDataVector(Vector dataVector,
Vector columnIdentifiers)
Replaces the current dataVector instance variable with the new
Vector of rows, dataVector.
public void setRowCount(int rowCount)
Sets the number of rows in the model. If the new size is greater than
the current size, new rows are added to the end of the model If the
new size is less than the current size, all rows at index rowCount and
greater are discarded.
Simply keep removing the table model's first row until there are no more rows left.
// clean table
DefaultTableModel myTableModel = (DefaultTableModel) this.myjTable.getModel();
while (myTableModel.getRowCount() > 0) {
myTableModel.removeRow(0);
}
Ypu can write a method
public void clearTable()
{
getTableModel().setRowCount(0);
}
then call this method from the place that you need to clear the table
Related
I wanted to set my 2nd column as JComboBox editor using TableModelListener. Also wanted to change the Model of ComboBox in 2nd column based on selected ComboBox in 1st column. Here I implemented a Listener that's listen to 1st column.
private class TableScheduleListener implements TableModelListener
{
//Listening for data changes.
#Override
public void tableChanged(TableModelEvent e)
{
int row = e.getFirstRow();
int column = e.getColumn();
}
if(column == 1)
{
for(int i = 0; i < createScheduleView.tblSchedule.getRowCount(); i++)
{
createScheduleView.tblSchedule.getCellEditor(i, 2);
}
int col = createScheduleView.tblSchedule.convertColumnIndexToModel(2);
if(col == 2 && createScheduleView.tblSchedule.getRowCount() < 7)
{
DefaultComboBoxModel cbModel = new DefaultComboBoxModel(createScheduleModel.setData().toArray());
createScheduleView.cbBreakStartTime.setModel(cbModel);
}
}
}
But I'm thinking how can I set as JComboBox as editor in my 2nd column. After settings it's model? Any help would appreciated.
UPDATE
I followed camickr tips from https://tips4java.wordpress.com/2009/06/28/combo-box-table-editor/
But wanted to override TableCellEditor from different class.
private class TableScheduleEditor extends JTable
{
// Determine editor to be used by row
#Override
public TableCellEditor getCellEditor(int row, int column)
{
int modelColumn = convertColumnIndexToModel(column);
if(modelColumn == 2 && row < 7)
{
DefaultComboBoxModel model = new DefaultComboBoxModel(createScheduleModel.setData().toArray());
createScheduleView.getCbBreakStartTime().setModel(model);
return new DefaultCellEditor(createScheduleView.getCbBreakStartTime());
}
else
return super.getCellEditor(row, column);
};
}
I'm thinking how this class can register in my JTable?
I tried to register this in my constructor this.createScheduleView.tblSchedule.setCellEditor(new TableScheduleEditor());
But it says cannot be converted to TableCellEditor because it extends JTable. Any trick to do this?
Also wanted to change the Model of ComboBox in 2nd column based on selected ComboBox in 1st column
Override the getCellEditor(...) method of the JTable to return the appropriate editor.
Here is a basic example to get you started:
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableComboBoxByRow extends JPanel
{
List<String[]> editorData = new ArrayList<String[]>(3);
public TableComboBoxByRow()
{
setLayout( new BorderLayout() );
// Create the editorData to be used for each row
editorData.add( new String[]{ "Red", "Blue", "Green" } );
editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
editorData.add( new String[]{ "Apple", "Orange", "Banana" } );
// Create the table with default data
Object[][] data =
{
{"Color", "Red"},
{"Shape", "Square"},
{"Fruit", "Banana"},
{"Plain", "Text"}
};
String[] columnNames = {"Type","Value"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model)
{
// Determine editor to be used by row
public TableCellEditor getCellEditor(int row, int column)
{
int modelColumn = convertColumnIndexToModel( column );
if (modelColumn == 1 && row < 3)
{
JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
return new DefaultCellEditor( comboBox1 );
}
else
return super.getCellEditor(row, column);
}
};
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Table Combo Box by Row");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TableComboBoxByRow() );
frame.setSize(200, 200);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
In the above example the "model" changes based on the row being edited.
In your case you will need to modify the logic to return the editor based on the value in the first column.
I'm working on jTable and I intend on deleting specific rows as part of the data manipulation to be conducted on the table. Essentially I've been able to successfully delete a row which the user would specify but I what I really want to do is to delete several rows based on boolean states or check boxes that are selected as part one of the four columns of the table.
I've attached a screenshot as to the current result of running the code and would like to be able to delete the rows based on the selected boolean states or check boxes.
Below is my code including my table model code which extends the AbstractTableModel:
package com.TableRowSelectProgramatically;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
#SuppressWarnings("serial")
public class JTableRowSelectProgramatically extends JPanel {
public MyTableModel MyTableModel;
public String Cell1 = "ABCD";
public JTableRowSelectProgramatically() {
initializePanel();
}
private void initializePanel() {
setLayout(null);
setPreferredSize(new Dimension(1250, 700));
// Table model
MyTableModel = new MyTableModel();
// Table
final JTable table = new JTable(MyTableModel);
table.setFillsViewportHeight(true);
// Row data
Object[] values = {Cell1, "EFGH", "IJKL", new Boolean(false)};
Object[] values2 = {"UVWX","QRST","MNOP", new Boolean(false)};
Object[] values3 = {"ABCD","YZAB","CDEF", new Boolean(false)};
final Object[] values4 = {"QWERTY","YTREWQ","QWERTY", new Boolean(false)};
// Insert row data
MyTableModel CustomTableModel = (MyTableModel) table.getModel();
CustomTableModel.insertData(values);
CustomTableModel.insertData(values2);
CustomTableModel.insertData(values3);
CustomTableModel.insertData(values);
CustomTableModel.insertData(values2);
CustomTableModel.insertData(values3);
CustomTableModel.insertData(values);
CustomTableModel.insertData(values2);
CustomTableModel.insertData(values3);
// Create edit menu label
JLabel labelEditMenu = new JLabel("EDIT MENU:\n");
// Create add row btn
JButton addRow = new JButton("Add Row");
// Attach listener for add row btn
addRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MyTableModel.insertData(values4);
}
});
// Create delete row btn
JButton deleteRow = new JButton("Delete Row");
// Attach listener for delete btn
deleteRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MyTableModel.removeRow(1);
}
});
// Create scroll pane
JScrollPane pane = new JScrollPane(table);
pane.setBounds(30, 30, 500, 500);
//
JTextField AgentIDTextField = new JTextField();
// Populate the JPanel
JPanel dataEntryPanel = new JPanel(new FlowLayout());
//dataEntryPanel.setBackground(Color.orange);
dataEntryPanel.setBounds(540, 30, 500, 50);
//dataEntryPanel.add(AgentIDTextField);
dataEntryPanel.add(labelEditMenu);
dataEntryPanel.add(addRow);
dataEntryPanel.add(deleteRow);
// Join up GUI
add(pane);
add(dataEntryPanel);
}
// Enable visibity of frame
public static void showFrame() {
JPanel panel = new JTableRowSelectProgramatically();
JFrame frame = new JFrame("Test table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
// Launch prog
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JTableRowSelectProgramatically.showFrame();
}
});
}
// Create custom table model for data entry
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"COLUMN 0", "COLUMN 1", "COLUMN 2", "COLUMN 3"};
private Vector data = new Vector();
#Override
public int getRowCount() {
return data.size();
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#SuppressWarnings("rawtypes")
#Override
public Object getValueAt(int row, int col) {
return ((Vector) data.get(row)).get(col);
}
public String getColumnName(int col){
return columnNames[col];
}
public Class getColumnClass(int c){
return getValueAt(0,c).getClass();
}
public void setValueAt(Object value, int row, int col){
((Vector) data.get(row)).setElementAt(value, col);
fireTableCellUpdated(row,col);
}
public boolean isCellEditable(int row, int col){
if (3 == col){
return true;
}
else {
return false;
}
}
public void insertData(Object[] values){
data.add(new Vector());
for(int i =0; i<values.length; i++){
System.out.println("data.size is: " + data.size());
((Vector) data.get(data.size()-1)).add(values[i]);
}
fireTableDataChanged();
}
public void removeRow(int row){
data.removeElementAt(row);
fireTableDataChanged();
}
}
}
New attempt at deleting rows of a JTable:
public void deleteRow() {
for (int i = 0; i < getRowCount(); i++) {
Object columnState = getValueAt(i, 3);
System.out.println("STEP 6 - In row " + i + " boolean value is: " + columnState);
boolean columnStateAsBoolean = (Boolean) columnState;
System.out.println("STEP 6 - In row " + i + " Column State As Boolean is: " + columnStateAsBoolean);
if(columnStateAsBoolean == true) {
removeRow(i);
}
System.out.println("-------------------------------------");
}
}
I really want to do is to delete several rows based on boolean states or check boxes
create a loop that starts on the last row and counts down to 0.
Then for every row you use the table.getValueAt(...) method to get the Boolean value of the column.
If the value is true then delete the row.
//Try something like this
int rowCount=table.getSelectedRowCount();//get selected row's count
int row;
if(rowCount>0)
{
while((row=table.getSelectedRow())!=-1)
(DefaultTableModel)table.getModel().removeRow(table.convertRowIndexToModel(row));
}
For initialization:
Vector<Vector> rowdata = new Vector<Vector>();
Vector columnname = new Vector();
JTabel result = new JTable(rowdata, columnname);
JScrollPane sqlresult = new JScrollPane(result);
In the later part, I assign values in rowdata and columnname vector. How to show new values in JTable on GUI?
In addition..
result.repaint();
..and..
((DefaultTableModel)result.getModel()).fireTabelDataChanged()
..seem to do nothing.
1) use DefaultTableModel for JTable
2) define Column Class
3) add row(s) DefaultTableModel.addRow(myVector);
for example
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class DefaultTableModelDemo {
public static final String[] COLUMN_NAMES = {
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
};
private DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0);
private JTable table = new JTable(model);
private JPanel mainPanel = new JPanel(new BorderLayout());
private Random random = new Random();
public DefaultTableModelDemo() {
JButton addDataButton = new JButton("Add Data");
JPanel buttonPanel = new JPanel();
buttonPanel.add(addDataButton);
addDataButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
addDataActionPerformed();
}
});
model = new DefaultTableModel(COLUMN_NAMES, 0) {
private static final long serialVersionUID = 1L;
#Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table = new JTable(model) {
private static final long serialVersionUID = 1L;
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (isRowSelected(row) && isColumnSelected(column)) {
((JComponent) c).setBorder(new LineBorder(Color.red));
}
return c;
}
};
mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
}
private void addDataActionPerformed() {
for (int i = 0; i < 5; i++) {
Object[] row = new Object[COLUMN_NAMES.length];
for (int j = 0; j < row.length; j++) {
row[j] = random.nextInt(5);
}
model.addRow(row);
}
}
public JComponent getComponent() {
return mainPanel;
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("DefaultTableModelDemo");
frame.getContentPane().add(new DefaultTableModelDemo().getComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
rowdata and columnname are used for the initialization of JTable, after the component JTable exists you need to use JTable.getColumnModel() and JTable.getModel() to add columns or rows.
Or JTable.addColumn(TableColumn aColumn) if you only wants to add a column.
Look here if you want to know how to add a row following an example.
I am trying to use a JTable with the first column predefined. The user enters data into the 2nd column only (Quantity). Then I calculate the final income by multiplying the Service and Quantity columns and display it in the third column, Income.
|Service | Quantity | Income
|$40.00 | X |
|$40.00 | 3 | 120
Here user inputs "3" because she did "3" of service X today at $40 each. The user can only update the Quantity column. The Income column will be calculated by the system.
What type of listener should I use? I was using a TableModelListener but when I want to update Income to 120 by calling setValue = $120 it fires off a TableListenerEvent and hence an infinite loop.
Should I use an ActionEvent, a ColumnListener or something else?
Also, I want the "focus" to increment down the rows, always staying on the second column (the column the user edits).
for Listning changes into TableCell you have to implements TableModelListener for example
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableProcessing extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;
private String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
private Object[][] data = {
{"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
{"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
{"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
{"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}};
private TableModelListener tableModelListener;
public TableProcessing() {
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model) {
private static final long serialVersionUID = 1L;
#Override// Returning the Class of each column will allow different renderers
public Class getColumnClass(int column) { // to be used based on Class
return getValueAt(0, column).getClass();
}
#Override // The Cost is not editable
public boolean isCellEditable(int row, int column) {
int modelColumn = convertColumnIndexToModel(column);
return (modelColumn == 3) ? false : true;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
//http://stackoverflow.com/questions/7188179/jtable-focus-query/7193023#7193023
table.setCellSelectionEnabled(true);
KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
InputMap map = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
map.put(tab, "selectNextRowCell");
//http://stackoverflow.com/questions/7188179/jtable-focus-query/7193023#7193023
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
setTableModelListener();
}
private void setTableModelListener() {
tableModelListener = new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE) {
System.out.println("Cell " + e.getFirstRow() + ", "
+ e.getColumn() + " changed. The new value: "
+ table.getModel().getValueAt(e.getFirstRow(),
e.getColumn()));
int row = e.getFirstRow();
int column = e.getColumn();
if (column == 1 || column == 2) {
TableModel model = table.getModel();
int quantity = ((Integer) model.getValueAt(row, 1)).intValue();
double price = ((Double) model.getValueAt(row, 2)).doubleValue();
Double value = new Double(quantity * price);
model.setValueAt(value, row, 3);
}
}
}
};
table.getModel().addTableModelListener(tableModelListener);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TableProcessing frame = new TableProcessing();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
I want to change default data in JTable at runtime. I am using netbeans. I tried solution given here adding data to JTable when working with netbeans
jTable1.getModel().setValueAt(row, column, value);
but it gives me this error:
If you want to change it at runtime you need to decide when you want it changed and add the code to the proper method/event. As it stands you have a method call in your class definition, which is not valid code.
For instance
public void setTableModel(){
displayTable.getModel().setValueAt(2,4,300);
}
And then call setTableModel() at an appropriate time.
because you wrote this code line out of current Class, you have to wrap these code line(s)
inside Class/void/constructor, for example
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableProcessing extends JFrame implements TableModelListener {
private static final long serialVersionUID = 1L;
private JTable table;
public TableProcessing() {
String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
Object[][] data = {
{"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
{"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
{"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
{"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
model.addTableModelListener(this);
table = new JTable(model) {
private static final long serialVersionUID = 1L;
#Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
#Override
public boolean isCellEditable(int row, int column) {
int modelColumn = convertColumnIndexToModel(column);
return (modelColumn == 3) ? false : true;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
#Override
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE) {
int row = e.getFirstRow();
int column = e.getColumn();
System.out.println(row + " : " + column);
if (column == 1 || column == 2) {
int quantity = ((Integer) table.getModel().getValueAt(row, 1)).intValue();
double price = ((Double) table.getModel().getValueAt(row, 2)).doubleValue();
Double value = new Double(quantity * price);
table.getModel().setValueAt(value, row, 3);
}
}
}
public static void main(String[] args) {
TableProcessing frame = new TableProcessing();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}