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()));
Related
I'm trying to make a Table take the data where I make condition a delete from ... to ... , the data returns one row thinner, maybe one row deleted? my code:
JScrollPane scrollPane_9 = new JScrollPane();
scrollPane_9.setBounds(10, 105, 1396, 524);
month.add(scrollPane_9);
DefaultTableModel tableModel = new DefaultTableModel();
table_10 = new JTable(tableModel);
scrollPane_9.setViewportView(table_10);
tableModel.addColumn("INVOICE ID ");
tableModel.addColumn("DATE");
tableModel.addColumn("CASH");
tableModel.addColumn("MADA");
tableModel.addColumn("COPON PRISE");
tableModel.addColumn("COPONE NAME");
tableModel.addColumn("SELMAN");
tableModel.addColumn("DESCRIPTION");
String sqlQ = "SELECT `invoiceid`,`date`,`cash`,`payme`, `coponpri`, `copon`,`selman`,`desc` FROM `daay` where DATE between DATE(?) and DATE(?)";
PreparedStatement pstmt5 = con.prepareStatement(sqlQ);
pstmt5.setDate(1, date8);
pstmt5.setDate(2,date9);
ResultSet rs4 = pstmt5.executeQuery();
if (rs4.next() == false)
{
JOptionPane.showMessageDialog(null, "لا يوجد مبيعات في هذا الشهر");
}
else
{
tableModel.addRow(new Object[]{"","","","","","","",""});
while(rs4.next()){
tableModel.addRow(new Object[]{rs4.getInt(1), rs4.getDate(2),rs4.getInt(3),rs4.getInt(4),rs4.getInt(5),rs4.getString(6),rs4.getString(7),rs4.getString(8)});
}
}
the data come to me less thin a one row
if (rs4.next() == false)
...
You read the first row outside the loop, so it is never added to the model.
Your logic should be something like:
tableModel.addRow(new Object[]{"","","","","","","",""});
while(rs4.next())
{
tableModel.addRow(new Object[]{rs4.getInt(1), rs4.getDate(2), rs4.getInt(3), rs4.getInt(4), rs4.getInt(5), rs4.getString(6), rs4.getString(7), rs4.getString(8)});
}
if (tableModel.getRowCount() == 1)
{
JOptionPane.showMessageDialog(null, "لا يوجد مبيعات في هذا الشهر");
}
That is check the number of rows added to the model after iterating through the ResultSet.
Or, maybe you could use:
//if (rs4.next() == false)
if (rs4.isLast())
I'm not an SQL expert, so I'm not sure what that will return on an empty ResultSet.
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'm working on a project where the TableView looks like this:
The datas are inside are a MySQL database, and I use a modell and an observableArrayList to get them.
I want to make an initialize method which read the whole selected Line and save it into the TextFields (like Name-Name, Address - Address etc). I tried with this, and now I can Select the Whole Line data, but can't split it into Strings to set the TextField texts:
ListViewerModell details = (ListViewerModell) table.getSelectionModel().getSelectedItem();
//Checking if any row selected
if (details != null) {
//??? - split
editName.setText("*Name*");
editAddress.setText("*Address*");
}
Tried with the modell getName(), getAdress() methods but I got NullPointerException and TablePos methods.
// create table
TableView<Person> table = new TableView<Person>();
// create columns
TableColumn<Person, String> nameCol = new TableColumn<Person, String>("Name");
nameCol.setCellValueFactory(new PropertyValueFactory("name"));
TableColumn<Person, Address> addressCol = new TableColumn<Person, String>("Address");
addressCol.setCellValueFactory(new PropertyValueFactory("address"));
// add columns
table.getColumns().setAll(nameCol, addressCol);
// get data from db, return object List<Person> from DB
ObservableList<Person> persons = getPersonsFromDB();
table.setItems(persons);
tableView.setOnMouseClicked((MouseEvent event) -> {
if (event.getClickCount() > 1) {
onEdit();
}
});
public void onEdit() {
// check the table's selected item and get selected item
if (table.getSelectionModel().getSelectedItem() != null) {
Person selectedPerson = table.getSelectionModel().getSelectedItem();
nameTextField.setText(selectedPerson.getName());
addressTextField.setText(selectedPerson.getAddress());
}
}
One way could be extracting the value of the columns of the selected row, like so :
Object selectedItems = table.getSelectionModel().getSelectedItems().get(0);
String first_Column = selectedItems.toString().split(",")[0].substring(1);
System.out.println(first_Column);
I need to make a programm in JavaFX where I need to manage a movie library. I have a tableview that is filled by an observable list. Then I made bindings between the Properties and the Textboxes or Labels next to the tableview. Now the problem is, I have also pictures that are related to the movies, like cinema posters. and at the moment I just use a hardcoded one:
imgPoster = new Image(getClass().getResourceAsStream("../resources/images/posters/" + 5 + ".jpg"));
In the datafile there is one column with the ID of the movie and the same number is also the picture for it. So I need to replace that "5" in the sample code with a binding or so that it actively changes the picture as soon as I click on a row in the tableview.
How do I do that?
edit:
After the first comment i did that:
imgOscars = new Image(getClass().getResourceAsStream("../resources/images/oscar/Oscar-logo.png"));
oscars = new ImageView(imgOscars);
oscars.setPreserveRatio(true);
oscars.setFitHeight(45);
but question stays
Either add a listener to the selected item property in the table (I am just guessing at the model you are using for the table):
TableView<Movie> table = ... ;
table.getSelectionModel().selectedItemProperty().addListener((obs, oldSelectedMovie, newSelectedMovie) -> {
if (newSelectedMovie == null) {
oscars.setImage(null);
} else {
// get image from new selection
Image image = new Image(getClass().getResourceAsStream("../resources/images/oscar/"+newSelectedMoviegetId()+".png"));
oscars.setImage(image);
}
});
or use a binding:
oscars.imageProperty().bind(Bindings.createObjectBinding(() -> {
if (table.getSelectionModel().getSelectedItem() == null) {
return null ;
} else {
Movie selected = table.getSelectionModel().getSelectedItem();
return new Image(getClass().getResourceAsStream("../resources/images/oscar/"+selected.getId()+".png")); ;
},
table.getSelectionModel().selectedItemProperty());
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();