I have a strange problem with JTable.
What I have
A two-column JTable. And the second column uses JComboBox as a cell editor.
Problem
When I edit that JComboBox cell with the origin column order it works fine. But When I change the column first, such as switching that 2 column(JComboBox column becomes fist), then I edited the JComboBox cell raising Bug. I must click that cell twice to fire the editing event, otherwise, JComboBox isn't activated.
What I tried
I tried to use TableCellListener, but it raises the exception, such as column index is -1;
My SSCCE:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.AbstractAction;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import java.awt.GridLayout;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
public class MainTable extends JDialog {
private static final long serialVersionUID = 156332386872772726L;
private final JPanel contentPanel = new JPanel();
private DefaultTableModel tableModel;
private JTable table;
public static void main(String[] args) {
try {
MainTable dialog = new MainTable();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public MainTable() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new GridLayout(1, 0, 0, 0));
{
JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane);
{
table = new JTable();
table.setAutoCreateRowSorter(true);
tableModel = new DefaultTableModel(new String[]{"first","second"},0);
table.setModel(tableModel);
{
//set comboBox to table
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addItem("Input");
comboBox.addItem("Output");
table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(comboBox));
table.getColumnModel().addColumnModelListener(new TableColumnModelListener(){
#Override
public void columnAdded(TableColumnModelEvent arg0) {
}
#Override
public void columnMarginChanged(ChangeEvent arg0) {
}
#Override
public void columnMoved(TableColumnModelEvent arg0) {
table.requestFocusInWindow();
comboBox.setFocusable(true);
comboBox.requestFocus();
comboBox.grabFocus();
comboBox.requestFocusInWindow();//try to get focus, not worked.
}
#Override
public void columnRemoved(TableColumnModelEvent arg0) {
}
#Override
public void columnSelectionChanged(ListSelectionEvent arg0) {
}
});
}
// TableCellListener tcl = new TableCellListener(table, new AbstractAction(){
// #Override
// public void actionPerformed(ActionEvent e) {
// }
// });
tableModel.addRow(new Object[]{"1","Input"});
tableModel.addRow(new Object[]{"2","Output"});//init. add 2 rows
scrollPane.setViewportView(table);
}
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("Add");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int row = tableModel.getRowCount();
tableModel.addRow(new Object[]{row,"Input"});//click button to add a row
}
});
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
}
}
}
I must click that cell twice to fire the editing event, otherwise, JComboBox isn't activated.
This only happens the first time you try to edit the column after changing column order. It will also happens if you switch the column back to its original position.
It is like the table doesn't have focus so the first mouse click is giving the table focus.
So you could try to add a TableColumnModelListener to the TableColumModel. Then in the columnMoved event you can try using table.requestFocusInWindow()
I am learning how to use JTable and I have issues with both my search and delete function.
From my codes, if I checked row 1 and row 3 and press the delete button, it will remove perfectly.
If I enter J in my search textfield and press the search button, John and Jane will be displayed
and now if I check the 2 rows and press the delete button then press the search button again,
John and Jane will be removed including Kate and Ann.
Please run the codes below if you guys still don't understand what I am saying.
follow the steps that I mentioned and you will see what's wrong.
help me. Thanks
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class TableExample extends JFrame {
private static final long serialVersionUID = 1L;
protected static final boolean DEBUG = false;
private JTable table;
TableModel model;
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("Delete");
JTextField searchTextField = new JTextField(15);
JButton searchBtn = new JButton("Search");
JPanel flowLayoutPanel = new JPanel(new FlowLayout());
TableRowSorter<TableModel> sorter;
public TableExample() {
String[] columnNames = {"Employer", "Company", "Salary", "Boolean"};
Object[][] data = {
{"Kate", "20",new Integer(5000), new Boolean(false)},
{"John", "35", new Integer(3000), new Boolean(false)},
{"Ann", "20", new Integer(4000), new Boolean(false)},
{"Jane", "12", new Integer(4000), new Boolean(false)},
{"May", "42", new Integer(4500), new Boolean(false)}
};
model = new DefaultTableModel(data, columnNames) {
#Override
public Class getColumnClass(int column) {
switch(column) {
case 0:
case 1: return String.class;
case 2: return Integer.class;
case 3: return Boolean.class;
default: return Object.class;
}
}
};
table = new JTable(model) {
public boolean isCellEditable(int row, int col) {
return true;
}
};
sorter = new TableRowSorter<TableModel>(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setRowSorter(sorter);
table.setGridColor(Color.black);
JScrollPane scrollPane = new JScrollPane(table);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for(int row = 0; row < table.getRowCount(); ++row) {
if((Boolean) table.getValueAt(row, 3) == true) {
((DefaultTableModel) model).removeRow(row);
row--;
}
}
}
});
searchBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = searchTextField.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter(text));
}
}
});
flowLayoutPanel.add(searchTextField);
flowLayoutPanel.add(searchBtn);
panel.add(flowLayoutPanel,BorderLayout.NORTH);
panel.add(scrollPane , BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
getContentPane().add(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TableExample frame = new TableExample();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
});
}
}
Since the row number changes when you filter the table. Use original row number to delete a correct row.
Use table.convertRowIndexToModel(row) to get the actual row number.
Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.
sample code:
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for(int row = 0; row < table.getRowCount(); ++row) {
if((Boolean) table.getValueAt(row, 3) == true) {
((DefaultTableModel) model).removeRow(table.convertRowIndexToModel(row));
row--;
}
}
}
});
Read more...
I have a problem here. I create program to add data to the table and sort it when i press the button. when i press the sort button once, it isn't wrong. but when i press again it is wrong. so why? please help me.
this is the code.
Nama Class
public class Nama {
private String nama;
private int index;
public Nama(String n,int i){
nama=n;index=i;
}
void setData(String n,int i){
nama=n;index=i;
}
String nama(){
return nama;
}
int ind(){
return index;
}
public String toString(){
return(String.format("%s %d", nama,index));
}
}
MergeSort Class
import java.util.*;
public class MergeSortS{
public void merge_sort(int low,int high,Nama [] a){
int mid;
if(low<high) {
mid=(low+high)/2;
merge_sort(low,mid,a);
merge_sort(mid+1,high, a);
merge(low,mid,high,a);
}
}
public void merge(int low,int mid,int high,Nama [] a){
int h,i,j,k;
Nama b[]=new Nama[50];
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high)){
if(a[h].nama().compareToIgnoreCase(a[j].nama())<0){
b[i]=a[h];
h++;
}
else{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid){
for(k=j;k<=high;k++){
b[i]=a[k];
i++;
}
}
else{
for(k=h;k<=mid;k++){
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
public MergeSortS() {
}
}
Panel Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
*
* #author Kareem
*/
public class Panel extends JPanel implements ActionListener{
JTable table;
JTextField tf1,tf2,tf3,tf4;
JButton b1,b2,b3,b4,b5,b6,b7;
Vector rows,columns,temp;
DefaultTableModel tabModel;
String[]data = new String[3];
String [] column = {"Nama", "Nim", "IP"};
Nama[] n,nim,ip;
MergeSortS mS;
int c=0,index=0;
public Panel(){
this.setBounds(0,0,1280, 800);
this.setLayout(null);
inaTf();
inaTab();
inaBut();
n= new Nama[50];
nim= new Nama[50];
ip= new Nama[50];
mS= new MergeSortS();
this.setVisible(true);
}
public void inaTab(){
rows=new Vector();
columns= new Vector();
temp= new Vector();
addColumns(column);
tabModel=new DefaultTableModel();
tabModel.setDataVector(rows,columns);
table = new JTable(tabModel);
table.setPreferredScrollableViewportSize(new
Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setBounds(100,100,200,200);
JScrollPane scroll = new JScrollPane(table);
scroll.setBounds(50,50,400,400);
add(scroll);
}
public void inaBut(){
b1=new JButton("add Row");
b1.setBounds(50,600,90,25);
add(b1);
b1.addActionListener(this);
b2=new JButton("Delete Row");
b2.setBounds(170,600,90,25);
add(b2);
b2.addActionListener(this);
b3=new JButton("SortName");
b3.setBounds(290,600,120,25);
add(b3);
b3.addActionListener(this);
b5=new JButton("SortNim");
b5.setBounds(290,650,120,25);
add(b5);
b5.addActionListener(this);
b6=new JButton("SortIP");
b6.setBounds(290,700,120,25);
add(b6);
b6.addActionListener(this);
b4=new JButton("RESET");
b4.setBounds(170,650,90,25);
add(b4);
b4.addActionListener(this);
}
public void inaTf(){
tf1=new JTextField();
tf1.setBounds(640,50,90,25);
add(tf1);
JLabel l1= new JLabel("Nama \t: ");
l1.setBounds(530,50,90,25);
add(l1);
tf2=new JTextField();
tf2.setBounds(640,80,90,25);
add(tf2);
JLabel l2= new JLabel("Nim : ");
l2.setBounds(530,80,90,25);
add(l2);
tf3=new JTextField();
tf3.setBounds(640,110,90,25);
add(tf3);
JLabel l3= new JLabel("IPK : ");
l3.setBounds(530,110,90,25);
add(l3);
tf4=new JTextField();
tf4.setBounds(640,140,90,25);
add(tf4);
JLabel l4= new JLabel("Hapus Baris ke ");
l4.setBounds(530,140,120,25);
add(l4);
}
public void addRow()
{
Vector r;
r = createBlankElement();
rows.addElement(r);
table.addNotify();
}
public void addRow(String [] data)
{
Vector r=new Vector();
r = isi(data);
rows.addElement(r);
table.addNotify();
}
public Vector createBlankElement()
{
Vector t = new Vector();
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
return t;
}
public Vector isi(String[] data) {
Vector t = new Vector();
for(int j=0;j<3;j++){
t.addElement((String) data[j]);
}
return t;
}
public void addColumns(String[] colName) {
for(int i=0;i<colName.length;i++)
columns.addElement((String) colName[i]);
}
void deleteRow(int index) {
if(index!=-1) {
rows.removeElementAt(index);
table.addNotify();
}
}
#Override
public void actionPerformed(ActionEvent e) {
try{
if(e.getSource()==b1){
data[0]=tf1.getText()+" "+index;
n[index]=new Nama(data[0],index);
data[1]=tf2.getText();
nim[index]=new Nama(data[1],index);
data[2]=tf3.getText()+rows.size();
ip[index]=new Nama (data[2],index);
c=c+1;
index=index+1;
addRow(data);
}
if(e.getSource()==b2){
int i;
i=Integer.parseInt(tf4.getText());
deleteRow(i);
// for(;i<rows.size();i++){
// n[i]=n[i+1];
// }
}
if(e.getSource()==b3){
mS.merge_sort(0, rows.size()-1, n);
temp.setSize(rows.size());
for(int i=0;i<index;i++){
temp.set(i, rows.get(n[i].ind()));
}
for(int i=0;i<index;i++){
rows.set(i, temp.get(i));
}
}
if(e.getSource()==b4){
rows.setSize(0);
temp.setSize(0);
for(int i=0;i<index;i++)n[i]=null;
index=0;
}
if(e.getSource()==b5){
mS.merge_sort(0, rows.size()-1, nim);
temp.setSize(rows.size());
for(int i=0;i<rows.size();i++){
temp.set(i, rows.get(nim[i].ind()));
}
for(int i=0;i<rows.size();i++){
rows.set(i, temp.get(i));
}
}
if(e.getSource()==b6){
mS.merge_sort(0, rows.size()-1, ip);
temp.setSize(rows.size());
for(int i=0;i<rows.size();i++){
temp.add(i, rows.get(ip[i].ind()));
}
for(int i=0;i<rows.size();i++){
rows.set(i, temp.get(i));
}
}
repaint();
}
catch (Throwable t){
JOptionPane.showMessageDialog(null, "AAAAAA");
}
}
}
Frame Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
*
* #author Kareem
*/
public class Frame extends JFrame {
public Frame(){
super("Penghitung Gaji");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(1280, 800);
this.getContentPane().add(new Panel());
this.setVisible(true);
}
public static void main(String[] args) {
Frame frame = new Frame();
}
}
Thanks. And Sorry for my bad english.
No where do you actually tell the TableModel that it's contents have changed...
Now, the thing that weirds me out, is you are keeping a Vector of the rows outside of the model...
mS.merge_sort(0, rows.size()-1, n);
temp.setSize(rows.size());
for(int i=0;i<index;i++){
temp.set(i, rows.get(n[i].ind()));
}
for(int i=0;i<index;i++){
rows.set(i, temp.get(i)); // This is doing nothing...
}
Once the data has been handed to the table model, you should not be interacting with it, unless you are willing to notify the table model of the changes.
Personally, I would simply use the inbuilt sorting capabilities of the JTable
ps- I would also, strongly, encourage you to learn how to use layout managers, they will save your sanity in the long run
Dashing through the snow
In a one-horse open sleigh
O'er the fields we go
Laughing all the way
Bells on bobtail ring'
Making spirits bright
What fun it is to ride and sing
A sleighing song tonight!
Jingle bells, jingle bells,
Jingle all the way.
Oh! what fun it is to ride
In a one-horse open sleigh.
Jingle bells, jingle bells, ....
song singing from code, please to apologize me if isn't this one your favorite song
can't comment or suggesting, excluding used LayoutManager in my code, I walked the path of least resistance, my endless lazyness
.
.
.
from code
.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowSorter.SortKey;
import javax.swing.ScrollPaneConstants;
import javax.swing.SortOrder;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
public class MyFrame {
private JFrame frame = new JFrame();
private JTable table;
private JPanel buttonPanel = new JPanel();
private JPanel buttonPanelSouth = new JPanel();
private JPanel textFieldPanel = new JPanel();
private JPanel northPanel = new JPanel();
private JPanel centerPanel = new JPanel();
private JLabel l1, l2, l3, l4;
private JTextField tf1, tf2, tf3, tf4;
private JButton b1, b2, b3, b4, b5, b6, b7;
private String[] columnNames = {"Nama", "Nim", "IP", "Hapus Baris ke"};
private Object[][] data = {
{"igor", "B01_125-358", "1.124.01.125", true},
{"lenka", "B21_002-242", "21.124.01.002", true},
{"peter", "B99_001-358", "99.124.01.001", false},
{"zuza", "B12_100-242", "12.124.01.100", true},
{"jozo", "BUS_011-358", "99.124.01.011", false},
{"nora", "B09_154-358", "9.124.01.154", false},
{"xantipa", "B01_001-358", "1.124.01.001", false},};
private DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public boolean isCellEditable(int row, int column) {
switch (column) {
case 3:
return true;
default:
return false;
}
}
#Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
public MyFrame() {
table = new JTable(model);
table.setAutoCreateRowSorter(true);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setFillsViewportHeight(true);
table.getSelectionModel().setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
DefaultTableCellRenderer stringRenderer =
(DefaultTableCellRenderer) table.getDefaultRenderer(String.class);
stringRenderer.setHorizontalAlignment(SwingConstants.CENTER);
JScrollPane pane = new JScrollPane(table,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
centerPanel.setLayout(new BorderLayout(10, 10));
centerPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
centerPanel.add(pane);
centerPanel.add(pane);
//
b1 = new JButton("add Row");
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
model.addRow(new Object[]{(tf1.getText()).trim(),
(tf2.getText()).trim(), (tf3.getText()).trim(), true});
}
});
b2 = new JButton("Delete Row");
b2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int rowToDelete = 0;
int rowToModel = 0;
if (table.getSelectedRow() > -1) {
rowToDelete = table.getSelectedRow();
rowToModel = table.convertRowIndexToModel(rowToDelete);
model.removeRow(rowToModel);
}
}
});
b3 = new JButton("RESET");
b3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
table.getRowSorter().setSortKeys(null);
}
});
b4 = new JButton("SortName");
b4.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
table.getRowSorter().toggleSortOrder(0);
}
});
b5 = new JButton("SortNim");
b5.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
TableRowSorter rowSorter = (TableRowSorter) table.getRowSorter();
List<SortKey> sortKeys = new ArrayList<SortKey>();
SortKey sortKey = new SortKey(1, SortOrder.ASCENDING);
sortKeys.add(sortKey);
rowSorter.setSortKeys(sortKeys);
rowSorter.sort();
}
});
b6 = new JButton("SortIP");
b6.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
TableRowSorter rowSorter = (TableRowSorter) table.getRowSorter();
List<SortKey> sortKeys = new ArrayList<SortKey>();
SortKey sortKey = new SortKey(2, SortOrder.DESCENDING);
sortKeys.add(sortKey);
SortKey sortKey1 = new SortKey(1, SortOrder.ASCENDING);
sortKeys.add(sortKey1);
SortKey sortKey2 = new SortKey(0, SortOrder.UNSORTED);
sortKeys.add(sortKey2);
rowSorter.setSortKeys(sortKeys);
rowSorter.sort();
}
});
b7 = new JButton("SortIP");
buttonPanel.setLayout(new GridLayout(1, 0, 50, 0));
buttonPanel.setBorder(new EmptyBorder(2, 10, 2, 10));
buttonPanel.add(b1);
buttonPanel.add(b2);
//
buttonPanelSouth.setLayout(new GridLayout(1, 4, 5, 5));
buttonPanelSouth.add(b3);
buttonPanelSouth.add(b7);
b7.setVisible(false);
buttonPanelSouth.add(b4);
buttonPanelSouth.add(b5);
buttonPanelSouth.add(b6);
centerPanel.add(buttonPanelSouth, BorderLayout.SOUTH);
//
l1 = new JLabel("Nama : ", JLabel.RIGHT);
tf1 = new JTextField();
l2 = new JLabel("Nim : ", JLabel.RIGHT);
tf2 = new JTextField();
l3 = new JLabel("IPK : ", JLabel.RIGHT);
tf3 = new JTextField();
l4 = new JLabel("Hapus Baris ke :", JLabel.RIGHT);
tf4 = new JTextField();
textFieldPanel.setLayout(new GridLayout(4, 2, 10, 10));
textFieldPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
textFieldPanel.add(l1);
textFieldPanel.add(tf1);
textFieldPanel.add(l2);
textFieldPanel.add(tf2);
textFieldPanel.add(l3);
textFieldPanel.add(tf3);
textFieldPanel.add(l4);
textFieldPanel.add(tf4);
//
northPanel.setLayout(new BorderLayout());
northPanel.add(textFieldPanel);
northPanel.add(buttonPanel, BorderLayout.SOUTH);
//
frame.add(northPanel, BorderLayout.NORTH);
frame.add(centerPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] arg) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MyFrame myFrame = new MyFrame();
}
});
}
}
I have a JTable which can have rows dynamically added by the user. It sits in a JScrollPane, so as the number of rows gets large enough, the scroller becomes active. My desire is that when the user adds a new row, the scroller moves all the way to the bottom, so that the new row is visible in the scrollpane. I'm currently (SSCCE below) trying to use a table model listener to detect when the row is inserted, and force the scrollbar all the way down when the detection is made. However, it seems this detection is "too early," as the model has updated but the new row has not actually been painted yet, so what happens is the scroller moves all the way to the bottom just before the new row is inserted, and then the new row is inserted just below the end of the pane (out of visibility).
Obviously this approach is wrong somehow. What is the correct approach?
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
public class TableListenerTest {
private JFrame frame;
private JScrollPane scrollPane;
private JTable table;
private DefaultTableModel tableModel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TableListenerTest window = new TableListenerTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TableListenerTest() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
JSplitPane splitPane = new JSplitPane();
frame.getContentPane().add(splitPane);
scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(100, 2));
splitPane.setLeftComponent(scrollPane);
tableModel = new DefaultTableModel(new Object[]{"Stuff"},0);
table = new JTable(tableModel);
scrollPane.setViewportView(table);
table.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.INSERT) {
JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
scrollBar.setValue(scrollBar.getMaximum());
}
}
});
JButton btnAddRow = new JButton("Add Row");
btnAddRow.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tableModel.addRow(new Object[]{"new row"});
}
});
splitPane.setRightComponent(btnAddRow);
}
}
EDIT: Updated SSCCE below based on trashgod's request. This version still does not work, however, if I move the scrolling logic from the table model listener to the button listener as he did, then it does work!
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
public class TableListenerTest {
private JFrame frame;
private JScrollPane scrollPane;
private JTable table;
private DefaultTableModel tableModel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TableListenerTest window = new TableListenerTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TableListenerTest() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
JSplitPane splitPane = new JSplitPane();
frame.getContentPane().add(splitPane);
scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(100, 2));
splitPane.setLeftComponent(scrollPane);
tableModel = new DefaultTableModel(new Object[]{"Stuff"},0);
table = new JTable(tableModel);
scrollPane.setViewportView(table);
table.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.INSERT) {
int last = table.getModel().getRowCount() - 1;
Rectangle r = table.getCellRect(last, 0, true);
table.scrollRectToVisible(r);
}
}
});
JButton btnAddRow = new JButton("Add Row");
btnAddRow.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tableModel.addRow(new Object[]{"new row"});
}
});
splitPane.setRightComponent(btnAddRow);
}
}
This example uses scrollRectToVisible() to (conditionally) scroll to the last cell rectangle. As a feature you can click on the thumb to suspend scrolling and release to resume.
private void scrollToLast() {
if (isAutoScroll) {
int last = table.getModel().getRowCount() - 1;
Rectangle r = table.getCellRect(last, 0, true);
table.scrollRectToVisible(r);
}
}
Addendum: I tried scrollRectToVisible in my SSCCE, and it still exhibits the same problem.
This Action provides both mouse and keyboard control:
JButton btnAddRow = new JButton(new AbstractAction("Add Row") {
#Override
public void actionPerformed(ActionEvent e) {
tableModel.addRow(new Object[]{"new row"});
int last = table.getModel().getRowCount() - 1;
Rectangle r = table.getCellRect(last, 0, true);
table.scrollRectToVisible(r);
}
});
Addendum: Here's a variation on your example that illustrates a revised layout strategy.
/** #see https://stackoverflow.com/a/14429388/230513 */
public class TableListenerTest {
private static final int N = 8;
private JFrame frame;
private JScrollPane scrollPane;
private JTable table;
private DefaultTableModel tableModel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
TableListenerTest window = new TableListenerTest();
window.frame.setVisible(true);
}
});
}
public TableListenerTest() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
tableModel = new DefaultTableModel(new Object[]{"Stuff"}, 0);
for (int i = 0; i < N; i++) {
tableModel.addRow(new Object[]{"new row"});
}
table = new JTable(tableModel) {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(200, table.getRowHeight() * N);
}
};
scrollPane = new JScrollPane();
scrollPane.setViewportView(table);
JButton btnAddRow = new JButton(new AbstractAction("Add Row") {
#Override
public void actionPerformed(ActionEvent e) {
tableModel.addRow(new Object[]{"new row"});
int last = table.getModel().getRowCount() - 1;
Rectangle r = table.getCellRect(last, 0, true);
table.scrollRectToVisible(r);
}
});
frame.add(scrollPane);
frame.add(btnAddRow);
frame.pack();
}
}
However, it seems this detection is "too early,"
For emphasis (#trashgod already mentioned it in a comment): that's true - and expected and a fairly general issue in Swing :-)
The table - and any other view with any model, not only data but also selection, adjustment, ... - is listening to the model to update itself. So a custom listener is just yet another listener in the line (with serving sequence undefined). If it wants to do something that depends on the view state it has to make sure to do so after all internal update is ready (in this concrete context, the internal update includes the update of the adjustmentModel of the vertical scrollBar) Postponing the custom processing until the internals are done, is achieved by wrapping SwingUtilities.invokeLater:
TableModelListener l = new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.INSERT) {
invokeScroll();
}
}
protected void invokeScroll() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
int last = table.getModel().getRowCount() - 1;
Rectangle r = table.getCellRect(last, 0, true);
table.scrollRectToVisible(r);
}
});
}
};
table.getModel().addTableModelListener(l);