Required help in java.how to make save changes in jtable when user edit it.
my data is stored and populated form Text File.
Assuming you are using a tabel model, this is how you listen for the user's table modification. Then save the row, column, and data to your text file.
table.getModel().addTableModelListener(new TableModelListener()
{
public void tableChanged(TableModelEvent e)
{
int row = e.getFirstRow();
int column = e.getColumn();
TableModel tmodel = (TableModel)e.getSource();
Object data = tmodel.getValueAt(row,column); //save this to your text file
}
});
Related
So I'm trying to after searching a name click on the table and then edit it in other table, the problem is that I'm not getting the right ID but instead only getting the ID that is the first.
JTable
Search in action
ID wrong
Edit Code
int linha = this.jTable1.getSelectedRow();
int idUtilizador = Integer.parseInt((String)(this.jTable1.getModel() ).getValueAt(linha, 0));
Utilizador uti = UtilizadorJpaController.read(idUtilizador);
CriarCliente updateCliente = new CriarCliente(uti);
updateCliente.setVisible(true);
Search Code
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(model);
jTable1.setRowSorter(tr);
tr.setRowFilter(RowFilter.regexFilter(jTextField1.getText().trim(),1))
When you sort or filter a table, the data in the TableModel doesn't change. Only the view of the data changes.
If you want to get the data from the model, then you need to convert the "view row" to the "model row":
int linha = this.jTable1.getSelectedRow();
int modelRow = jTable1.convertRowIndexToModel(linha);
int idUtilizador = Integer.parseInt((String)(this.jTable1.getModel() ).getValueAt(modelRow, 0));
//int idUtilizador = Integer.parseInt((String)(this.jTable1.getModel() ).getValueAt(linha, 0));
Also, why are you using Integer.parseInt(...)? If the data in the model is an integer value, then it should be stored as an Integer value, not a String value.
Table width goes beyond the right margin and some content inside table cell is lost when there is bigger word(continuous text) present.
Below code implemented to print table
public Table createTable(Document doc, UnitValue[] columnsWidth) {
Table table = new Table(columnsWidth);
table.setBorder(new SolidBorder(0.9f));
float tableWidth = doc.getPdfDocument().getDefaultPageSize().getWidth()-(doc.getLeftMargin()+doc.getRightMargin());
table.setWidth(tableWidth);
return table;
}
public void addTable(Document doc, AbstractElement<?> parent, Table table) {
if(parent == null){
table.setMarginTop(2f);
doc.add(table);
}
}
UnitValue percentageUnit = new UnitValue(2, 1.00f);
UnitValue[] percentageUnitArr = new UnitValue[2];
percentageUnitArr[0] = percentageUnit;
percentageUnitArr[] = percentageUnit;
Table table = createTable(doc, percentageUnitArr);
addTable(doc, iTextParent, table);
I have a JTable with rows of Data
I have this event that listen every time a row got mouse clicked
private void tablePOMouseClicked(java.awt.event.MouseEvent evt) {
try {
int row1 = tablePO.getSelectedRow();
cellA = tablePO.getValueAt(row1, 0).toString();
cellB = tablePO.getValueAt(row1, 1).toString();
cellC = tablePO.getValueAt(row1, 2).toString();
cellD= tablePO.getValueAt(row1, 3).toString();
cellE = tablePO.getValueAt(row1, 4).toString();
cellF = tablePO.getValueAt(row1, 5).toString();
cellG = tablePO.getValueAt(row1, 6).toString();
cellH = tablePO.getValueAt(row1, 7).toString();
} catch (Exception e) {
}
}
variable cellA-H are all Strings.
its working good, but now I want to change it, I dont want the user to have the need to use the mouse, so instead, I want the user just select the row with using either UP/DOWN arrow to navigate the rows and put the selected row under the highlight, but I have no Idea how I am able to achieve it, reading the data from highlighted/selected row by using the UP/DOWN Keys (Not by pointing the row with mouse click).
Add a ListSelectionListener to the table.
An event will be generated whenever the row selection changes whether you use the mouse or the keyboard.
Read the section from the Swing tutorial on How to Write a ListSelectionListener for more information and working examples.
I want to apply the filter to the JTable.
String text = textField.getText();
rowSorter = new TableRowSorter<>(tableModel);
this.getjTable1().setRowSorter(rowSorter);
this.getjTable1().removeAll();
if (text.trim().length() == 0) {
rowSorter.setRowFilter(null);
} else {
//String regex = String.format("^%s$", text);
if(jCheckBoxExtract.isSelected()){
text="^"+text+"$";
}
else{
if(!text.contains(".")||text.contains("$"))text="^"+text;
}
RowFilter rowFilter = RowFilter.regexFilter(text, 1);
rowSorter.setRowFilter(rowFilter);
}
this.getjTable1().repaint();
this code work but now, if I want to get a value in jtable, the model doesn't update. The model use in jtable is always the old model but not the new model after filter.
to get the proper value do this whenever you need the row from the table:
model.getSelectedEntry(table.convertRowIndexToModel(table.getSelectedRow()));
I used SWT to create a table, and in this table's forth column need to be editable, so I used TableEditor to make it. However every time I need to click the next Widget Text to save the content of previous text content. For example: in my table the forth column can be editable,
I fill number in the first row forth column, I need to click the second row forth column, and only do this the content of Text of first row forth column can be saved.why? Follow is my
code:
final TableEditor editor=new TableEditor(table);
editor.horizontalAlignment=SWT.LEFT;
editor.grabHorizontal=true;
final int EDITABLECOLUMN=3;
table.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// Clean up any previous editor control
Control oldEditor = editor.getEditor();
if (oldEditor != null)
oldEditor.dispose();
// Identify the selected row
TableItem item = (TableItem) e.item;
if (item == null)
return;
// The control that will be the editor must be a child of the
// Table
Text newEditor = new Text(table, SWT.NONE);
newEditor.setText(item.getText(EDITABLECOLUMN));
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent me) {
Text text = (Text) editor.getEditor();
editor.getItem().setText(EDITABLECOLUMN, text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
editor.setEditor(newEditor, item, EDITABLECOLUMN);
}
});
and my table created in this way:
public Table createTable(Composite parent){
//表格布局
GridData gridData=new GridData();
gridData.horizontalAlignment=SWT.FILL;
gridData.grabExcessHorizontalSpace=true;
gridData.grabExcessVerticalSpace=true;
gridData.verticalAlignment=SWT.FILL;
//创建表格,及其格式
final Table table=new Table(parent,SWT.CHECK|SWT.MULTI|SWT.FULL_SELECTION|SWT.V_SCROLL|SWT.H_SCROLL);
table.setHeaderVisible(true);
table.setLayoutData(gridData);
table.setLinesVisible(true);
//创建表头
String[] tableHeader={"Num","Package","Class","Run Times"};
int[] tableColumnWidth={100,150,400,80};
for(int i=0;i<tableHeader.length;i++){
TableColumn tableColumn=new TableColumn(table,SWT.NONE|SWT.CENTER);
tableColumn.setText(tableHeader[i]);
tableColumn.setMoveable(true);
tableColumn.setWidth(tableColumnWidth[i]);
}
table.addListener(SWT.Selection, tableListener);
return table;
}
I changed the content of area where was marked as number one, this content only saved when I clicked area 2. It means only clicking area 2 to active the event: