Get Multiple raws selected in Jtable in java - java

Im triying to get all raws selected in table , I us the GetSelectedRaw() method to get the raw wich its selected by the user :
int raw_index = table.getSelectedRow();
but what if the user select more than one raw ??? I need to get all index of all raws selected in the table ...
I put onMoussPressed and onMoussReleased to the table :
int start_rows_to_delete; // the first selected raw
int end_rows_to_delete; // the last selected raw
private void tableMousePressed(java.awt.event.MouseEvent evt) {
start_rows_to_delete = table.getSelectedRow();
}
private void tableMouseReleased(java.awt.event.MouseEvent evt) {
end_rows_to_delete = table.getSelectedRow();
}
runing of prog. give me:
start_rows_to_delete = start_rows_to_delete !!!!!!
To more understande my goal , plz check this picture :
How do I do that? I googled a lot, but either I used the wrong keywords or there are no simple solutions on the internet. I hope somebody here can help me.
Best regards and thanks in advance, Fadel

From the JavaDocs
Returns the index of the first selected row, -1 if no row is selected.
Try using JTable#getSelectedRows, which will return an array of the selected row indicies
You may also want to take a look JTable#convertRowIndexToModel which will convert the view index to the model index which is useful when the table is sorted.
Instead of using a MouseListener you should use a ListSelectionListener which will let you know when the selection has changed, as the use may change the selection using the keyboard, which the MouseListener, obviously, won't tell you about.
Check out How to write a List Selection Listener for more details

Use the ListSelectionModel.
You can get it from a table Table.getSelectionModel()
The ListSelectionModel gives you the selected indexes. With them you can get the selected objects through the TableModel.

Related

Displaying just one value in JComboBox when one is selected

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.

Get all selected rows by shift click

If I have a JTable in java and if I click on the first row then shift+click lets say on the 10th row, how would get all the selected rows between 1 and 10... Is there a code for it? thanks in advance
how would get all the selected rows
Check out the JTable API. You can use the getSelectedRows() method to get the indexes of all the selected rows. Then you write a loop to iterate through the indexes to access the data that you need.
I believe what you're asking for is the method JTable.setSelectionMode(int), using as parameter ListSelectionModel.SINGLE_INTERVAL_SELECTION or possibly ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, to enable the selection of multiple contiguous rows. For instance:
JTable table = ...;
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

JTable model column

I have a JTable and a Model for that table.
Now I want to change the order of the columns and to hide or show some columns (e.g. like Windows Explorer in "Detail View" via Menu on rightclick).
My first problem here is, the getColumnName function. Do I have to keep track of, which column is at which place and then return the right columnName or is this already part of the model?
Same for the getValueAt function. Can I always return the value for the first column if I get columnIndex = 0, even if the user has dragged this column to the end of the table?
And nearly the same problem for adding/removing columns. If I do that, of course I have to fireTableStructureChanged, but do I also have to adapt e.g. the getColumnName function?
I haven't found a tutorial for that. All tutorials stop at "you can use a model".
I'd really like to see an example of such a dynamic model.
Thanks a lot.
You should use the getColumn(int) method of the model, and for accessing the model, you'll need to convert the row and column view indices with JTable's convertRowIndexToModel(int), convertColumnIndexToModel(int) and the equivalents for converting the model indices to view indices.
You need to understand the difference between the "View" and the "Model". When you reorder the columns in the JTable (view), this does not change the order of the data in the model.
If you want to access the first column that is displayed in the table you use:
table.getValueAt(row, 0);
if you want to access the first column in the model, then you use:
table.getModel().getValueAt(row, 0);
I want to hide or show some columns
See Table Column Manager.

How do i get the values of multiple selection from a JTable

Can someone please provide a sample code or at least a method that I can use to get the string values of multiple selections in a JTable? I searched the web, but I found only examples of how to get values from a single selection. Based on that I tried to implement the code myself using loops, but it blew up on my face. Any help will be greatly appreciated.
JTable has a method for that :
int[] selection = table.getSelectedRows();
Of course, this method returns the indices of the selected rows. You can use these indices to get the values you want from the the table model.

Java: JTable adding and moving columns

im quite new in Java.
I want to add Columns in a JTable at a specified index.
For this i am using addColumn(..) and then move them with moveColumn(...), this works great at the first time, but when i add another column it kind of moves also the other(before added columns).
Do you have any suggestions?
this is the code i've written in the TableModel is:
public void addColumn(Object columnName,
Vector columnData, JTable table) {
int moveTo = ((Integer)columnName);
boolean unselected = moveTo==-1;
super.addColumn(this.getColumnCount(), columnData);
if(!unselected) {//if a column was selected
table.moveColumn(this.getColumnCount()-1, moveTo+1);
}
}
this works great at the first time,
but when i add another column it kind
of moves also the other(before added
columns).
I don't understand what that means.
If you need more help post your SSCCE that demonstrates the problem.

Categories

Resources