I want to get First column value as we can achieve in Jtable using swing. below is my code and image for jtable.
String Table_Clicked = jTable1.getModel().getValueAt(row, 0).toString();
As you can see in image when i click to the Name column value eight it gives me first column value like 8. But I select Name Column
So how to achieve this in JavaFX with TableView Componenet.
I get the selected Value from the TableView as you can see in image below with the code.
tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
if(tableview.getSelectionModel().getSelectedItem() != null)
{
TableViewSelectionModel selectionModel = tableview.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();
TablePosition tablePosition = (TablePosition) selectedCells.get(0);
Object val = tablePosition.getTableColumn().getCellData(newValue);
System.out.println("Selected value IS :" + val);
}
}
});
So I want the same in tableview First column data as we can get in Jtable? so how to get that NO value.. as using my above code I get the value of selected Cell that is eight print in console.. but I want to get First Column Value.. Help me to go thorough Ahead.
Thanks..
UPDATE FOR DATA FILLING CODE OF TABLEVIEW
PreparedStatement psd = (PreparedStatement) conn.prepareStatement("SELECT No,name FROM FieldMaster");
psd.execute();
ResultSet rs = psd.getResultSet();
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
//We are using non property style for making dynamic table
final int j = i;
namecol = new TableColumn(rs.getMetaData().getColumnName(i+1));
namecol.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>()
{
@Override
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param)
{
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
tableview.getColumns().addAll(namecol);
System.out.println("Column ["+i+"] ");
}
while(rs.next())
{
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++)
{
//Iterate Column
row.add(rs.getString(i));
}
System.out.println("Row [1] added "+row );
data.add(row);
}
tableview.setItems(data);
conn.close();
Solution
You can retrieve the relevant field by calling the getter on the model object corresponding to the selected row.
In the code below the
newValue.getId()
call is the key.Without Java 8 lambdas:
With Java 8 lambdas:
Sample Code
Answers to Additional Questions
So in your update you can see that the type of each row's data is
ObservableList<String>
, whereas in my answer the type isIdentifiedName
. To get the posted solution to work for your data type, the change is trivial. The equivalent ofnewValue.getId()
would benewValue.get(0)
, to return the first item in your list for the selected row.Yes you could, but you would have to make extensive changes to your database fetching code to load the data created into an
IdentifiedName
class rather than anObservableList<String>
and doing so would lose the generic nature of your database loading code.You need to setup the type of your Table and columns correctly for your data types, not the sample ones I provided for my use case.
Replace these types:
With these types:
Minor advice
I advise taking a refresher by reading up on the Java Generics Trail. Tables in JavaFX are pretty complicated in their use of generics, but using correct generics in your table code can make it easier to write (as long as you are using a good IDE which is good at guessing the generics when needed).
You also might want to provide an minimal, complete, tested and readable example with future questions of this type (not all questions). Construction of one could help you solve your issues quicker. Also, ensuring your code has consistent indentation makes it easier to read.
You have to take a look at the underlying
ObservableList
. The code that works for me was:In my test that returns a
Person
object (POJO i wrote) which has to contain aget()
method.I can get first column value using below code:
Thanks..