Limit rows shown in JTable + Next/Previous button - java

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

Related

How to Adjust a JScrollPane based on the JTable's size?

I have a table right now that's displaying a list of classes and everything works fine except the formatting of it inside of the scroll pane. Here's what I have right now for it.
String[] columns = {"Class","Period","Size", "Honors/AP"};
JTable classTable = new JTable(new DefaultTableModel(new Object[][] {}, columns));
DefaultTableModel model = (DefaultTableModel)classTable.getModel();
//populating the table based on the number of classes that the teacher has
for (Class c: t.getClasses()) {
model.addRow(new Object[]{c.getClassName(),c.getPeriod(),c.getSize(),c.isHonorsAP()});
}
if (classTable.getRowCount()==0)
model.addRow(new Object[]{});
JScrollPane scrollPane = new JScrollPane(classTable);
classTable.setPreferredSize(new Dimension(700,100));
classTable.setPreferredScrollableViewportSize(classTable.getPreferredSize());
classTable.setFillsViewportHeight(true);
if (classTable.getPreferredSize().getHeight() < scrollPane.getPreferredSize().getHeight())
scrollPane.setPreferredSize(classTable.getPreferredSize());
So the problem on one end is that when there aren't enough rows to fill the preferred size of the table, it leaves extra space at the bottom and I'm trying to get that removed.
And then on the other end, when there are more rows that can fit, I need the size to stay but add in the scroll bar.
I'm really confused right now. Any help would be greatly appreciated

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 function getSelectedRow(), getSelectedColumn() returning -1

I am new in Java programming. I need to get the indices of selected column and row. I am getting -1 as selected indices for both the column and row. I have searched for a solution but didn't find anything satisfactory.
My code is following:
private void deleteProductButtonActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel tableModel = (DefaultTableModel) this.productDisplaTable.getModel();
JTable table = new JTable(tableModel);
int selectedRowIndex = table.getSelectedRow();
int selectedColIndex = table.getSelectedColumn();
System.out.println(selectedRowIndex );
System.out.println(selectedColIndex);
}
You're checking if a row is selected before the JTable has been displayed before the user can even interact with it.
Instead why not have that code in an ActionListener or some other listener so that the user at least has a chance to select something? This suggests that you might have a misunderstanding on how event-driven programming works and need to study the concepts a little bit more.
What makes you think that creating a a new JTable would have any selected rows or columns
JTable table = new JTable(tableModel); //???
Try using a table that is actually visible to the user instead
In your code you create a new JTable but you don't add this component to any container. Thus it won't never be visible and no row nor column could ever be selected.
Now, while we can add components dynamically in Swing we tipically place all our components before the top-level container (window) is made visible. In this case you should place the table when you initialize your components (don't forget the scroll pane) and do whatever you need to do when the button is pressed.
On the other hand I'm not sure what are you trying to achieve. I mean you already have a table called productDisplaTable. If you want to print the selected row and column in that table then make this little change:
private void deleteProductButtonActionPerformed(java.awt.event.ActionEvent evt) {
//DefaultTableModel tableModel = (DefaultTableModel) this.productDisplaTable.getModel();
//JTable table = new JTable(tableModel);
int selectedRowIndex = this.productDisplaTable.getSelectedRow();
int selectedColIndex = this.productDisplaTable.getSelectedColumn();
System.out.println(selectedRowIndex );
System.out.println(selectedColIndex);
}
Thanks all for taking time to reply.
I got the answer I was looking from #dic19's comment.
Now I clearly see the mistake I was doing. This was due to my lack of knowledge in Java programming.

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.

Displaying a jtable starting from column 100 - 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));

Categories