H
Haircuts Are Important
I attempted to modify 2 different projects on the Internet.
As you know, I have been trying to duplicate the Excel spreadsheet
freeze column capability while maintaining scrollability (vertical
across the 2 tables).
First, my problem with a modified version (added dynamically adding
rows to the 2 tables using "insert") of the code at
http://www.java2s.com/Code/Java/Swing-Components/FixedTableColumnExample.htm
produced code where the 2 tables wouldn't do exactly what I wanted.
Basically, I have been unable to get the the 2 tables to scroll
vertically together etc.
Second below, if I change the dimension of either JScrollPane it
changes the second one. So, the result is I either get a large left
JScrollPane or a small right JScrollPane.
I'd also like to remove any spacing between these 2 tables.
Third, I also tried to put each table into a separate JPanel and put
all this into a JScrollPane, but it (appeared to) yield the same
results as the second thing I tried.
What are the details to accomplish this.
Thanks,
class ParallelTables {
static JScrollPane createTableA() {
DefaultTableModel model = new DefaultTableModel(100, 1);
for (int row=model.getRowCount(); --row>=0 {
model.setValueAt(row, row, 0);
}
JTable table = new JTable(model);
return new JScrollPane(table);
}
static JScrollPane createTableB() {
DefaultTableModel model = new DefaultTableModel(100, 5);
for (int row=model.getRowCount(); --row>=0 {
model.setValueAt(row, row, 0);
}
JTable table = new JTable(model);
return new JScrollPane(table);
}
public static void main(String[] args) throws Exception {
JScrollPane scrollerA = createTableA();
JScrollPane scrollerB = createTableB();
scrollerA.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_NEVER);
// the following statement binds the same BoundedRangeModel to
both vertical scrollbars.
scrollerA.getVerticalScrollBar().setModel(
scrollerB.getVerticalScrollBar().getModel());
JPanel panel = new JPanel();
panel.add(scrollerA);
panel.add(scrollerB);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
As you know, I have been trying to duplicate the Excel spreadsheet
freeze column capability while maintaining scrollability (vertical
across the 2 tables).
First, my problem with a modified version (added dynamically adding
rows to the 2 tables using "insert") of the code at
http://www.java2s.com/Code/Java/Swing-Components/FixedTableColumnExample.htm
produced code where the 2 tables wouldn't do exactly what I wanted.
Basically, I have been unable to get the the 2 tables to scroll
vertically together etc.
Second below, if I change the dimension of either JScrollPane it
changes the second one. So, the result is I either get a large left
JScrollPane or a small right JScrollPane.
I'd also like to remove any spacing between these 2 tables.
Third, I also tried to put each table into a separate JPanel and put
all this into a JScrollPane, but it (appeared to) yield the same
results as the second thing I tried.
What are the details to accomplish this.
Thanks,
class ParallelTables {
static JScrollPane createTableA() {
DefaultTableModel model = new DefaultTableModel(100, 1);
for (int row=model.getRowCount(); --row>=0 {
model.setValueAt(row, row, 0);
}
JTable table = new JTable(model);
return new JScrollPane(table);
}
static JScrollPane createTableB() {
DefaultTableModel model = new DefaultTableModel(100, 5);
for (int row=model.getRowCount(); --row>=0 {
model.setValueAt(row, row, 0);
}
JTable table = new JTable(model);
return new JScrollPane(table);
}
public static void main(String[] args) throws Exception {
JScrollPane scrollerA = createTableA();
JScrollPane scrollerB = createTableB();
scrollerA.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_NEVER);
// the following statement binds the same BoundedRangeModel to
both vertical scrollbars.
scrollerA.getVerticalScrollBar().setModel(
scrollerB.getVerticalScrollBar().getModel());
JPanel panel = new JPanel();
panel.add(scrollerA);
panel.add(scrollerB);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}