Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #4922
| From | "John B. Matthews" <nospam@nospam.invalid> |
|---|---|
| Newsgroups | comp.lang.java.gui |
| Subject | Re: JTable, JScrollPane and empty space |
| Date | 2012-01-11 07:47 -0500 |
| Organization | The Wasteland |
| Message-ID | <nospam-E77B48.07475111012012@news.aioe.org> (permalink) |
| References | <IdydnSRiIqV1LpHSnZ2dnUVZ_v2dnZ2d@megapath.net> |
In article <IdydnSRiIqV1LpHSnZ2dnUVZ_v2dnZ2d@megapath.net>,
"A. W. Dunstan" <no@spam.thanks> wrote:
> I'd be more than happy to switch to some other layout manager if
> that's needed.
One approach is to use BoxLayout: use setPreferredScrollableViewportSize
as desired and restrict the maximum size of the smaller table's scroll
pane.
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TablePanel extends JPanel {
public static final int WIDE = 300;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("TablePanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TablePanel());
f.pack();
f.setVisible(true);
}
});
}
public TablePanel() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JTable tableA = new JTable(new MyModel("A", 12));
int h = tableA.getPreferredSize().height;
tableA.setPreferredScrollableViewportSize(
new Dimension(WIDE, h));
JScrollPane jspA = new JScrollPane(tableA);
jspA.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jspA.setMaximumSize(new Dimension(Short.MAX_VALUE, h));
this.add(jspA);
JTable tableB = new JTable(new MyModel("B", 32));
h = tableB.getPreferredSize().height;
tableB.setPreferredScrollableViewportSize(
new Dimension(WIDE, h / 2));
final JScrollPane jspB = new JScrollPane(tableB);
jspB.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(jspB);
}
private static class MyModel extends AbstractTableModel {
private final String name;
private final int rows;
public MyModel(String name, int rows) {
this.name = name;
this.rows = rows;
}
@Override
public int getRowCount() {
return rows;
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public String getColumnName(int col) {
return name + (col + 1);
}
@Override
public Object getValueAt(int row, int col) {
return (row + 1) / 10.0;
}
@Override
public Class<?> getColumnClass(int col) {
return Number.class;
}
}
}
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Find similar
JTable, JScrollPane and empty space "A. W. Dunstan" <no@spam.thanks> - 2012-01-10 16:55 -0500
Re: JTable, JScrollPane and empty space markspace <-@.> - 2012-01-10 19:43 -0800
Re: JTable, JScrollPane and empty space "A. W. Dunstan" <no@spam.thanks> - 2012-01-11 09:12 -0500
Re: JTable, JScrollPane and empty space markspace <-@.> - 2012-01-11 13:08 -0800
Re: JTable, JScrollPane and empty space "A. W. Dunstan" <no@spam.thanks> - 2012-01-12 11:16 -0500
Re: JTable, JScrollPane and empty space "John B. Matthews" <nospam@nospam.invalid> - 2012-01-11 07:47 -0500
csiph-web