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.
Related
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);
Okey so my problem is next: I use web scraping to take some data from web page IMDB in this case, that data is titles of movies, and I already tried to print it in console and that works fine. My problem is that I can not save that titles in my table columns, I put all needed codes for this problem, and I cant find where I made mistake and why titles wont show in table columns. At the and, user need to pick one title and that title need to be stored in text field. Have someone any idea, please?
I have table:
TableColumn izborAuta = new TableColumn("Izbor auta");
TableColumn lokacijaPreuzimanja = new TableColumn("Lokacija
preuzimanja");
TableColumn lokacijaVracanja = new TableColumn("Lokacija Vracanja");
TableColumn cena = new TableColumn("Cena");
I have this code to setup columns:
izborAuta.setCellValueFactory(new PropertyValueFactory<Vozila, String>
("izborAuta"));
lokacijaPreuzimanja.setCellValueFactory(new
PropertyValueFactory<Vozila, String>("lokacijaPreuzimanja"));
lokacijaVracanja.setCellValueFactory(new
PropertyValueFactory<Vozila, String>("lokacijaVracanja"));
cena.setCellValueFactory(new PropertyValueFactory<Vozila, String>
("cena"));
tableView.setItems(Baza.baza.prikazBaze());
tableView.getColumns().addAll(izborAuta, lokacijaPreuzimanja,
lokacijaVracanja, cena);
I have this code, so when I pick one item from table that item need to be stored in textField:
tableView.setOnMouseClicked((e) -> {
Vozila v = (Vozila)
tableView.getSelectionModel().getSelectedItem();
txIzborAuta.setText(v.getIzborAuta());
txLokacijaPreuzimanja.setText(v.getLokacijaPreuzimanja());
txLokacijaVracanja.setText(v.getLokacijaVracanja());
txCena.setText(v.getCena());
});
And at the end I use web scraping to save items in table:
Document doc = Jsoup.connect("https://www.imdb.com/chart/top?
ref_=nv_mv_250").get();
Elements elems = doc.select("table.chart.full-width");
for (Element e : elems) {
String izborAuta = e.select(".titleColumn").text();
String lokacijaPreuzimanja = e.select(".titleColumn").text();
String lokacijaVracanja = e.select(".titleColumn").text();
String cena = e.select(".titleColumn").text();
Vozila v = new Vozila();
v.setIzborAuta(izborAuta);
v.setLokacijaPreuzimanja(lokacijaPreuzimanja);
v.setLokacijaVracanja(lokacijaVracanja);
v.setCena(cena + " " + "RSD");
Baza.insertVozila(v);
}
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
}
});
I want to retrieve some data from a filtered row.
This is how i filter my table :
String makeText = makeFilterCombo.getSelectedItem().toString();
if (makeText == "All") {
makeText = "";
}
String numar = getEssRegex();
String impact = impactBox.getSelectedItem().toString();
if (impact == "All") {
impact = "";
}
TableModel model;
model = jTable1.getModel();
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
jTable1.setRowSorter(sorter);
List<RowFilter<Object, Object>> rfs = new ArrayList<RowFilter<Object, Object>>(2);
rfs.add(RowFilter.regexFilter(makeText, 2));
rfs.add(RowFilter.regexFilter(numar, 5));
rfs.add(RowFilter.regexFilter(impact, 9));
RowFilter<Object, Object> af = RowFilter.andFilter(rfs);
sorter.setRowFilter(af);
And this is how i try to get a value from a filtered row:
int f = search(connectedCarIndex);
connectedImage1 = jTable1.getModel().getValueAt(jTable1.convertRowIndexToModel(f), 10).toString();
connectedImage2 = jTable1.getModel().getValueAt(jTable1.convertRowIndexToModel(f), 11).toString();
connectedImage3 = jTable1.getModel().getValueAt(jTable1.convertRowIndexToModel(f), 12).toString();
System.out.println(connectedImage1 + "-------" + connectedImage2 + "------" + connectedImage3);
But none of this works ?
Can anybody help me ?
The code works and i can see the connected image name if the rows are shown
int f = search(connectedCarIndex);
I have no idea what the search(...) method does.
If you are searching the data that is displayed in the table then you would just use:
table.getValueAt(...);
If you are searching all the data that is stored in the TableModel then you would use:
table.getModel().getValueAt(...);
there is no need to convert the index if you know what you are searching.
I'm currently getting the value of my jTable as everybody does:
String d = jTable1.getModel().getValueAt( jTable1.getSelectedRow() , row ).toString();
The thing is that now I'm sorting my jTable with a rowsorter:
sorter = new TableRowSorter<TableModel>(modelo);
jTable1.setRowSorter(sorter);
private void filterTable(){
//If current expression doesn't parse, don't update.
try {
rf = RowFilter.regexFilter("(?iu)"+jFilter.getText(),0,1,2,3,4);//no filtrar la columna con imágenes porque hace cualquiera
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
sorter.setRowFilter(rf);
}
THE PROBLEM:
Once the table is filtered, the function getSelectedRow returns the correct row, but the getModel function returns the original model, not the one after filtering...
THE QUESTION:
How to get the correct value from the table when it is filtered?
You probably need to convert your "row" value to a model index value, using convertRowIndexToModel.
String d = Table1.getModel().getValueAt(convertRowIndexToModel(jTable1.getSelectedRow()) , column ).toString();
For future wanderers:
The problem was on the way of getting the value from the jTable:
This is wrong when you have a rowsorter:
String d = jTable1.getModel().getValueAt( jTable1.getSelectedRow() , row ).toString();
This is what you should be doing:
String d = jTable1.getValueAt( jTable1.getSelectedRow() , row ).toString();