JTable get value from cell when is not submitted - java

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.

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.

Select a row of a Table for second time

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().

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.

How to get the contains of file in jtable?

I create a jtable like this :
String name = temp.getName();
String enemy = namaFileUtama.toString();
DefaultTableModel models = (DefaultTableModel) Main_Menu.jTable4.getModel();
List<ReportMomentOfTruth> theListRMOT = new ArrayList<ReportMomentOfTruth>();
ReportMomentOfTruth rmot = new ReportMomentOfTruth();
rmot.setNameOfMainFile(name);
rmot.setNameOfComparingFile(enemy);
theListRMOT.add(rmot);
for (ReportMomentOfTruth reportMomentOfTruth : theListRMOT) {
models.addRow(new Object[]{
reportMomentOfTruth.getNamaFileUtama(),
reportMomentOfTruth.getNamaFilePembanding(),
});
}
You know, I dont get an idea. How can I get the contains the file if I click one row in jtable then the contains will be show in jTextArea ? Any suggestion ? any example perhaps ?
Thanks
edit
You know, I am using netbeans, I can get a method like this
private void jTable4MouseClicked(java.awt.event.MouseEvent evt) {
if (evt.getClickCount() == 1) {
}
}
Now how to ?
How can I get the contains the file if I click one row in jtable then the contains will be show in jTextArea?
You can better use JEditorPane that has a method setPage() that can be used to initialize the component from a URL.
Just get the values of selected row and use below code to set the content in JEditorPane.
sample code:
final JEditorPane document = new JEditorPane();
document.setPage(new File(".../a.java").toURI().toURL());
Add ListSelectionListener to detect the selection change event in the JTable
final JTable jTable = new JTable();
jTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int row = jTable.getSelectedRow();
if(row != -1){
String firstColumnValue = jTable.getModel().getValueAt(row, 0).toString();
String secondColumnValue = jTable.getModel().getValueAt(row, 1).toString();
// load the JEditorPane
}
}
});;
Read more...

Modify excel spreadsheet using jxl on Android

I'm developing an Android App for filling a excel pre-formatted spreadsheet, using jxl. My issue is when i'm using the code for modifying cells according to the tutorial:
WritableCell cell = sheet0.getWritableCell(0,8);
if (cell.getType() == CellType.LABEL)
{
Label l = (Label) cell;
l.setString("hi");
}
It doesn't appear anything on the spreadsheet. Even the the cell if the document is formatted for text, still doesn't work. I've attempted to write a number using:
WritableCell cell = sheet0.getWritableCell(0,8);
if (cell.getType() == CellType.NUMBER)
{
Number l = (Number) cell;
l.setValue(1);
}
and format the cell on the document as a number type, and still doesn't work. The only way that i can write on the document is by using the addCell method:
Label label2 = new Label(0, 8, "X");
sheet0.addCell(label2);
But in this way, the system will just add a new cell, not modifying the content, and losing the original format (the most critical for me is the borders style).
Please advise.
Kind Regards.
I had exactly the same problem, and I ended up solving it like this:
Cell existingCell = worksheet.getCell(columnIndex, rowIndex);
Label label = new Label(columnIndex, rowIndex, "some string");
label.setCellFormat(existingCell.getCellFormat());
worksheet.addCell(label);

Categories