Preventing columns from moving on delete in JTable - java

I'm trying to implement a checkbox interface which allows a user to show/hide columns in a JTable, but when I remove the column, the column seems to move position and there is no way of knowing 100% where the columns are. The table is built with this code:
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
String[] columnNames = {"Artist","Track","Album","Genre","Year","Filetype"};
for (String column : columnNames) {
model.addColumn(column);
}
table.createDefaultColumnsFromModel();
table.getTableHeader().setReorderingAllowed(false);
Here's the code I have for implementing the checkbox listeners:
if (e.getSource() == artist) {
if (!artist.isSelected()) {
table.removeColumn(table.getColumnModel().getColumn(0));
} else {
table.addColumn(table.getColumnModel().getColumn(0));
}
}
if (e.getSource() == trackName) {
if (!trackName.isSelected()) {
table.removeColumn(table.getColumnModel().getColumn(1));
} else {
table.addColumn(table.getColumnModel().getColumn(1));
}
}
/* etc */

You can to use the Table Column Manager.
It will manager the hiding/showing of the table columns for you.

Related

Java Swing: JTable tableChanged does not work after model is updated

On load, my JTable has 2 columns - . So its a string in the first column and a checkbox in the second column. When I click on the checkbox tableChanged is fired and I can print the row data that was selected.
I need to change the table data when user selects a new category in the dropdown.
When the table data is updated, and I click on the checkbox the tableChanged is no longer fired.
This is what I have:
This is how I am updating the table data:
comboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String t = (String) comboBox.getSelectedItem();
if (t.equals("survey2")) {
String[] columnNames = { "Volume Name", "Select" };
Object[][] data = { { "pt1", false }, { "pt2", false },
{ "pt3", false }, { "pt4", false },
};
model = new DefaultTableModel(data, columnNames);
table.setModel(model);
}
}
});
This is my tableChanged:
table.getModel().addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
if ((Boolean)table.getModel().getValueAt(table.getSelectedRow(), 1)) {
System.out.println(">\t"
+ table.getValueAt(table.getSelectedRow(), 0));
} else {
System.out.println(">\t"
+ table.getValueAt(table.getSelectedRow(), 0));
}
}
});
I do not understand why the event is not fired after updating the model. Am I updating the table incorrectly?
You area creating a new TableModel but you added the ChangeListener to the old TableModel.
Don't create a new TableModel!
You can clear the data by using setRowCount(0).
The you can add the new data back to the DefaultTableModel by using:
the setDataVector(...) method, or
by adding data back to the model one row at a time using the addRow(...) method.
So there is no need to create a new TableModel. If you want to create a new TableModel then you also need to add your ChangeListener to this new model.

Java JComboBox icon

Displaying data in a JTable. One column serves as a field Checkbox. The problem is that instead of the icon appears in the display ChceckBox true / false. How do I fix this?
Add data:
private DefaultTableModel headermodel = new DefaultTableModel();
private JScrollPane scrollHeader = new JScrollPane();
private JTable headerTable = new JTable();
public void loadHead(){
header = model.getHead();
int ids=0;
int id=1;
for(String head: header) {
headermodel.addRow(new Object[]{id,head});
headerMap.put(ids,head);
id++;
ids++;
count++;
}
header.clear();
}
and display data in JTable:
headerTable = new JTable(headermodel);
headermodel.addColumn("Lp.");
headermodel.addColumn("Column Name");
headermodel.addColumn("Constraint");
headermodel.addColumn("Sum");
scrollHeader = new JScrollPane(headerTable);
TableColumnModel tcm = headerTable.getColumnModel();
tcm.getColumn(2).setCellEditor(new DefaultCellEditor(new JCheckBox()));
tcm.getColumn(3).setCellEditor(new DefaultCellEditor(new JCheckBox()));
tcm.getColumn(3).setCellRenderer(headerTable.getDefaultRenderer(boolean.class));
add(scrollHeader);
The model's getColumnClass(int columnIndex) method should return Boolean.class for the appropriate column index so that the renderer knows to render a check box for that column. For example,...
DefaultTableModel headermodel = new DefaultTableModel(){
#Override
public Class<?> getColumnClass(int columnNumber) {
if (columnNumber == 2 || columnNumber == 3) {
return Boolean.class;
} else {
return super.getColumnClass(columnNumber);
}
}
}
You shouldn't have to set the cell renderer for these columns for this since the default cell renderer will handle Boolean.class appropriately.

java - JTable setValueAt not working

I'm doing a chess game and I need to make a log table that prints every move. The LogTable class is like this:
public class LogTable {
private DefaultTableModel model;
private JTable table;
public LogTable(JPanel panel){
String[] columnNames = {"Move No.",
"White",
"Black"};
model = new DefaultTableModel(columnNames, 0);
table = new JTable();
//model.isCellEditable(i, i1)
table.setModel(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
}
public void newMove(chessPiece piece){
if (piece.getColor() == 0){
Object[] newRow = new Object[3];
newRow[0] = model.getRowCount()+1;
newRow[1] = piece.sayPos();
newRow[2] = " ";
model.addRow(newRow);
}
else {
model.setValueAt(piece.sayPos(), model.getRowCount(), model.getColumnCount());
}
}
}
But in the first black move it thows an ArrayOutOfBoundsException. The newMove function is called at the chessPiece class:
public void move(int newX, int newY, JPanelSquare jPanelSquareGrid[][], LogTable logTable){
resetShowValidMoves(jPanelSquareGrid);
logTable.newMove(this);
}
The rest of the move code is in each piece, which call super. I'm using the DefaultTableModel.
From Java API :
public DefaultTableModel(Object[] columnNames,int rowCount)
Constructs a DefaultTableModel with as many columns as there are elements in
columnNames and rowCount of null object values. Each column's name
will be taken from the columnNames array.
You instantiate the DefaultTableModel with 0 rows. So you can't set the value of an item in row 0 since it doesn't exist.

Remove JTable row that read file records

I am New in java, I have a JTable that can read records from a txt file and show they perfectly.
I want to add a new book to my JFrame that when user select a row on table and clicked the "delete" button, that row should delete and that deleted row records must delete from txt file,too.
my code is this, but it has errors and not seen JTable! :
public class CopyOfAllUserTable extends AbstractTableModel {
Vector data;
Vector column;
public static void main(String[] args){
new CopyOfAllUserTable();
}
public CopyOfAllUserTable() {
String line;
data = new Vector();
column = new Vector();
try {
FileInputStream fis = new FileInputStream("D:\\AllUserRecords.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
while (st1.hasMoreTokens())
column.addElement(st1.nextToken());
while ((line = br.readLine()) != null) {
StringTokenizer st2 = new StringTokenizer(line, " ");
while (st2.hasMoreTokens())
data.addElement(st2.nextToken());
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
final JFrame frame1=new JFrame();
JTable table=new JTable(data,column);
JButton button1=new JButton("Delete");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel model=new DefaultTableModel(data, column);
JTable table=new JTable(model);
}
});
JPanel panel=new JPanel();
panel.add(table);
panel.add(button1);
frame1.add(panel);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setBounds(200, 80, 600, 500);
frame1.setVisible(true);
frame1.setEnabled(true);
}
public int getRowCount() {
return data.size() / getColumnCount();
}
public int getColumnCount() {
return column.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (String) data.elementAt((rowIndex * getColumnCount())
+ columnIndex);
}
}
My problem is in delete row, and read records from file to jtable are perfectly successful.
Firstly you're not adding your JTable to the content of the frame.
For containers like: frame.getContentPane() and JPanel you should add the child components by using their #add(...) method.
For example:
final JPanel panel=new JPanel(new BorderLayout());
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel model=new DefaultTableModel(data, column);
JTable table=new JTable(model);
panel.add(new JScrollPane(table));
panel.revalidate();
}
});
Note that JPanel default layout is FlowLayout. Second thing - if you want to have headers and scrolling in your JTable you need to wrap it with JScrollPane.
Next - you should revalidate the panel after adding/removing/etc.
The second issue is removing rows from JTable. I usually write a method to handle it:
protected void removeRows(final int[] rows) {
int modelRows[] = new int[rows.length];
for(int i = 0; i < rows.length; ++i) {
modelRows[i] = table.convertRowIndexToModel(rows[i]);
}
Arrays.sort(modelRows);
for(int i = modelRows.length - 1; i >= 0; --i) {
int row = modelRows[i];
model.removeRow(row);
}
model.fireTableDataChanged();
}
The convertRowIndexToModel method converts index returned by JTable#getSelectedRows() or JTable#getSelectedRow() (which are the visible indices) to the model indices. If you set RowSorter for your JTable or you leave it to standard implementation:
table.setAutoCreateRowSorter(true);
You are adding table directly to the panel with out using the JScrollPane. Your table header will not be visible if you do like this,
So instead of this,
JPanel panel=new JPanel();
panel.add(table);
Do this,
JPanel panel=new JPanel();
panel.add(new JScrollPane(table));
Why to use JScrollPane? Read this.
When user selects a row and clicks on delete, then get the selected row and remove it from the table list. As you are using AbstractTableModel then you have to write your custom removeRow(..) method to perform this.
Example:
private boolean removeSelectedRow(int row) {
// Remove the row from the list that the table is using.
dataList.remove(row);
// You need to call fireXXX method to refresh the table model.
fireTableDataChanged();
return true;
// If fail return false;
}
If delete is the action then first get the selected row and then call removeSelectedRow(int) like the following,
private void deleteRow() {
int selectedRow = table.getSelectedRow();
boolean deleteStatus = removeSelectedRow(selectedRow);
// Only if the deletion is success then delete from the file.
if(deleteStatus) {
// Delete it from the file too.
}
}
first you have to make sure that something has been selected: when there is something selected than enable the delete button. please look up the JTable java source code #
http://developer.classpath.org/doc/javax/swing/JTable-source.html
and the following code:
1331: /**
1332: * Receives notification when the row selection changes and fires
1333: * appropriate property change events.
1334: *
1335: * #param event the list selection event
1336: */
1337: public void valueChanged(ListSelectionEvent event)
1338: {
1339: firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
1340: Boolean.FALSE, Boolean.TRUE);
1341: int r = getSelectedRow();
1342: int c = getSelectedColumn();
1343: if (r != lastSelectedRow || c != lastSelectedColumn)
1344: {
1345: Accessible o = getAccessibleAt(lastSelectedRow,lastSelectedColumn);
1347: Accessible n = getAccessibleAt(r, c);
1348: firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, o, n);
1350: lastSelectedRow = r;
1351: lastSelectedColumn = c;
1352: }
1353: }
You need to register for the last event to be notified when the selected rows have been changed. Add your own listener to enable the deletebutton based on whether or not a row has been selected which is as you can see in the event itself.
Please use to start with the DefaultTableModel because it will work in 90% of the cases.
And any change is applied to the tabledatamodel which will automatically propogate to the JTable View: normally you never change the view because all selection and scroll information is lost which is something you don't want.
When the delete button is fired the approach is straight forward: there is a row selected, otherwise it is impossible to click it: remove that selected row number from the defaultTableModel, and last but not least I would write simply the entire contents of the datamodel model to the designated file because the table's model hold the actual rows that are indeed displayed in the View.
So please think in terms of models models and models: Views are instantiated only once, packed scrolled etc and than you leave them as is. Models are normally also never changed: you change the contents of the models by adding and or deleting rows. One other tip: use always renderers: those that don't don't, in my humble opinion, don't understand how to work with JTables.
And yes you can leave out the first part to listen for selection changes: sure and pop up a warning to indicate the problem. And in a later stage add the functionality that listens for selection changes to enable and or disable the JButton delete row.
Hope this helps.

TableModelListener and multiple column validation

This is the first time for me to post here, so sorry if I made some mistake.
I am working on a JTable which column data have to verify some parameters, for example:
Column 3 values > 30
Column 4 values > 10
Column 5 values > 4
Also the first 2 columns are filled "automatically", putting 0s in the rest of the columns.
If that data is correct, in the Column 5 I would show an image of a tick, otherwise, I would show an image of a warning.
For verifying this I use the following code
ImageIcon accept = new javax.swing.ImageIcon(getClass().getResource("/resources/accept.png"));
ImageIcon deny = new javax.swing.ImageIcon(getClass().getResource("/resources/exclamation.png"));
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
double d1 = Double.valueOf(jTable.getValueAt(row, 2).toString());
double d2 = Double.valueOf(jT.getValueAt(row, 3).toString());
double d3 = Double.valueOf(jT.getValueAt(row, 4).toString());
if(d1>MAX_A||d2>MAX_B||d3>MAX_C){
jTable.setValueAt(deny, row, 5);
}
else{
jTable.setValueAt(accept, row, 5);
}
}
The problem of this code is that returns a Stack Overflow, and I don't know how to handle this.
Is there any other way to implement some verifier on a table that implies multiple cells?
Thanks in advance.
The problem of this code is that
returns a Stack Overflow, and I don't
know how to handle this.
The problem is that your code sets a value in the model listener so another tableChanged event is generated. Your code should be something like:
if (e.getColumn() != 5)
// do your code
I don't see a problem using a TableModelListener to dynamically set the value of a column based on data in another column. Here is a simple example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableProcessing extends JPanel implements TableModelListener
{
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)
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
#Override
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
// The Cost is not editable
#Override
public boolean isCellEditable(int row, int column)
{
return (column == 3) ? false : true;
}
};
model.addTableModelListener( this );
JTable table = new JTable( model );
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
String[] items = { "Bread", "Milk", "Tea", "Coffee" };
JComboBox<String> editor = new JComboBox<String>( items );
DefaultCellEditor dce = new DefaultCellEditor( editor );
table.getColumnModel().getColumn(0).setCellEditor(dce);
}
/*
* The cost is recalculated whenever the quantity or price is changed
*/
public void tableChanged(TableModelEvent e)
{
if (e.getType() == TableModelEvent.UPDATE)
{
int row = e.getFirstRow();
int column = e.getColumn();
if (column == 1 || column == 2)
{
TableModel model = (TableModel)e.getSource();
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);
}
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Table Model Listener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TableProcessing());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
You probably get your error because of getFirstRow call. I think it's a bad idea to validate table structure in JTable.tableChanged - how do you know that the table was fully filled and ready for validation? I would suggest filling the whole table first, then invokation of validation. Maybe there would be also a good idea to use separate table to display validation results

Categories