Displaying a jtable starting from column 100 - java - java

If I have a jTable ( inside a jScrollPane ) with 1000+ columns. Is it possible when creating this jtable that the first column to be displayed is the 100th column ? all the previous columns could be seen by scrolling backwards. Thanks for any help.

Try
int columnToScrollTo = 100;
table.scrollRectToVisible(table.getCellRect(0, columnToScrollTo, true));

Related

Adding multiple rows to JTable

This is a pretty newbie question, but I'm having a difficult time adding multiple rows to a Jtable.
I have a program which reads data from a file that the user has searched for, and I would like to output it all on a table.
The problem is, whenever there is multiple items that match that search, the information doesn't come outputted all on one table, rather the popup menu comes up multiple times, each time displaying just one row on the Jtable, instead of giving me just one popup that will have all the rows in one Jtable.
Here is the code:
for (int x=0;x<data.length(); ++x)
{
if (database[4].equalsIgnoreCase(valenceInput)) //Search database for the number inputted by user
{
Object[][] rows = {
{database[0],database[1],database[2],database[3],database[4]}
};
Object[] cols = {
"Name","Symbol","Atomic Number","Atomic Mass", "# of Valence Electrons"
};
JTable table = new JTable(rows, cols);
JOptionPane.showMessageDialog(null, new JScrollPane(table));
found=true; //Booleon set to true to ensure desired output
break;
}
}
This is what the output comes up as:
http://i.imgur.com/RFkRtug.png
Directly after this, I will get another popup which will display another row of data that matched the search, I'm trying to figure out how to put all the rows on one Jtable instead of it showing me many each with just 1 row of data.
Build the TableModel within the for-loop and show the table once it's done...
DefaultTableModel model = new DefaultTableModel(
new Object[]{"Name","Symbol","Atomic Number","Atomic Mass", "# of Valence Electrons"},
0);
for (int x=0;x<data.length(); ++x)
{
if (database[4].equalsIgnoreCase(valenceInput)) //Search database for the number inputted by user
{
Object[] rows = {database[0],database[1],database[2],database[3],database[4]};
model.addRow(rows);
}
}
JTable table = new JTable(model);
JOptionPane.showMessageDialog(null, new JScrollPane(table));
See How to Use Tables for more details

jTable select only data where checkbox is chceck

Hi I have a problem with separate data in jTable.
My jTable:
When I click to Second button, so I have two data in jTable.
It's correct but when I click to First button again I get same jTable that as jTable in Second button.
This is my code:
DefaultTableModel model = new DefaultTableModel();
model = (DefaultTableModel) jTable1.getModel();
for(int i = 0; i < jTable1.getRowCount(); i++)
{
if(jTable1.getValueAt(i, 0) == Boolean.FALSE)
{
model.removeRow(i);
}
}
jTable2.setModel(model);
But it doesn't work. Is there anyone who tells me why ?
But it doesn't work. Is there anyone who tells me why ?
When you remove, for example, row 0, then all the rows shift down by one, but the value of "I" keeps increasing by one so you will "skip" rows every time you remove a row.
The solution is to start on the last row and decrement "I":
for(int i = jTable.getRowCount() - 1; i >=0; I--)
It's not a problem.
Yes it is. You posted example only works because you selected every other row in the table. Try selecting the first two rows and see what happens. Have said that, this is not the real solution to your problem, because you should NOT be removing data from the first TableModel.
It's correct but when I click to First button again I get same jTable that as jTable in Second button.
The problem is that you can't delete the data from the first TableModel. You need to create a new TableModel and then "copy" the data from the first TableModel to the second TableModel.

Java Swing JTable select programmatically multiple rows

I have a JTable with multiple rows and every row is presented via Point on a scatter plot. What I have to do is when a given point is selected on the scatter plot I have to associate this selection with selecting of the corresponding row in the JTable.
I have an Integer that represents, which row I have to highlight.
What I tried is:
JTable table = new JTable();
...
...// a bit of code where I have populated the table
...
table.setRowSelectionInterval(index1,index2);
So the problem here is that this method selects all rows in the given range [index1,index2]. I want to select for example rows 1,15,28,188 etc.
How do you do that?
To select just one row, pass it as both the start and end index:
table.setRowSelectionInterval(18, 18);
Or, if you want to select multiple, non-contiguous indices:
ListSelectionModel model = table.getSelectionModel();
model.clearSelection();
model.addSelectionInterval(1, 1);
model.addSelectionInterval(18, 18);
model.addSelectionInterval(23, 23);
Alternately, you may find that implementing your own subclass of ListSelectionModel and using it to track selection on both the table and the scatterplot is a cleaner solution, rather than listening on the scatterplot and forcing the table to match.
It also works without using the ListSelectionModel:
table.clearSelection();
table.addRowSelectionInterval(1, 1);
table.addRowSelectionInterval(15, 15);
table.addRowSelectionInterval(28, 28);
...
Just don't call the setRowSelectionInterval, as it always clears the current selection before.
There is no way to set a random selection with a one method call, you need more than one to perform this kind of selection
table.setRowSelectionInterval(1, 1);
table.addRowSelectionInterval(15, 15);
table.setRowSelectionInterval(28, 28);
table.addRowSelectionInterval(188 , 188 );
And So On....
Here is a generic method to accomplish this:
public static void setSelectedRows(JTable table, int[] rows)
{
ListSelectionModel model = table.getSelectionModel();
model.clearSelection();
for (int row : rows)
{
model.addSelectionInterval(row, row);
}
}

Limit rows shown in JTable + Next/Previous button

I'm trying to create a JTable that's only showing one row at the time. When you press Next/Previous, the table will display either the next or the previous row, depending on the button pressed. If there is no Next/Previous, the JTable shall display the first/last element.
Code for only showing one row at a time:
tableModel = new DefaultTableModel(data, columnNames);
tableModel.setRowCount(0); //If I remove this, the things I add ends up att the 2nd line.
tableModel.addTableModelListener(resultTable);
resultTable = new JTable(tableModel);
scroll = new JScrollPane(resultTable, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
final int rows = 1;
Dimension d = resultTable.getPreferredSize();
resultTable.setPreferredSize(new Dimension(d.width,resultTable.getRowHeight()*rows));
Code for Next-button (Not working, what do to? Shows the same element as before):
int height = resultTable.getRowHeight()*(rows-1);
JScrollBar bar = scroll.getVerticalScrollBar();
bar.setValue( bar.getValue()+height);
Code for Previous-button (Not working, same as above, nothing happens/changes.)
int height = resultTable.getRowHeight()*(rows-1);
JScrollBar bar = scroll.getVerticalScrollBar();
bar.setValue(bar.getValue()-height);
What do to? Some help would be greatly appreciated. Been trying to solve this forever.
Edit: Trying to follow the code from here: JTable row limitation but can't get it to work.. Though I only want to show 1 row, not 10.
From the above you specify row count to get single row in a table
final int rows = 2;
Alternatively you could try with table with Row header. Here is a sample
http://www.java2s.com/Code/Java/Swing-Components/TableRowHeaderExample.htm

Choose what rows are shown in JTable

I have a JTable and, inside its TableModel, I saved an int; this is the row index that I want my view to "keep in the middle" of the Viewport.
What I mean is that, for example, if I have 1000 rows, 10 are shown in the View at any time and my index is 500, I want my vieport to automatically show rows 495-504.
These rows are not necessarily selected.
I hope I was able to explain my problem properly.
Thank you
Assuming your row heights are the same,
int rowPosition = index - 10 / 2;
int pixelPosition = rowPosition * table.getRowHeight(rowPosition);
scrollPane.getViewPort().setViewPosition(new Point(0, pixelPosition));
If the row heights aren't the same, you would have to sum all of the row heights from row 1 to row index - 5.
1) JTable (notice can by caused by Filtering) returns number of rows
2) each of row can returns Rectangle
3) put this Rectangle to the JScrollPane#scrollRectToVisible(Rectangle aRect) wrapped into invokeLater(), simple example about moving JViewport to the decision row here

Categories