Re: Problems with JTable using fixed rows
--=-=-=
markspace <nospam@nowhere.com> writes:
Felix Natter wrote:
hi,
I have a special table setup in order to get fixed header and footer
rows and a scrollable middle part: Three JTable (header, data, footer)
in a vertical BoxLayout which share a common TableColumnModel.
Ideally, you should post a tiny example which does the same thing as your
program, i.e., throws the exception. It should be an SSCCE:
http://sscce.org/
I realize this may be difficult to construct if you have a lot of custom
code, but slimming your code down will have two advantages. One, you might
spot the error yourself. And two, we'll have a much better chance of
finding it for you if we have an example in front of us.
One row, maybe one or two columns, plus the header and footer, in a simple
JPanel in a simple JFrame, then the exception should happen. If you can
get that in less than 200 lines or so, we have a good chance of helping
you.
Thanks for the suggestion, the simple SSCCE is attached (I know Noel
posted one, but maybe it still is helpful).
The problem is definitely that the data table is wrapped in a
JScrollPane, and, as Noel pointed out, that the footer JTable
is _below_ the data table.
I tried some code from here (dummy scrollbars and such):
http://www.esus.com/docs/GetQuestionPage.jsp?uid=1267
but with no success.
many thanks for the help,
--
Felix Natter
--=-=-=
Content-Type: text/x-java
Content-Disposition: inline; filename=Test3.java
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class DataModel extends AbstractTableModel {
private Object[][] data;
private Object[] column;
public int getColumnCount() { return column.length; }
public int getRowCount() { return data.length; }
public String getColumnName(int col) {
return (String)column[col];
}
public Object getValueAt(int row, int col) {
// this gets called all the time!
System.out.println("getValueAt("+row+","+col+")");
return data[row][col];
}
public void setValueAt(Object obj, int row, int col) {
data[row][col] = obj;
}
public boolean CellEditable(int row, int col) {
return true;
}
public DataModel() {
data = new Object[][]{
{ "a","","","","",""},
{ "","b","","","",""},
{ "","","c","","",""},
{ "","","","d","",""},
{ "","","","","e",""},
{ "","","","","","f"}};
column = new Object[]{"A","B","C","D","E","F"};
}
}
class HFDataModel extends AbstractTableModel {
public int getRowCount() { return 1; }
public int getColumnCount() { return 6; }
public boolean isCellEditable(int r, int c) { return false; }
public Object getValueAt(int r, int c) {
return "HEADER/FOOTER";
}
}
public class Test3 extends JFrame {
JTable headerTable, dataTable, footerTable;
DataModel datamodel;
JPanel tablepnl;
JScrollPane tblSP;
public Test3() {
datamodel = new DataModel();
dataTable = new JTable(datamodel);
// the headerTable is not necessary to reproduce the behavior
//headerTable = new JTable(new HFDataModel(), dataTable.getColumnModel());
footerTable = new JTable(new HFDataModel(), dataTable.getColumnModel());
tblSP = new JScrollPane(dataTable);
tblSP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
tblSP.setVisible(true);
tablepnl = new JPanel();
tablepnl.setLayout(new BoxLayout(tablepnl, BoxLayout.Y_AXIS));
//tablepnl.add(headerTable);
tablepnl.add(Box.createRigidArea(new Dimension(0,20)));
tablepnl.add(tblSP);
tablepnl.add(Box.createRigidArea(new Dimension(0,20)));
tablepnl.add(footerTable);
add(tablepnl);
}
public static void main(String[] args) {
Test3 frame = new Test3();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}
--=-=-=--