Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #4924

Re: JTable, JScrollPane and empty space

From "A. W. Dunstan" <no@spam.thanks>
Organization OptiMetrics, Inc.
Date 2012-01-11 09:12 -0500
Subject Re: JTable, JScrollPane and empty space
Newsgroups comp.lang.java.gui
References <IdydnSRiIqV1LpHSnZ2dnUVZ_v2dnZ2d@megapath.net> <jej0hf$h35$1@dont-email.me>
Followup-To comp.lang.java.gui
Message-ID <o5ednZh5q8RBBZDSnZ2dnUVZ_omdnZ2d@megapath.net> (permalink)

Followups directed to: comp.lang.java.gui

Show all headers | View raw


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

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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