update jscrollpane / viewable area when click on JButton
Hi all,
I have a JTable inside a JScrollPane. I use some JButtons to do some row selections on the JTable.
I would like the viewable area / scroll pane to increment when the row selections are changed using the JButtons. By doing this I wish to see, at the bottom of the scrollpane the row that was selected by the JButton. (i.e. I want the behaviour you get if you select a row then use the arrow keys to move up and down the rows).
Below is a single class which sets up the table buttons etc. A lot of the length comes from the table's data so don't worry.
If you run it and click on the "Next team member" button you will see what it currently does.
package ui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
/*
* Class Window displays a frame in which there is a table and some buttons to
* manipulate the table. It is used as an example of manipulating table data.
*/
publicclass Windowextends JFrame
{
/*
* Data members.
* ========================================================================
*/
private JTable table;
private JScrollPane scroller;
privatefinalstaticint noButtons= 4;
privatefinalstatic String[] buttonNames=
{
"Pick for team","Unpick","Next team member","Next non-team member"
};
private JButton[] buttons=new JButton[noButtons];
/*
* End of Data members.
* ========================================================================
*/
/*
* Constructor.
* ========================================================================
*/
public Window()
{
setTitle("Testing table manipulation");
setSize(1024,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Make the table.
table= makeTable();
// Create the scroll pane.
scroller=new JScrollPane(table);
scroller.setSize(800,300);
// Create buttons to manipulate the data.
for (int i= 0; i < noButtons; i++)
{
buttons[i]=new JButton(buttonNames[i]);
buttons[i].addActionListener(new ButtonHandler(buttonNames[i]));
}
// A panel for the buttons.
JPanel buttonPanel=new JPanel();
buttonPanel.setSize(224,300);
for (int i= 0; i < noButtons; i++)
buttonPanel.add(buttons[i]);
// Add everything to the frame.
JPanel contentPane= (JPanel)this.getContentPane();
contentPane.add(scroller,BorderLayout.WEST);
contentPane.add(buttonPanel,BorderLayout.EAST);
// Make visible.
setVisible(true);
}
/*
* End of Constructor.
* ========================================================================
*/
/*
* Methods.
* ========================================================================
*/
// Makes a table.
private JTable makeTable()
{
// The table model.
TableModel tm=new TableModel();
// The actual table.
JTable jt=new JTable(tm);
// The table's selection model.
jt.getSelectionModel().addListSelectionListener(new RowSelectionListener());
return jt;
}
/*
* End of Methods.
* ========================================================================
*/
/*
* Inner classes
* ========================================================================
*/
/*
* Inner class TableModel manages the table model for the table in Window.
* This class contains the real data and methods to manipulate that data.
*/
privateclass TableModelextends AbstractTableModel
{
// Data.
Vector<String> columns;
Vector<Vector> rows;
public TableModel()
{
columns=new Vector<String>();
rows=new Vector<Vector>();
columns.add("Surname");
columns.add("Firstname");
columns.add("SquadNo");
columns.add("Position");
columns.add("In team?");
Vector<Object> v=new Vector<Object>();
v.add("McGeady");
v.add("Aiden");
v.add(46);
v.add("AM RLC");
v.add(true);
rows.add(v);
v=new Vector<Object>();
v.add("McGovern");
v.add("Michael");
v.add(47);
v.add("GK");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Zurawski");
v.add("Maciej");
v.add(7);
v.add("F C");
v.add(true);
rows.add(v);
v=new Vector<Object>();
v.add("McManus");
v.add("Stephen");
v.add(44);
v.add("D LC");
v.add(true);
rows.add(v);
v=new Vector<Object>();
v.add("Virgo");
v.add("Adam");
v.add(4);
v.add("D/F RC");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Feguson");
v.add("Barry");
v.add(6);
v.add("DM C");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Boyd");
v.add("Kris");
v.add(19);
v.add("S C");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Prso");
v.add("Dado");
v.add(9);
v.add("S C");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Gordon");
v.add("Craig");
v.add(1);
v.add("GK");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Pressley");
v.add("Steven");
v.add(4);
v.add("D RC");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Hartley");
v.add("Paul");
v.add(7);
v.add("AM RC");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Smith");
v.add("John");
v.add(10);
v.add("AM L");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Smith");
v.add("John");
v.add(10);
v.add("AM L");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Smith");
v.add("John");
v.add(10);
v.add("AM L");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Smith");
v.add("John");
v.add(10);
v.add("AM L");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Smith");
v.add("John");
v.add(10);
v.add("AM L");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Smith");
v.add("John");
v.add(10);
v.add("AM L");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Smith");
v.add("John");
v.add(10);
v.add("AM L");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Smith");
v.add("John");
v.add(10);
v.add("AM L");
v.add(false);
rows.add(v);
v=new Vector<Object>();
v.add("Smith");
v.add("John");
v.add(10);
v.add("AM L");
v.add(false);
}
// AbstractTableModel methods.
// The number of rows.
publicint getRowCount()
{
return rows.size();
}
// The number of columns.
publicint getColumnCount()
{
return columns.size();
}
// The value at row, column.
public Object getValueAt(int row,int column)
{
return (rows.elementAt(row)).elementAt(column);
}
// Allows the column names to be set.
// Also obtains the column name at column.
public String getColumnName(int column)
{
return columns.elementAt(column);
}
// Allows boolean columns to be displayed as checkboxes.
public Class getColumnClass(int columnIndex)
{
return getValueAt(0,columnIndex).getClass();
}
// Is the cell at row, column editable?
publicboolean isCellEditable(int row,int column)
{
returntrue;
}
// Allows the data to be changed.
publicvoid setValueAt(Object o,int row,int column)
{
(rows.elementAt(row)).setElementAt(o, column);
fireTableDataChanged();// Very important.
}
}
/*
* Inner class RowSelectionListener handles selection events on a Window
* instance's table rows.
*/
privateclass RowSelectionListenerimplements ListSelectionListener
{
publicvoid valueChanged(ListSelectionEvent e)
{
//Ignore extra messages.
if (e.getValueIsAdjusting())
return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (!lsm.isSelectionEmpty())
{
int selectedRow = lsm.getMinSelectionIndex();
String s="";
for (int i= 0; i < table.getColumnCount(); i++)
{
s+= (table.getValueAt(selectedRow, i)).toString() +" ";
}
System.out.println(s);// Would be appended to the textarea.
}
}
}
/*
* Inner class buttonHandler handles events from the buttons.
*/
privateclass ButtonHandlerimplements ActionListener
{
// Data members.
private String name;
// Constructor.
// Sets the name.
public ButtonHandler(String nm)
{
name= nm;
}
publicvoid actionPerformed(ActionEvent ae)
{
if (name.equals(buttonNames[0]))// "Pick for team"
{
changeStatus(true);
}
elseif (name.equals(buttonNames[1]))// "Unpick"
{
changeStatus(false);
}
elseif (name.equals(buttonNames[2]))// "Next team member"
{
int row;
if (table.getSelectionModel().isSelectionEmpty())
row= -1;
else
row= table.getSelectedRow();
// From next row until end of rows, look for correct value.
while (true)
{
for (int i= (row+1); i < table.getRowCount(); i++)
{
// The in team? column is the last one.
int inTeamColumn= table.getColumnCount()-1;
if (((Boolean)table.getValueAt(i,inTeamColumn)).booleanValue() ==true)
{
table.getSelectionModel().setSelectionInterval(i, i);
return;
}
}
// Go back to the start.
row= -1;
}
}
elseif (name.equals(buttonNames[3]))// "Next non-team member"
{
int row;
if (table.getSelectionModel().isSelectionEmpty())
row= -1;
else
row= table.getSelectedRow();
// From next row until end of rows, look for correct value.
while (true)
{
for (int i= (row+1); i < table.getRowCount(); i++)
{
// The in team? column is the last one.
int inTeamColumn= table.getColumnCount()-1;
if (((Boolean)table.getValueAt(i,inTeamColumn)).booleanValue() ==false)
{
table.getSelectionModel().setSelectionInterval(i, i);
return;
}
}
// Go back to the start.
row= -1;
}
}
}
// Changes whether they were picked or unpicked.
// picked = true means they were picked.
privatevoid changeStatus(boolean picked)
{
int row= table.getSelectedRow();
if (row < 0)
{
// Should be a dialogue (or do nothing?)
System.out.println("Error! No row was selected.");
}
else
{
for (int i= 0; i < table.getColumnCount(); i++)
{
if (table.getColumnName(i).equals("In team?"))
{
boolean boolVal= (Boolean)table.getValueAt(row, i);
if (boolVal == !picked)
table.setValueAt(picked, row, i);
}
}
// Reselect the row.
table.getSelectionModel().setSelectionInterval(row, row);
}
}
// Find the next one who has value picked.
privatevoid findNext(boolean picked)
{
}
}
/*
* End of Inner Classes.
* ========================================================================
*/
/*
* Test program.
* ========================================================================
*/
publicstaticvoid main(String[] args)
{
new Window();
}
/*
* End of Test program.
* ========================================================================
*/
}

