views:

2036

answers:

2

Hi,

I got a Java Swing GUI and got a problem with a JTable in a JScrollPane. For some reason, when the rows of the table model are increased during the program execution, the JScrollPane isn't updated - that is, if the rows are increased so that the height of the table is over the height of the scroll view, the scroll panes aren't updated as supposed. (The new rows are shown at the screen as expected). If the window is resized, scrolling is updated as expected.

The vertical scrolling policy is VERTICAL_SCROLLBAR_AS_NEEDED, table models fireTableDataChanged is called..

Unfortunately the code's a bit complex so I can't provide an code sample causing the problem. But thought to ask if somebody got some ideas straight..

EDIT: Still a bit more confusing : horizontal scrolling policy is HORIZONTAL_SCROLLBAR_AS_NEEDED, and if the table width if over the view width (that is, the horizontal scrollbar is used), this problem doesn't occur...

EDIT: The problem isn't that the table should be scrolled but that the scrollbar's aren't activated as they should.

+2  A: 

You might need to post some of your code. I've just knocked up the following test and it works as advertised, i.e. vertical scrollbars are activated when the number of rows exceeds the viewport height:

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.AbstractTableModel;

public class JTableTest {

public static void main(String[] args) {
 final MyTableModel tm = new MyTableModel();
 tm.addData(new Data("R1C1", "R1C2"));

 JTable table = new JTable(tm);
 JScrollPane scrollPane = new JScrollPane(table);
 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
 JFrame frame = new JFrame();
 frame.setLayout(new GridLayout());
 frame.add(scrollPane);
 frame.pack();
 frame.setSize(400, 150);
 frame.setVisible(true);

 Thread t = new Thread(new Runnable() {
  private int count = 2;
  public void run() {
   for ( ; ; ) {
    tm.addData(new Data("R" + count + "C1", "R" + count + "C2"));
    count++;
    try {
     Thread.sleep(2000);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 });
 t.start();
}

private static class MyTableModel extends AbstractTableModel {
 private List<Data> dataList = new ArrayList<Data>();

 public int getColumnCount() {
  return 2;
 }

 public void addData(Data data) {
  dataList.add(data);
  fireTableRowsInserted(dataList.size()-1, dataList.size()-1);
 }

 public int getRowCount() {
  return dataList.size();
 }

 public Object getValueAt(int rowIndex, int columnIndex) {
  Data data = dataList.get(rowIndex);
  return columnIndex == 0 ? data.data1 : data.data2;
 }
}

private static class Data {
 public String data1;
 public String data2;

 public Data(String data1, String data2) {
  this.data1 = data1;
  this.data2 = data2;
 }
}

}

MrWiggles
Thanks; last '}' was forgotten outside the code section. But yes, I'll think I got to work up some test version that does have the problem (or find out how to fix the bug)
Touko
A: 

Hmm.. after returning to the issue I found out that our row header customizing in JScrollPane was causing the problem. (Some preferredsizes were set with not-so-sensible values etc)..

Touko