Re: JTable, JScrollPane and empty space
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class Sandbox extends JFrame {
public Sandbox(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
m_settings = new JPanel();
m_gbc = new GridBagConstraints();
m_gbc.gridx = 0;
m_gbc.gridy = 0;
m_gbc.anchor = GridBagConstraints.WEST;
m_gbc.insets = new Insets(2, 5, 2, 5);
m_gbl = new GridBagLayout();
m_settings.setLayout(m_gbl);
add(m_settings, BorderLayout.CENTER);
// Title across the top
JLabel l = new JLabel("Range?");
m_gbl.setConstraints(l, m_gbc);
m_settings.add(l);
m_gbc.gridx++;
l = new JLabel("Parameter");
m_gbl.setConstraints(l, m_gbc);
m_settings.add(l);
m_gbc.gridx++;
l = new JLabel("Value");
m_gbl.setConstraints(l, m_gbc);
m_settings.add(l);
m_gbc.gridy++;
// First table
addTable("ATM", new DummyTableModel(6));
// Second table
addTable("ICLD", new DummyTableModel(15));
}
private void addTable(String label, TableModel tm) {
JScrollPane scrollPane = new JScrollPane(new JTable(tm));
//scrollPane.setPreferredSize(new Dimension(400, 100));
//scrollPane.setMinimumSize(new Dimension(200, 50));
m_gbc.fill = GridBagConstraints.BOTH;
m_gbc.weighty = 1;
m_gbc.weightx = 1;
addNonRangeComponent(label, scrollPane);
}
protected void addNonRangeComponent(final String paramName,
final JComponent component) {
m_gbc.gridx = 1;
m_gbc.gridy++;
// Label what we're showing.
JLabel quantity = new JLabel(paramName);
m_gbl.setConstraints(quantity, m_gbc);
m_settings.add(quantity);
m_gbc.gridx++;
// Show the parameter.
m_gbl.setConstraints(component, m_gbc);
m_settings.add(component);
}
// Where we put things & how they're laid out.
private JPanel m_settings;
private GridBagLayout m_gbl;
private GridBagConstraints m_gbc;
public static void main(String[] args) {
Sandbox sb = new Sandbox("New Range Layout Sandbox");
sb.setVisible(true);
sb.pack();
}
}
/**
* Simple table model. First column is 'is selected?', 2nd column is a
* description of what's selected. 1st column is boolean & editable, 2nd
* is String & not editable.
*/
class DummyTableModel extends AbstractTableModel {
public DummyTableModel(int nRows) {
m_isSelected = new boolean[nRows];
}
public int getRowCount() { return m_isSelected.length; }
public int getColumnCount() { return 2; }
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0: return m_isSelected[rowIndex];
case 1: return "The value for this row is " + rowIndex;
default: return null;
}
}
public String getColumnName(int column) {
switch (column) {
case 0: return "Selected?";
case 1: return "Description";
default: return "Oops";
}
}
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: return Boolean.class;
case 1: return String.class;
default: return null;
}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Boolean b = (Boolean)aValue;
m_isSelected[rowIndex] = b.booleanValue();
}
final private boolean[] m_isSelected;
}
--
Al Dunstan, Software Engineer