I have created a JTable where the included items are provided with radio buttons in order to enable users to select any of these once at a time. In order to show always the first item selected when the JTable is initialised (or when the item previously selected has been deleted) what method should I use?
You should see the JTable Javadoc and particularly at :
getCellRect(int row, int column, boolean includeSpacing)
and
scrollRectToVisible(Rectangle aRect)
Something like
table.scrollRectToVisible(table.getCellRect(table.getSelectedRows()[0], 0, true));
should suit you.
Related
I have a JTable whose every cell contains a JList. What I want to do is to make that table editable so that every item on the JList can be edited either using a JTextfield or Choosing an item from another List whenever the user right clicks on that list item in a particular table cell. I also want 2 of my table column need to be set uneditable. Here is a picture of my JTable. I want the every cell and every JList item on that cell to be editable leaving the 'Batch' and 'Break' column to be uneditable.
P.S. I don't want any spoon fed code. I just want an idea how it can be done. And I'll be really grateful if you can reference me some link on the web where I can read to learn how this type of problems can be solved. Thank you..!
enter image description here
Hello :) I need help with changing a JComboBox in a JTable. I'm new to GUI-Programming and Swing and couldn't solve this problem: I have to change the behavior of a JComboBox.
You can see the ComboBox in the picture below. If "Ja" is selected there should just be "Nein" as an option and the other way around. It would also be cool if "Nein" is set per default. The code was written from one student from last semester and I have difficulties to adjust the combobox like I have to.
That's the code snippet where the ComboBox gets initialized.
optionsInteger = new JComboBox<String>();
optionsInteger.addItem("Ja");
optionsInteger.addItem("Nein");
optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
optionsInteger.setSelectedIndex(1);
optionsInteger.setName("optionsInteger");
The ComboBox gets inserted to a JTable in this method:
public void repaintXTable(DefaultTableModel model,JTable table, int xAmount, JScrollPane scrollPane,
JComboBox<String> optionsInteger) {
model.setRowCount(xAmount);
th = table.getTableHeader();
tcm = th.getColumnModel();
tcs = tcm.getColumns();
tcs.nextElement().setHeaderValue("");
tcs.nextElement().setHeaderValue("Lower");
tcs.nextElement().setHeaderValue("Upper");
tc = tcs.nextElement();
tc.setHeaderValue("Integer");
tc.setCellEditor(new DefaultCellEditor(optionsInteger));
for(int i=0;i<xAmount;i++)
{
model.setValueAt("X"+(i+1), i, 0);
}
}
Thank you very much for your help.
In your code, this line
optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
sets the default selection to the zeroth element (Ja). This line
optionsInteger.setSelectedIndex(1);
sets the default selection to the first element (Nein).
Either set the selected item or the selected index. There's no need to do both.
A JComboBox does not remove the selected element by default. If the selected element is removed, how would the selected element display in your JTable?
If you really want to do this, you'll have to create your own version of a JComboBox.
. If "Ja" is selected there should just be "Nein" as an option and the other way around.
So then you need two separate ComboBoxModels, one model containing "Nein" and the other containing "Ja". Then when you start to edit the cell you check the current value and use the model containing the other value.
Check out How to add unique JComboBoxes to a column in a JTable (Java). This example shows how you can dynamically change the model at the time the cell is about to be edited.
It would also be cool if "Nein" is set per default.
This has nothing to do with the editor. You just add "Nein" to the TableModel when you add the data to the model.
I have JTable with ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
When I use CTRL+mouse click, rows are selected and all are right. But if I want to deselect only one selected row and press CTRL+mouse click to one selected row, all rows are deselected, not only one. Then if I press CTRL+mouse click to some row again, previouse rows are selected again, but without row, that I wanted to deselect.
I want to deselect only one row using CTRL+mouse click. How can I do that?
EDIT:
I had this in my code:
table.getColumnModel().setColumnSelectionAllowed(true);
I found that if I remove this line then all works fine. But can JTable work correctly although 'ColumnSelectionAllowed' is true?
Try method clearSelection on JTable .
it works by calling clearSelection on the ListSelectionModel
If you are looking for deselection one row from many, use the below code:
table.getSelectionModel().removeSelectionInterval(rowFrom, rowTo);
for deselecting one row:
table.getSelectionModel().removeSelectionInterval(row, row);
you can use multiple selection like this
table.getSelectionModel().removeSelectionInterval(1, 3); //1,2,3 starting from 0
table.getSelectionModel().removeSelectionInterval(5, 5); //5
to add it back, use:
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);
you can use multiple selection like this
table.getSelectionModel().addSelectionInterval(1, 2); //1,2
table.getSelectionModel().addSelectionInterval(4, 4); //4
to set it to specific range, which will clear your old selection first,
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);
Which it is equal to:
table.clearSelection();
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);
In Swing is possible to create a JComboBox in a JTable, as seen by this guide from Oracle. They have a lovely picture that shows this in action:
However, what the fail to show is that if you haven't clicked on the cell, the dropdown arrows are not visible and it just looks like a normal text label, as seen below:
You can see that knitting has dropdown arrows because I just clicked on it, but the other ones don't. This is sadly less than ideal because there are no visual cues that the cell can be clicked on to show a list of options. In other words, the "Sport" column looks identical to the "Last Name" column. One of them is a dropdown, the other is not, but they visually look the same unless you happen to click on one of them.
Is there any way that this can be done in Swing?
EDIT: To clarify, what I want is for ALL the cells in the "Sport" column to have arrows indicating a dropdown menu, even if they were not the least one clicked. Basically, I want it to look like a combo box whether I've clicked on it or not.
I'm not sure you understand the distintion between "renderer" and "edit" modes in the JTable. All the cells in the Sport column in your example are backed by a combobox, when in edit mode.
What I believe you're trying to do is something like...
Which will clutter the UI (IMHO)
So based on the example from here, I modified the code to change the default cell renderer for the Sport column
public void setUpSportColumn(JTable table,
TableColumn sportColumn) {
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement("Snowboarding");
model.addElement("Rowing");
model.addElement("Knitting");
model.addElement("Speed reading");
model.addElement("Pool");
model.addElement("None of the above");
comboBox.setModel(model);
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
model = new DefaultComboBoxModel();
model.addElement("Snowboarding");
model.addElement("Rowing");
model.addElement("Knitting");
model.addElement("Speed reading");
model.addElement("Pool");
model.addElement("None of the above");
//Set up tool tips for the sport cells.
ComboBoxTableCellRenderer renderer
= new ComboBoxTableCellRenderer();
renderer.setModel(model);
sportColumn.setCellRenderer(renderer);
}
And added this...
public class ComboBoxTableCellRenderer extends JComboBox implements TableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setSelectedItem(value);
return this;
}
}
First of all, you may want to check this out: how to add checkbox and combobox in table cell?
In your example, I am thinking that this may be due to the fact that there is not enough height to display the GUI. What I mean is that the Swing components automatically resize to fill their container, and if the container height is too small, then it may not display the GUI correctly.
Here are images to illustrate my example (I used Windows XP):
Initial Launch:
Click on Cell:
After Cell Click:
I want to make a whole Row editable...e.g if user selected column 1 than click on edit button the table will show the whole row in editable form...it will be great if any one can give only code upon button event.
I want to make a whole Row editable...e.g if user selected column 1
than click on edit button the table will show the whole row in
editable form...it will be great if any one can give only code upon
button event.
I think have to use JToggleButton, because (JButtons) simple click is immediatelly finalized in comparing with JToggleButton (has two states)
for JTable have to set proper SelectionMode and to override public boolean isCellEditable(int row, int col) {