Java: Index Select in ComboBox in a JTable - java

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.

Related

Selecting row and column of the Jtable by clicking the jCombobox that is inside a column of jtable

In my Jtable instance a column have the jComboBox , now what i want is to select the row and column of the cell once the value of selected jcombobox is changed.
If i use the actionPerformed event of the jcombobox, and get the jtable.getSelectedrow and column. System give me last selected row and column instead of current row and column.
Please guide me what to do .. thank you
private void jComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) jTable.getModel();
int selectedRow = jTable.getSelectedRow();
int selectedColumn = jTable.getSelectedColumn();
System.out.println("Row : " + selectedRow);
System.out.println("Column : " + selectedColumn);
}
now what i want is to select the row and column of the cell once the value of selected jcombobox is changed.
Don't add an ActionListener to the combo box, that is not the way an editor of the table was designed to be used.
Instead you should be adding a TableModelListener to the TableModel of the JTable. An event will be generated whenever data is changed in the table. The event will contain the row/column of the cell that was changed.
For a working example check out: TableModelListener and multiple column validation

Add values to existing JTable dynamically

I've created a JTable using following way. This table has 5 columns and 4 rows.
All 4 rows are empty in this status.
String[] columns = {"Emplotee ID","Name","Address","City","Salary"};
//Table that already have 4 empty rows
DefaultTableModel model = new DefaultTableModel(columns,4);
JTable detail = new JTable(model);
JScrollPane scroll = new JScrollPane(detail);
Now I want to add values into those empty rows using String array.
The GUI of this program has 5 JTextFields to get user input. When I enter values into JTextFileds and click Add button, all JTextFiled values get by String array named values.
String[] values = new String[6];
//When click addButton all textFiled data should go into table
addButton.addMouseListener(new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent evt){
values[0] = idField.getText(); //get JTextFields data into array
values[1] = nameField.getText();
values[2] = addressField.getText();
values[3] = cityField.getText();
values[4] = salaryField.getText();
//What now?
}
});
I know I can use this to add new row into the table. But this is not the feature what I want.
DefaultTableModel newModel = (DefaultTableModel)detail.getModel();
newModel.addRow(values);
Set contents of an existing table row with:
int row;
....
// make sure row is set to index of the table row
// you want to populate
for (int c=0; c<values.length; ++c)
detail.setValueAt(values[c], row, c);
TableModel also has setValueAt method, if you prefer.

Get selected cell value in JTable [duplicate]

This question already has answers here:
How to get cell value of jtable depending on which row is clicked
(2 answers)
Closed 7 years ago.
When I double click the cell of the JTable, I want it to take the value of that cell and write it in the textfield. What should I do? Here is what I have tried so far, but I don't know where to go from here:
table_1.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
JTable table = (JTable) me.getSource();
Point p = me.getPoint();
int row = table.rowAtPoint(p);
if (me.getClickCount() == 2) {
textfield.settext(???????????);
}
}
});
i understand how it works:
int row = table.rowAtPoint(p);
int column = table.columnAtPoint(p);
textfield.settext(table_1.getValueAt(row, column));
Jtable table = (JTable)e.getsource();
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
ObjectType o = (ObjectType)target.getValueAt(row, column) );
Do this. Will get the value in your JTable based on the row and column selected and then casts the returned value to your object type in the table and returns the value at the row, column. This is inside your Listener.
Shown in similar question Possible Dup?
You can get the value of the table by using:
table.getModel().getValueAt(row, col);
where
row - the row whose value is to be queried
col - the column whose value is to be queried
table - your object name (class jTable)
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.
In addition, I recommend to read this documentation.
Try to write something like this:
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(final MouseEvent e) {
if (e.getClickCount() == 1) {
final JTable jTable= (JTable)e.getSource();
final int row = jTable.getSelectedRow();
final int column = jTable.getSelectedColumn();
final String valueInCell = (String)jTable.getValueAt(row, column);
textfield.setText(valueInCell);
}
});

JavaFX TableView how to get cell's data?

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);

How to use hidden column data of jTable in tooltip

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

Categories

Resources