Select a row of a Table for second time - java

i currently able to select a row of table using ListSelectionModel object and open a new window. but if i close that window and click that row again, it would not open anymore until i select another row of table (i can't select a row for a second time). do you know how can i solve this issue ?
this is what i have done:
ListSelectionModel model = table.getSelectionModel();
model.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e)
{
if(!model.getValueIsAdjusting())
{
int row = model.getMinSelectionIndex();
//new Window opens :
SubjectDetail sd = new SubjectDetail(Datalist2.project.listOfData().get(row));
}
}
});

The selection is not working because that particular row is already selected.
Try clearing the selection when a new window is opened.
table.getSelectionModel().clearSelection().

Related

Java SWT clear table headers

I'm working on a SWT GUI and I'm trying to create a button that on press clear all table data and the headers.
table.removeAll();
This command don't work well because it's removing only the data inside and I need to remove the table headers too.
Is there a solution?
EDIT: after the code to clear the header worked, if I try to add new file with data, the header go to the next point. why if it blank? (the ArrayLists that contains the header names cleared too).
First image is when first file uploaded, after click on "Start" button the data shown on table:
Second image is after the table cleared all data by pressing on another button:
Third image is after uploading new file and press "Start" button:
EDIT: Headers Set
tableConfigurationSystemColumnTools.add("Parameter Name");
for (String str : tableSystemColumn) {
String[] a = str.split("PCM");
tableConfigurationSystemColumnTools.add(a[0].trim());
}
for (int loopIndexSystemColumnTools = 0;
loopIndexSystemColumnTools < tableConfigurationSystemColumnTools.size(); loopIndexSystemColumnTools++) {
TableColumn column = new TableColumn(tableConfigurationSystem, SWT.NULL);
column.setWidth(100);
column.setText(tableConfigurationSystemColumnTools.get(loopIndexSystemColumnTools));
}
for (int loopIndexSystemColumnTools = 0; loopIndexSystemColumnTools < tableConfigurationSystemColumnTools.size(); loopIndexSystemColumnTools++) {
tableConfigurationSystem.getColumn(loopIndexSystemColumnTools).pack();
}
EDIT: I've founded the answer, look at my comment.
button.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
table.removeAll();
TableColumn[] columns = table.getColumns();
for (int i = 0; i < columns.length; i++) {
columns[i].setText("");
}
}
});
I founded the answer - in the "for" loop that creating TableColumn, I'v added "index":
TableColumn column = new TableColumn(tableConfigurationSystem, SWT.NONE, loopIndexSystemColumnTools);
this is working.
Thank you guys.

JTable get value from cell when is not submitted

I would like to get value from cell when its is no submitted (cell is in edit mode) - "real time"; Is it possible?
I tried this but it is working only if i submit data - press enter
int row = jTable.getSelectedRow();
int col = jTable.getSelectedColumn();
String cellValue = jTable.getValueAt(row, col).toString();
I want to get on keypress cell value without exiting it, get this text real time while typing
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
int row = jTable.getSelectedRow();
int col = jTable.getSelectedColumn();
if (e.getID() == KeyEvent.KEY_RELEASED) {
if (jTable.isEditing())
jTable.getCellEditor().stopCellEditing();
String cellValue = (jTable.getValueAt(row, col)!=null) ? jTable.getValueAt(row, col).toString() : "";
System.out.println(cellValue);
}}
jTable.getCellEditor().stopCellEditing() - cause ugly in/out animation while typing
#camickr Sorry for the confusion. Your solution is ok.
I just needed to add jTable.editCellAt(row, col); to get back into edit mode.
Thanks again
cell is in edit mode
The editing must be stopped before the value is saved to the model.
The easiest way to do this is to use:
JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
when you create the table.
Now when you click on the button to do your processing the table loses focus so the data is saved.
Check out Table Stop Editing for more information.

Appending a new row to a JTable at runtime

I am developing a Java application that uses a JTable. I want to allow the user to enter data in a JTable that can later be printed or saved. The issues is: Suppose the user has entered some data and the cursor is on the last cell of the row in a table (I have uploaded an image-suppose the cursor is on the highlighted cell).How do I make it in such a way that when a user presses the Enter Button(Keyboard button) the application will add/append a new row that is empty to the table so that the user can fill in other data.
Attachment
Try this...
JTable table = new JTable();
DefaultTableModel tblModel = new DefautlTableModel(0,0);
table.setModel(tblModel);
//an action or click then you should run this code
tblModel.addRow(new Object[]{""});
DefaultTableModel tbm = (DefaultTableModel) jTable1.getModel();
Vector rowData = null;
tbm.addRow(rowData);

Read cell value in JTable while it is being edited

Please guide me how can i read the value of jtable cell while it is in edit mode ( being edited). I have written above code in keyTyped event of jtable.
int col = tblItem.getSelectedColumn();
int row = tblItem.getSelectedRow();
try {
float value = Float.parseFloat(tblItem.getModel().getValueAt(row,2).toString());
String str = new help.StringHelp().convertFloatString(value);
tblItem.setValueAt(str + "", row, 2);
} catch (Exception ex) {
ex.printStackTrace();
}
Suggest me how can i solve this issue.
and directly press save button
So you need to stop editing on the cell before doing the Save.
You can either use:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
when you create the table.
Or use:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
in the ActionListener of your button.
Check out Table Stop Editing for more information.

Refreshing table Editor with table item in swt

Hello i have a following code for swt table
private Table folderAssociationTable;
private TableItem item;
private TableEditor editor;
folderAssociationTable = componentsRenderer.createTableWidget(container, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION, 2, true);
folderAssociationTable.addListener(SWT.MeasureItem, new Listener() {
public void handleEvent(Event event) {
event.height = 20;
}
});
componentsRenderer.createTableColumWidget(folderAssociationTable, SWT.CHECK, "Item", 80);
// add a column to display source folders
componentsRenderer.createTableColumWidget(folderAssociationTable,
SWT.LEFT, PropertyClass.getPropertyLabel(QTLConstants.SOURCE_FOLDER_COLUMN_LABEL), 200);
// add a column to display target folders
componentsRenderer.createTableColumWidget(folderAssociationTable,
SWT.LEFT, PropertyClass.getPropertyLabel(QTLConstants.TARGET_FOLDER_COLUMN_LABEL), 200);
and then adding table data in this fashion
item = new TableItem(folderAssociationTable, SWT.NONE);
editor = new TableEditor (folderAssociationTable);
Button button = new Button(folderAssociationTable, SWT.CHECK);
button.setText("item "+ (i+1));
button.pack();
editor.minimumWidth = button.getSize ().x;
editor.setEditor(button, item, 0);
item.setText(1, folderlist[i]); // add source folder to the first column of the table
item.setText(2, targetFolderPath); // add target folder to the second column of the table
sourceTargetFolderMap.put(folderlist[i], targetFolderPath);
I have remove button on this page outside table component , on its action Listener i am removing selected row from table , but in this case when table is getting updated only table items are updated but table editor remain at same position , how can i refresh both table editor and table items on click of remove button.
Have you seen the example given in the documentation? Add this to your ActionListener:
// to close the old editor
Control oldEditor = editor.getEditor();
if (oldEditor != null) oldEditor.dispose();
// add some logic if you want to open the editor again on a different row

Categories