I'm using that method to get whole object.
tableView.getSelectionModel().getSelectedItem()
How can I get data from single cell?
I mean like get S20BJ9DZ300266 as String?
Assuming you know something is selected, you can do
TablePosition pos = table.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
// Item here is the table view type:
Item item = table.getItems().get(row);
TableColumn col = pos.getTableColumn();
// this gives the value in the selected cell:
String data = (String) col.getCellObservableValue(item).getValue();
Assuming you haven't selected any row but know where and what you want....
// Item here is the table view type:
Unpaid item = userTable.getItems().get(0);
TableColumn col = userTable.getColumns().get(3);
// this gives the value in the selected cell:
String data = (String) col.getCellObservableValue(item).getValue();
JOptionPane.showMessageDialog(null, data);
table.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) {
selected.setText("");
return;
}
System.out.println(newValue.getId());
});
NB: "getId" is your "get" Column and "table" is the name of your tabelview
I solved it. I get the string, for example, "[1, proveedor1, calzado1, Rojo, 39, 600, 2, 1200]", and get the frist cell (id) whit substring. saludos
TablePosition pos = (TablePosition) tableVenta.getSelectionModel().getSelectedCells().get(0);
int index = pos.getRow();
String selected = tableVenta.getItems().get(index).toString();
selected = selected.substring(1, selected.indexOf(","));
System.out.println(selected);
If you are using scene builder, add a method to On Edit Commit for the particular column and add the logic in the controller class. Use event.getNewValue() to get the new value entered by the user. Eg.
#FXML
private void UpdateName(TableColumn.CellEditEvent<ModelClass, String> event) {
ModelClass mc = Table.getSelectionModel().getSelectedItem();
mc.setName(event.getNewValue());
System.err.println("new value "+event.getNewValue());
}
Use this when you allow editable columns
you can use this code i think it's working
String a=personTable.getColumns().get(0).getCellObservableValue(0).getValue().toString();
System.out.println("value"+a);
Related
Hey I have a JTable that has a Combobox as a cellEditor. I have values in that Table and I added a Combobox and I need to have the value in the cell be the selected index of the combobox.
DefaultTableModel tableModel = new DefaultTableModel(rows,columes);
//Select combobox values
Object[] string = (Object[]) sqlSTypes.executeSqlSelectOneDimension(sql);
if(string != null) {
comboBoxtypes = new JComboBox<Object>(string);
}
if( comboBoxtypes != null) {
dealPositionsTable.getColumnModel().getColumn(3).setCellEditor((TableCellEditor) new DefaultCellEditor(comboBoxtypes));
}
I'm not sure if you are asking how to add rows of a comboBox in your code. I'm assuming you have figured out how to add the row and the row value that you want, based off of your cell data. Next you'll need to set the selected index of the comboBox.
Once you get the value of the cell use:
setSelectedIndex( your index here )
Alternatively you could use:
setSelectedItem( your item here )
If you know the value of the row in the comboBox.
I want to edit a specific cell in a TableView using JavaFX (for example, row 3, column 5).
I tried this code but it doesn't work.
//Define the string
String s = "myString";
//Define the number
int value = 5;
//Synthesize the item = row
Item item = new Item(s, value);
//Set the i-th item
table.getItems().set(i, item);
Is this what you need? In your example you do not mention any position but the question title does.
table.getTableView().getItems().get(
t.getTablePosition().getRow())
).setLastName(t.getNewValue()
You can find here a complete example.
Just edit the list you are providing to the table to what you want and just call
yourtable.refresh();
I need a way to get the user selection every time a user selects an item on a TableView, even if the item is already selected.
The tableView.getSelectionModel().selectedItemProperty().addListener works when the user selects a different item from the one highlighted, but if the user selects the highlighted item again, it doesn't seem to work.
How would this be fixed?
you can do this:
tableView.setOnMouseClicked((MouseEvent event) -> {
if(event.getButton().equals(MouseButton.PRIMARY)){
System.out.println(tableView.getSelectionModel().getSelectedItem());
}
});
the code above doesn't work if you select the highlighted item again using editable table cell
If you're only interested in the clicks on the rows, use a custom rowFactory:
TableView<Item> table = ...
EventHandler<MouseEvent> clickListener = evt -> {
TableRow<Item> row = (TableRow<Item>) evt.getTarget();
if (!row.isEmpty()) {
// do something for non-empty rows
System.out.println("you clicked " + row.getItem());
}
};
table.setRowFactory(tv -> {
TableRow<Item> row = new TableRow<>();
// add click listener to row
row.setOnMouseClicked(clickListener);
return row;
});
The simplest way what i know:
yourTableView.setOnMousePressed(e ->{
if (e.getClickCount() == 2 && e.isPrimaryButtonDown() ){
int index = yourTableView.getSelectionModel().getSelectedIndex();
System.out.println("" + index);
}
});
put it in to constructor or initialize method in your corntroller class... :)
I am trying to display the data of hidden column as tooltip. Hiding is working perfectly using the following code:
JTable table = new JTable(model){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
int realColumnIndex = convertColumnIndexToModel(colIndex);
try {
tip = getValueAt(rowIndex, 8).toString();
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}
return tip;
}
};
TableColumnModel tcm = table.getColumnModel();
TableColumn tc;
for(int i = 1; i <= 7; i++){
tc = tcm.getColumn(8);
tcm.removeColumn(tc);
}
But the tooltip is not showing the data of hidden column (getValue function is not returning value). So do hiding the column delete the data as well ?
You do not need to for loop as you do not use the i variable ;-)
The removeColumn on the JTable does not remove the data from the model, as clearly stated in the javadoc
Removes aColumn from this JTable's array of columns. Note: this method does not remove the column of data from the model; it just removes the TableColumn that was responsible for displaying it.
There is no mention in the javadoc for the same method on the TableColumnModel, but I would assume it works the same way, but you can always give it a try to call it on the JTable instead
The real problem in your code is the use of getValueAt, which uses the row and column index of the table, and not of the model
Note: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering.
And since you removed that column, it simply does not exists for the table. Call the getValue method on the model instead, and do not forget to convert the row index
Despite a lot of research I can't find an answer or solve how to get the selected text element inside a JList to a variable. Therefore I would preciate some help. I have tried to select the index of the selected element and removed elements with this code and that works fine, but as I wrote I want the selected text to a variable after pressing a button. Thanks!
int index = list.getSelectedIndex();
model.removeElementAt(index);
Parts of my JList code:
model = new DefaultListModel();
list = new JList(model);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(430, 60));
Parts of my actionlistener code:
// Select customer
if(event.getSource() == buttonSelectCustomer){
int index = list.getSelectedIndex(); // Just for test
model.removeElementAt(index); // Just for test
int number = model.getSize(); // Just for test
//String selectedText = list.getSelectedValue(); // Not working!
}
Use the ListModel#getElementAt(int) method with the currently selected index. If you are certain your model only contains String instances, you can directly cast it to a String as well
You can't get the selected text because you try to get it after you have removed the selected element.
you can change your code:
if(event.getSource() == buttonSelectCustomer)
{
int index = list.getSelectedIndex(); // Just for test
model.removeElementAt(index); // Just for test
int number = model.getSize(); // Just for test
String selectedText = list.getSelectedValue(); // Not working!
}
to my code:
if(event.getSource() == buttonSelectCustomer)
{
String selectedText = (String)list.getSelectedValue(); // it works
int index = list.getSelectedIndex(); // Just for test
model.removeElementAt(index); // Just for test
int number = model.getSize(); // Just for test
}
then it works.
It's easy to retrieve the item of selected index.
Here is a simple code-snippet:
String[] string = new String[]{"Hello","Hi","Bye"};
JList list = new JList(string);
Now use the following code to get the selected item as a string:
String item = list.getSelectedIndex().toString();