The porblem is that even I change cell value to the same exact value it counts as a change. So for example if I double click a cell and don't change its value my program proceeds to write it to the server.
How can I make that only cells that have their values changed, for instance (x -> y) write to server. (x -> x) do nothing.
Code below:
Misc_Table.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int col = e.getColumn();
Doo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
Object value = model.getValueAt(row, col);
int selected = (int) Functions_ComboBox.getSelectedIndex();
if(selected == 0){
String val = (String) value;
int int_Val = Integer.parseInt(val);
BackEnd.WriteMultiple_16Bit(unitID, row, int_Val , 1);
}
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(MISC,"Wrong number format.");
} catch (Exception e2) {
}
}
});
}
});
You can use the Table Cell Listener.
The TableCellListener replaces the TableModelListener and will only generate events when the data has actually changed, not when the editor is stopped. You just provide an Action to be invoked when the data changes.
Related
I use:
Java 10 SE
Java Swing
Eclipse IDE
I have JTable, the contents gets loaded at runtime dynamically. It has some JComboBoxes. If I select the JComboBox, and then attempt to reload the table, the JComboBox appears visible at the time when the table loading is in progress.
Besides that, if the JComboBox's contents gets updated (elsewhere in different table, when the combo supposed to reflect that new contents), that new contents does not get visible staright away after loading the JTable dynamically.
The snap-shot sample of the app:
That's, the table being loaded at runtime up, and in the middle you have vsisble JComboBox persistent from the previous selection.
How to:
Get rid off that persistent JComboBox
Make the data visible instantly, upon update under the combo, once you load the table dynamically
I have the public final class TableColumnEditor extends DefaultCellEditor{
which returns the JComboBox on a specific column:
else if(row == ROW_2_DEVS_WORK_WEEKEND) {
ProjectMetrics metrics = new ProjectMetrics();
JComboBox<String> combo = new JComboBox<>();
combo.setBackground(Color.WHITE);
for(String devs : metrics.identifyDevsThatWorkAtWeekend()) {
combo.addItem(devs);
}
return combo;
}
I have the public final class TableColumnRenderer extends DefaultTableCellRenderer{
which makes sure that the view displays the JComboBox under that specific column:
else if(row == ROW_2_DEVS_WORK_WEEKEND) {
ProjectMetrics metrics = new ProjectMetrics();
JComboBox<String> combo = new JComboBox<>();
combo.setBackground(Color.WHITE);
for(String devs : metrics.identifyDevsThatWorkAtWeekend()) {
combo.addItem(devs);
break;
}
return combo;
}
The table gets loaded dynamically right here (non-essential things removed):
public static void reloadTableDynamically(JTable metricsTable){
DefaultTableModel model = (DefaultTableModel)metricsTable.getModel();
if(projectData.isEmpty()) {
metricsTable.clearSelection();
int rowCount = model.getRowCount();
for(int item = (rowCount - 1); item >= 0; item--) {
model.removeRow(item);//clears previous rows
}
metricsTable.repaint();
return;
}
model.getDataVector().clear();
int rowCount = constantRows + ((devsTask.size() == 0) ? 1 : devsTask.size());
try {
new Thread(()-> {
int lastRowID = 0;
int devsTaskID = 0;
for(int item = 0; item < rowCount; item++) {
Object[] input = null;
if(item == 0) {
input = new Object[] {"", metrics.getProjectDateRange(), "" };
}//similar branches removed
else {
devsTaskID++;
input = new Object[] {"", devsTask.get(devsTaskID).getDeveloper(), ""};
}
model.addRow(input);
metricsTable.scrollRectToVisible(new java.awt.Rectangle(metricsTable.getCellRect(lastRowID++, 0, true)));
metricsTable.repaint();
try {
Thread.sleep(Config.getInstance().getReloadInOutTable());
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
metricsTable.scrollRectToVisible(new java.awt.Rectangle(metricsTable.getCellRect(projectData.size() - 1, 0, true)));
metricsTable.repaint();//so that to reach the last row
}).start();
}
catch(Exception e) {
}
}
What do you think?
Well, I figured out how to overcome this problem.
Firstly, the JComboBox gets updated on EDT(Event Despatch Thread).
/**
* #param combo The JComboBox ref.
* #param toDisplay The value to add to it
*/
public static void updateComboBoxOnEventDespatchThread(JComboBox<String> combo, String toDisplay) {
Runnable doComboUpdate = new Runnable() {
public void run() {
combo.addItem(toDisplay);
}
};
SwingUtilities.invokeLater(doComboUpdate);
}
Under the JTable column editor:
else if(row == ROW_2_DEVS_WORK_WEEKEND) {
ProjectMetrics metrics = new ProjectMetrics();
JComboBox<String> combo = new JComboBox<>();
combo.setBackground(Color.WHITE);
Runnable doComboInsert = new Runnable() {
public void run() {
int id = 0;
for(String devs : metrics.identifyDevsThatWorkAtWeekend()) {
UIutils.updateComboBoxOnEventDespatchThread(combo, "("+ ++id +") " + devs);
}
}
};
SwingUtilities.invokeLater(doComboInsert);
return combo;
}
But the main fix, without which both issues do not go away, is following.
That is, I noticed that in order for data to appear under the table instantly, firstly, you need to select any other unrelated table's cell.
That is, the Java thread, which loads the JTable at runtime, does need to have this:
if(model.getRowCount() > 0) {
metricsTable.selectAll();
}
That's probably a hack, but it works for me!
I have a Jtable with columns of Quantity, price and amount. If the user enters quantity and price, the amount value will show at the time of key releasing. It's working perfectly.
I am also having one textfield below the table, that shows the sum of table's last column values.
When the user changes the data, at the same time the JTextField values also will be change.
Now I am using a mouse click event. When clicking on the textField it will calculate sum of table last column values and display.
Can anyone help me?
Here is my code:
int rowCount = Table.getRowCount();
int lastRow = rowCount - 1;
//System.out.println("Last Row = " + lastRow);
//System.out.println("Last Row Value is = " + Table.getValueAt(lastRow, 0));
if (Table.getValueAt(lastRow, 0) == null) {
DefaultTableModel tmodel = (DefaultTableModel) Table.getModel();
tmodel.removeRow(lastRow);
}
else {
double value;
double total = 0;
//System.out.println("Row Count = " + rowCount);
for (int i = 0; i < rowCount; i++) {
value = (double) Table.getValueAt(i, 5);
total = total + value;
}
totalField.setText(new DecimalFormat("##.##").format(total));
}
But I don't want this mechanism.
When the user enters some values or modifies the values, at the same time that value will reflect in Textfield.
Instead of MouseListener, use a KeyListener on the JTable.
table.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent arg0) {
// You can set the value of textField to the value of Jtable column here.
}
#Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
But beware that this will be called for every keystroke on the table column. So you should keep the processing inside the keyTyped() method to a minimum.
I believe you want a focusListener for the textField, as and when a user clicks on this textField this event gets triggered.
Get the values from the table and do the math here.
JTextField textField = new JTextField("A TextField");
textField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
//do here
}
I have a JTable which has 2 columns
column 0 username
column 1 password.
for the password column it will be encrypted to SHA256.
Basically what I want to achieve is it will update all the rows in my password column to SHA256 that I have edited after my button is pressed.
so..
I have a RowData class, this will store the text being edited and the position of the text
being edited(rows,columns).
public class RowData {
int rows = 0, columns = 0;
String text = " ";
public RowData(String text,int rows, int columns) {
setEditedRows(rows);
setEditedColumns(columns);
setEditedText(text);
}
public int getEditedRows() {
return rows;
}
public int getEditedColumns() {
return columns;
}
public String getEditedText() {
return text;
}
public void setEditedRows(int rows) {
this.rows = rows;
}
public void setEditedColumns(int columns) {
this.columns = columns;
}
public void setEditedText(String text) {
this.text = text;
}
}
I wrote a TableModelListener.. I have an List to store the text and the rows and columns
after the table has changed
table.getModel().addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel) e.getSource();
//System.out.println(model.getValueAt(row, column));
if(column == 1) {
String data = (String) model.getValueAt(row, column);
System.out.println(data);
dataList.add(new RowData(data,row,column));
}
}
});
In my button I loop through the list and retrieve the rows, and columns and text
and set the password to SHA256 to the JTable.
updateBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (table.getCellEditor() != null) {
table.getCellEditor().stopCellEditing();
for(int i = 0; i < dataList.size(); i++) {
String text = dataList.get(i).getEditedText();
int rows = dataList.get(i).getEditedRows();
int columns = dataList.get(i).getEditedColumns();
//System.out.println(dataList.get(i).getEditedText() + " " + dataList.get(i).getEditedRows() + dataList.get(i).getEditedColumns());
table.setValueAt(convertPassword.convertToSHA256(text), rows ,columns);
}
}
}
});
The result I get is I will keep printing the password endlessly in my console.
So I think that my logic is wrong and needed to be corrected.
table.setValueAt(convertPassword.convertToSHA256(text), rows ,columns);
When you change the TableModel the TableModelListener will be invoked again. The TableModelListener is invoked whether you change the data by using the JTable or by updating the TableModel directly.
The solution would be to remove the TableModelListener when you click on your button, at the start of your ActionListener. You would then need to add the TableModelListener back to the TableModel at the end of the code in case the user make further changes.
Another solution is to have 3 columns in the TableModel, username, password and sha256Password. Then you can use the JTable to display only the first two columns. See the removeColumn() method of JTable. Then your conversion code would update the TableModel using:
table.getModel().setValueAt(value, row, 2);
Now the code in your TableModel will be invoked, but because you check for updates to column 1, nothing will happen when you update column 2.
Then when you save the data you save the data from the TableModel.
Edit:
I must click into another cell before I can press my button to edit.
You need to stop the cell editing. See Table Stop Editing for a couple of solutions.
I have a button click event on which i am getting a column value, if a Table row is selected. But if i don't select the row and click the button i get the error: java.lang.ArrayIndexOutOfBoundsException:-1 my question is how can i check to see if a row has been selected pseudocode: if(Row == selected) { execute }
java code i have:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
int row = Table.getSelectedRow();
String Table_click = (Table.getModel().getValueAt(row, 0).toString());
//... implementation hire
} catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
Thank you for your help.
Stop and think logically about your problem before you're tempted to post. Take a break from coding if you need to -- once you take a break, the solution to the problem often presents itself in short order.
int row = Table.getSelectedRow();
if(row == -1)
{
// No row selected
// Show error message
}
else
{
String Table_click = (Table.getModel().getValueAt(row, 0).toString());
// do whatever you need to do with the data from the row
}
When no row is selected, getSelectedRow() returns -1.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
int row = Table.getSelectedRow();
if (row > -1) {
String Table_click = (Table.getModel().getValueAt(row, 0).toString());
//... implementation here
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
if row > -1 then you know a row has been selected.
see: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#getSelectedRow()
I'm trying to set a RowSorter on my Jtable, I used the method setAutoCreateRowSorter(Boolean b) to sort the rows
table.setAutoCreateRowSorter(true);
But when I make the table as rawSorted, I get a strange error!
The conflict is visible when I want to delete a line, I used fireTableRowsDeleted().
int raw = table.getSelectedRow(); // the index of raw that i want to delete it
System.out.println(raw);
model.delte_raw(raw); // model is my table model
public void delte_raw(int raw)
{
if (!ls.isEmpty()) {
this.fireTableRowsDeleted(raw+1, raw);
ls.remove(raw);
}
I want to show you what result return the code as above in 2 cases:
Case 1:
When I make my table as not rawsorted:
table.setAutoCreateRowSorter(false);
when I delete a line, it all works successfully.
Case 2:
When I make my table as rawsorted:
table.setAutoCreateRowSorter(true);
when I delete a line, I get the error as below:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Invalid range
at javax.swing.DefaultRowSorter.checkAgainstModel(DefaultRowSorter.java:921)
at javax.swing.DefaultRowSorter.rowsDeleted(DefaultRowSorter.java:878)
at javax.swing.JTable.notifySorter(JTable.java:4277)
at javax.swing.JTable.sortedTableChanged(JTable.java:4121)
at javax.swing.JTable.tableChanged(JTable.java:4398)
at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:296)
at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:261)
I think that the error is in my defaultRowSorter, so I defined my specific cellRenderer as below:
// final TableCellRenderer r = table.getTableHeader().getDefaultRenderer();
//TableCellRenderer wrapper = new TableCellRenderer() {
// private Icon ascendingIcon = new ImageIcon("images/e.png");
// private Icon descendingIcon = new ImageIcon("images/e.png");
//
// #Override
// public Component getTableCellRendererComponent(JTable table,
// Object value, boolean isSelected, boolean hasFocus,
// int row, int column)
// {
// Component comp = r.getTableCellRendererComponent(table, value, isSelected,
// hasFocus, row, column);
// if (comp instanceof JLabel) {
// JLabel label = (JLabel) comp;
// label.setIcon(getSortIcon(table, column));
// }
// return comp;
// }
//
// /**
// * Implements the logic to choose the appropriate icon.
// */
// private Icon getSortIcon(JTable table, int column) {
// SortOrder sortOrder = getColumnSortOrder(table, column);
// if (SortOrder.UNSORTED == sortOrder) {
// return null;
// }
// return SortOrder.ASCENDING == sortOrder ? ascendingIcon : descendingIcon;
// }
//
// private SortOrder getColumnSortOrder(JTable table, int column) {
// if (table == null || table.getRowSorter() == null) {
// return SortOrder.UNSORTED;
// }
// List<? extends RowSorter.SortKey> keys = table.getRowSorter().getSortKeys();
// if (keys.size() > 0) {
// RowSorter.SortKey key = keys.get(0);
// if (key.getColumn() == table.convertColumnIndexToModel(column)) {
// return key.getSortOrder();
// }
// }
// return SortOrder.UNSORTED;
// }
//
//};
//table.getTableHeader().setDefaultRenderer(wrapper);
But again, the same error!
Why do I get this error? I googled it a lot, but either I used the wrong keywords or there are no simple solutions on the internet.
In your table model:
public void delte_raw(int raw) {
if (!ls.isEmpty()) {
this.fireTableRowsDeleted(raw+1, raw); // why raw+1 ???
ls.remove(raw);
}
}
As your table model extends from AbstractTableModel and looking at fireTableRowsDeleted(int firstRow, int lastRow) javadoc:
Notifies all listeners that rows in the range [firstRow, lastRow],
inclusive, have been deleted.
So it should be:
public void delte_raw(int raw) {
if (!ls.isEmpty()) {
ls.remove(raw); // remove the row index from the List and then fire the event
fireTableRowsDeleted(raw, raw);
}
}
Knowing the exception source: looking at DefaultRowSorter.checkAgainstModel(int firstRow, int endRow) implementation:
private void checkAgainstModel(int firstRow, int endRow) {
if (firstRow > endRow || firstRow < 0 || endRow < 0 ||
firstRow > modelRowCount) {
throw new IndexOutOfBoundsException("Invalid range");
}
}
As you can see, calling this method with [raw+1,raw] range causes an IndexOutOfBoundsException.
Edit
As #mKorbel masterfully points out, I've totally overlooked this:
int raw = table.getSelectedRow(); // this is the index in the view
model.delte_raw(raw); // convert raw in the right model index is needed
You need to convert raw in the right model index. Otherwise it can cause side effects since in a sorted table is most likely the selected index in the view be different than its related model's index:
int raw = table.getSelectedRow(); // this is the index in the view
model.delte_raw(table.convertRowIndexToModel(raw)); // perfect
See JTable.convertRowIndexToModel(int viewRowIndex)