My aim is to provide an interface like a matrix, each cell in matrix will have 2 values and user will select best among the two.
So i thought of going for jTable and combo boxes, the problem is in my matrix the value of each cell in a column is different. But the following code that adds the combo box to whole column of the table and if i change the combo box value, it changes for the whole table. How to insert combo boxes into the table such that each one has different values
javax.swing.JComboBox k = new javax.swing.JComboBox();
k.addItem("1");
k.addItem("2");
k.addItem("3");
k.setEnabled(true);
k.setVisible(true);
this.jTable1.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(k));
I also tried with DefaultTableModel
code is
DefaultTableModel t =new javax.swing.table.DefaultTableModel();
t.setColumnCount(10);
t.setRowCount(10);
t.setValueAt(k, 0, 0);
jTable1.setModel(t);
but i get the output in the gui as
javax.swing.JComboBox[,0,0,0x0,invalid,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=16777544,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=1]
I tried typecasting "k" as JComboBox and JComponent in setValueAt method, which didn't work
Someone please help
Override the getCellEditor(....) method. For example: How to add unique JComboBoxes to a column in a JTable (Java)
Related
I'm using A JCombobox with editable = true in a JPanel and using a JTable in the same panel with a column that is set to show combobox as its field type. I applied
AutoCompleteDecorator.decorate(cb);
to JCombobox that is outside the JTable and its working perfectly But when I applied the same line of code to combobox within jtable which selects the first occurrence of the data that match the key typed.
How can I resolve this issue. Any Suggestion ?
Look at the image below in which the exact item is selected that I typed.
And this is the image of combobox within JTable.
When I press w key it select the first occurrence windy and set it in the cell.
You haven't posted a code, but have you checked which TableCellEditor you use?
You might have used DefaultTableCellEditor. Change it to ComboBoxCellEditor if that's the case.
example:
JCombobox cmbItems = new JComboBox();
cmbItems.addItem("choice1");
cmbItems.addItem("choice2");
cmbItems.addItem("choice3");
AutoCompleteDecorator.decorate(cmbItems);
TableColumn colWithCmb = table.getColumnModel().getColumn(0);
colWithCmb.setCellEditor(new ComboBoxCellEditor(cmbItems));
Ok I have a JTable where I'm displaying a JList on every cell. To do this I had to implement TableCellRenderer and extend DefaultCellEditor. Here is where I return the actual JList to be rendered in GUI.
What I want to do is when user de-selects an item from a JList, I want to also de-select all items for all JLists for that table row starting at the clicked column.
My problem is that I can't figure out a way to de-select all items that come after the current clicked table column. All I can access is this DefaultListModel. I guess I need to access the actual JList in order to remove all selected items.
Below is method I'm using. Any ideas how to do this? Thanks.
public void deselectFromLocation(int row_, int column_){
DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
int cols = dtm.getColumnCount();
for(int i=column_; i<cols;i++){
PCSListModel lm = (PCSListModel) dtm.getValueAt(row_, i);
//How can I access the actual JList object in order to remove all selected items?
//The PCSListMode is DefaultListModel and has no access to JList object. Thanks.
}
}
Presumably, your renderer and editor obtain the existing selection state from your TableModel, perhaps updating an instance of ListSelectionModel that is used as part of preparing the component for use. You can update the other model value(s) in your implementation of stopCellEditing(). Your TableModel will have to fire a suitable TableModelEvent for the other cells; do not do so for the value being edited. A related example is seen here.
I have a very odd issue with JTable.
I put data in JTable from DB. When user double clicks on any cell, it copies the cell contents of the first column of the row where user double clicked. So far, it works perfect.
The problem arises when the user sorts the JTable by clicking on the header. when the table has been sorted and now when the user double clicks on any row, it doesn't what is currently stored on that row's first column. It copies what was originally stored on the first column of that row when the JTable was not sorted.
Any ideas?
Problem:
The problem here is that you are getting the initial rows indexes in the JTable TableModel, and not the relevants row indexes shown in the table view.
Solution:
You can map the shown indexes of a sorted jTable with their relevants ones in the dataModel using convertRowIndexToModel(index) method which takes in input the row index in the view and returns the index of the corresponding row in the model.
Let's say that you have the following jTable:
TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
table.setRowSorter(new TableRowSorter(myModel));
And then loop throught model indexes and use this method with each index to get its corresponding one in the TableModel:
table.getRowSorter().convertRowIndexToModel(0); // index 0 here
As suggested in How to Use Tables: Sorting and Filtering, "When using a sorter, always remember to translate cell coordinates." Most likely, you have neglected this in your event handler.
Try Sorting your JTable TableModel data too. Jtable -> TableModel is the one which hold the actual data. JTable is just a view.
I have created a JComboBox and I am using it on a column of a JTable - see code below.
Object[] items = {"1", "2", "3", "4"};
JComboBox comboBox = new JComboBox(items);
comboBox.setRenderer(new customComboBoxRenderer());
myTable.getColumnModel().getColumn(Table.CONF_COL).setCellEditor(new DefaultCellEditor(comboBox));
Now, my problem is that I cannot find a way to make each combo box in the column of the table with the combo boxes to have an initial value (might be different on each combo box) which will also be the selected item of the particular combo box when the program starts.
I think the problem is being created because the JTable is using only one JComboBox instance for all the cells of the column and therefore I can never get a different instance of combo box for each of the cells on the column and set the selected item to them.
Any help would be really appreciated.
Many thanks,
Soc
read Oracle tutorial How to use Tables - Using a Combo Box as an Editor for working code example
everything depends of value stored in DefaultComboBoxModel, in your case I'd be store Integer not String value for JComboBoxes model
value stored in XxxTableModel representing selected or inital value in JComboBox as TableCellEditor,
there you can store Integer value too, to avoiding any parsing between String and Integer
simple use (JTable or XxxTableModel).setValueAt to display desired value in JTable (painted by TableCellRenderer)
more info about TableCellRenderer and TableCellEditor in Oracle tutorial
In your customComboBoxRenderer
call setSelectedItem(same type as your jcombobox elements, in this case String)
inside your
getTableCellRendererComponent(.. ) method
I have a JTable with Customized CellRenderer and CellEditor, Intially the table is loaded with
a list of values Say with 12 rows and 5 colums, I have a JTextField at the top of the table in which I applied KeyListener and made the Textfield to display like a JComboBox with a list of values as soon as first 3 characters typed in that field, eg. Typing 'met' will display all the medicine names starting with "met", now what I want to do is I have to Implement that Textfield into the Jtable's last row's 2nd column Say 13th row in the situation I mentioned above. and after selecting any 1 medicine from the list of displayed value the JTable could Add a row dynamically and insert a new row with that search textfield, Please Suggest me an Idea and Code for this, also guide me how to apply cellrenderer and celleditor for a particular cell(Cell which contains the dynamic search textfield)...
Thanks a lot in Advance :)
Kindly let us assume jTable2 be your JTable variable name and TextField be your JTextField variable name. Then use the following code with the keylisterner of the text field to get what is wanted:-
javax.swing.table.DefaultTableModel dft= (javax.swing.table.DefaultTableModel)
jTable2.getModel();
jTable2.setModel(dft);
dft.addRow(new Object[1]);
jTable2.setValueAt(TextField.getText(),jTable2.getRowCount()-1,1);