Beispiel #1
0
  SwingTableDemo() {

    // Create a new JFrame container.
    JFrame jfrm = new JFrame("JTable Demo");

    // Specify FlowLayout for the layout manager.
    jfrm.setLayout(new FlowLayout());

    // Give the frame an initial size.
    jfrm.setSize(460, 180);

    // Terminate the program when the user closes the application.
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create a table that displays order data.
    jtabOrders = new JTable(data, headings);

    // Wrap the table in a scroll pane.
    JScrollPane jscrlp = new JScrollPane(jtabOrders);

    // Set the scrollable viewport size.
    jtabOrders.setPreferredScrollableViewportSize(new Dimension(420, 62));

    // Create the radio buttons that determine
    // what type of selections are allowed.
    jrbRows = new JRadioButton("Select Rows", true);
    jrbColumns = new JRadioButton("Select Columns");
    jrbCells = new JRadioButton("Select Cells");

    // Add the radio buttons to a group.
    ButtonGroup bg = new ButtonGroup();
    bg.add(jrbRows);
    bg.add(jrbColumns);
    bg.add(jrbCells);

    // Radio button events are handled in common by the
    // actionPerformed() method implemented by TableDemo.
    jrbRows.addActionListener(this);
    jrbColumns.addActionListener(this);
    jrbCells.addActionListener(this);

    // Create the Single Selection Mode check box.
    // When checked, only single selections are allowed.
    jcbSingle = new JCheckBox("Single Selection Mode");

    // Add item listener for jcbSingle.
    jcbSingle.addItemListener(
        new ItemListener() {

          public void itemStateChanged(ItemEvent ie) {
            if (jcbSingle.isSelected())
              // Allow single selections.
              jtabOrders.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            else
              // Allow multiple selections.
              jtabOrders.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
          }
        });

    // Add the components to the content pane.
    jfrm.add(jscrlp);
    jfrm.add(jrbRows);
    jfrm.add(jrbColumns);
    jfrm.add(jrbCells);
    jfrm.add(jcbSingle);

    // Display the frame.
    jfrm.setVisible(true);
  }