Receiving incorrect values from a JTable - java

I am trying to retrieve and use values from a JTable after a row has been selected using this code:
#Override
public void valueChanged(ListSelectionEvent e){
TableModel tableModel = this.mainFrame.getViewersTableModel();
this.mainFrame.setViewerButtonsEnabled(
!((Boolean)(tableModel.getValueAt(e.getLastIndex(), 1)))
);
}
In my scenario, the table has one row with a value of true, and another of false.
The very strange thing is that the first time I select a row, the value given is correct, whether it be the row holding true, or the one with false, but any subsequent selections result in getting true, no matter which row I pick.

Well, I fixed it.
Instead of getting the required boolean in my event handler class, I just made a method in the mainFrame class that retrieves the boolean from the table. The issue is now resolved.
Thank you to all who helped.

Related

Get the value of a specific cell of JTable with mouse clicking

I have a JTable and Given such codes:
jTable.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println(jTable.getRowCount());
System.out.println(jTable.getModel().getValueAt(jTable.getRowCount(), 0));
}
});
If I click on a certain row, like in the picture above, I clicked the second row, how can I get that row's content?(How to get the Canada)?
Personally, I use the Mouse Clicked event. You could try something like this inside your event method:
private void myTableMouseClicked(java.awt.event.MouseEvent evt) {
int row = this.myTable.getSelectedRow();
int column = this.myTable.getSelectedColumn();
this.myTable.getValueAt(selectedRow, selectedColumn);
}
Be aware that the getValueAt method returns an Object. You probably will need to cast the Object returned into the object it is supposed to be. And also you could have a global variable that's going to have the value returned by getValueAt for using it as you need.
I hope it helps.

How to get the table whose cell is currently being clicked?

I've two tables in panels. When I click on first table on some cell, its row is getting selected. And when I click on the second table on some cell, its row is also getting selected.
Now, How will I come to know, which table is last clicked. I tried with isRowSelected on both the tables, both are returning, so I'm not able to find the last clicked table?
Can somebody help me?
you have to read how
JTable
ListSelectionListener
works,
you have to understand both concepts, simple example here, another here
I don't know if this must be determined from a MouseListener or from a ListSelectionListener, but the simplest solution is similar: use a different listener for each table:
table1.addXxxListener(new XxxListener() {
// here, you know it's table 1
}
table2.addXxxListener(new XxxListener() {
// here, you know it's table 2
}
Another way to do it is to check the source of the event:
new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (e.getSource()==table1.getSelectionModel()) {
// Event comes from table1
} else if (e.getSource()==table2.getSelectionModel()) {
// Event comes from table2
}
}
}
Of course this is true if and only if the selection model is used by a single table (which is the case if you have not set your own ListSelectionModel)

GWT CELLTABLE How to restore old value in EditableTextCell?

I want to used editableNumbercell like intger,decimal etc..
But there is no such gwt widget present so I am using editable text cell and using validation for numbers when user update value in cell.
How to avoid cell editing when validation fails.
if validation fail then editabletext cell value restored to old value.How to do that?
intgerColumn.setFieldUpdater(new FieldUpdater<RecordInfo, String>() {
public void update(int index, RecordInfo object, String value) {
// Called when the user changes the value.
if(value.matches("(-)?(\\d){1,8}")){
object.setColumnInRecordEdited(true);
object.setValue(value);
RecordData.get().refreshDisplays();
}else{
Window.alert("Specify valid integer value for parameter");
// How to rest old value here? currently update value set to cell
}
}
});
Any help or guidance in this matter would be appreciated.
You can do it like this:
// clear incorrect data
cell.clearViewData(KEY_PROVIDER.getKey(object));
cellTable.redraw();
Where cell is the TextEditCell that you're using for that column.
It´s usefull for me.

problem when implementing a selection listener in a JTable

I am developing a JTable with different rows. I would like to associate an event to the selection of a row in this table. I have used the following selection class to provide behaviour to the table selection:
public class TableSelectionListener implements ListSelectionListener{
public Integer item;
public TableSelectionListener(Integer item){
this.dialog = item;
}
public void valueChanged(ListSelectionEvent e) {
System.out.println("The row clicked is "+item);
}
}
When I create an instance of this table, sai tabletest, I have added the following piece of code:
tabletest.getSelectionModel().addListSelectionListener(new TableSelectionListener(tabletest.getSelectedRow());
The problem is that when I click on one row once, instead of retrieving the related message once, I retrieve the same message several times, suggesting that the actions repeated several times. For example:
The row clicked is 0
The row clicked is 0
The row clicked is 0
The row clicked is 0
Does anyone know where the problem may be?
Well, that's just normal.
Your selection listener is created with the value of tabletest.getSelectedRow() at its creation table (which is zero). And, as you never change the value of item in your listener, this listener fcan only display 0as a result.
If I were you, I wouold replace the valueChanged() method by something like (although it's untested and I remember strange things happens sometimes when mixing view and model row values) this :
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()) // added as sometimes, multiple events are fired while selection changes
System.out.println("The row clicked is "+e.getFirstIndex());
}
Firstly, it's perfectly normal to get multiple ListSelectionEvents, while the selection is being changed. You can use the getValueIsAdjusting method to determine when selection has ended (it will return false).
Secondly, there's no need to construct your TableSelectionListener with a row number. When your valueChanged method is called, you can get the index of the first/last selected row (remember it's possibly to select multiple rows in the table, unless you disable that) using e.getFirstIndex() and e.getLastIndex() respectively.
An easier way of doing it, is as follows:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
System.out.println("e...."+table.getSelectedRow());
}
});

How to focus on JTextField within a table

I'm writing a search & replace function in a kind of spreadsheet program. What I want is that if you search for a string, the program shows a table with the element that has been found.
So far so good, but I cannot get the element to obtain the focus, with the cursor in it so you can immediately start typing.
I'm using a customized JTable and also a customized TableCellEditor. The following tricks do not seem to work:
(within the customized TableCellEditor):
SwingUtilities.invokeLater(new Runnable() {
public void run() {
my_textfield.requestFocus();
}
});
or:
my_jtable.editCellAt(0, 3);
my_jtable.requestFocus();
or
my_jtable.getEditorComponent().requestFocusInWindow();
Am I missing something? Is there a good description (nice flow diagram) that shows how events take place? Or example code that might do something similar?
With some googling i found a forum thread : programmatically start editing a cell in a JTable answered with following idea:
(in a subclass of JTable)
editCellAt(row,column);
requestFocus();
DefaultCellEditor ed = (DefaultCellEditor)
getCellEditor(row,column);
ed.shouldSelectCell(new ListSelectionEvent(this,row,row,true));
Would it work?
Did you try the editcellat without the requestfocus ?
also make sure that you override/implemenet to return true
/**
* Returns true.
* #param anEvent an event object
* #return true
*/
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
Check if you have enabled selection on your custom table instance like below:
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
With this being set, generally a call to table.editCellAt(row, col); starts editing. Example :
JTable myTable = new JTable(rows, cols);
myTable.setColumnSelectionAllowed(true);
myTable.setRowSelectionAllowed(true);
and somewhere else..where edit is needed,
boolean wasEditStarted = table.editCellAt(row, col);
if (wasEditStarted) {
table.changeSelection(row, col, false, false);
}

Categories

Resources