Java by API/javax.swing.table/TableColumnModel — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:17, 31 мая 2010

TableColumnModel: addColumnModelListener(TableColumnModelListener x)

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class MainClass {
  public static void main(String args[]) {
    Object rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    Object headers[] = { "Upper", "Lower" };
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(rows, headers);
    TableColumnModelListener tableColumnModelListener = new TableColumnModelListener() {
      public void columnAdded(TableColumnModelEvent e) {
        System.out.println("Added");
      }
      public void columnMarginChanged(ChangeEvent e) {
        System.out.println("Margin");
      }
      public void columnMoved(TableColumnModelEvent e) {
        System.out.println("Moved");
      }
      public void columnRemoved(TableColumnModelEvent e) {
        System.out.println("Removed");
      }
      public void columnSelectionChanged(ListSelectionEvent e) {
        System.out.println("Selection Changed");
      }
    };
    TableColumnModel columnModel = table.getColumnModel();
    columnModel.addColumnModelListener(tableColumnModelListener);
    columnModel.setColumnMargin(12);
    TableColumn column = new TableColumn(1);
    columnModel.addColumn(column);
    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





TableColumnModel: addColumn(TableColumn aColumn)

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class MainClass {
  public static void main(String args[]) {
    Object rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    Object headers[] = { "Upper", "Lower" };
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(rows, headers);
    TableColumnModelListener tableColumnModelListener = new TableColumnModelListener() {
      public void columnAdded(TableColumnModelEvent e) {
        System.out.println("Added");
      }
      public void columnMarginChanged(ChangeEvent e) {
        System.out.println("Margin");
      }
      public void columnMoved(TableColumnModelEvent e) {
        System.out.println("Moved");
      }
      public void columnRemoved(TableColumnModelEvent e) {
        System.out.println("Removed");
      }
      public void columnSelectionChanged(ListSelectionEvent e) {
        System.out.println("Selection Changed");
      }
    };
    TableColumnModel columnModel = table.getColumnModel();
    columnModel.addColumnModelListener(tableColumnModelListener);
    columnModel.setColumnMargin(12);
    TableColumn column = new TableColumn(1);
    columnModel.addColumn(column);
    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





TableColumnModel: getSelectionModel()

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class MainClass extends JFrame {
  public MainClass() {
    super("Selection Model Test");
    setSize(450, 350);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    TableModel tm = new AbstractTableModel() {
      public int getRowCount() {
        return 10;
      }
      public int getColumnCount() {
        return 10;
      }
      public Object getValueAt(int r, int c) {
        return "0";
      }
    };
    final JTable jt = new JTable(tm);
    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
    JPanel controlPanel, buttonPanel, columnPanel, rowPanel;
    buttonPanel = new JPanel();
    final JCheckBox cellBox, columnBox, rowBox;
    cellBox = new JCheckBox("Cells", jt.getCellSelectionEnabled());
    columnBox = new JCheckBox("Columns", jt.getColumnSelectionAllowed());
    rowBox = new JCheckBox("Rows", jt.getRowSelectionAllowed());
    
    cellBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        jt.setCellSelectionEnabled(cellBox.isSelected());
        columnBox.setSelected(jt.getColumnSelectionAllowed());
        rowBox.setSelected(jt.getRowSelectionAllowed());
      }
    });
    columnBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        jt.setColumnSelectionAllowed(columnBox.isSelected());
        cellBox.setSelected(jt.getCellSelectionEnabled());
      }
    });
    rowBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        jt.setRowSelectionAllowed(rowBox.isSelected());
        cellBox.setSelected(jt.getCellSelectionEnabled());
      }
    });
    buttonPanel.add(new JLabel("Selections allowed:"));
    buttonPanel.add(cellBox);
    buttonPanel.add(columnBox);
    buttonPanel.add(rowBox);
    columnPanel = new JPanel();
    ListSelectionModel csm = jt.getColumnModel().getSelectionModel();
    JLabel columnCounter = new JLabel("Selected Column Indices:");
    csm.addListSelectionListener(new SelectionDebugger(columnCounter, csm));
    columnPanel.add(new JLabel("Selected columns:"));
    columnPanel.add(columnCounter);
    rowPanel = new JPanel();
    ListSelectionModel rsm = jt.getSelectionModel();
    JLabel rowCounter = new JLabel("Selected Row Indices:");
    rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
    rowPanel.add(new JLabel("Selected rows:"));
    rowPanel.add(rowCounter);
    controlPanel = new JPanel(new GridLayout(0, 1));
    controlPanel.add(buttonPanel);
    controlPanel.add(columnPanel);
    controlPanel.add(rowPanel);
    getContentPane().add(controlPanel, BorderLayout.SOUTH);
  }
  public static void main(String args[]) {
    MainClass se = new MainClass();
    se.setVisible(true);
  }
  public class SelectionDebugger implements ListSelectionListener {
    JLabel debugger;
    ListSelectionModel model;
    public SelectionDebugger(JLabel target, ListSelectionModel lsm) {
      debugger = target;
      model = lsm;
    }
    public void valueChanged(ListSelectionEvent lse) {
      if (!lse.getValueIsAdjusting()) {
        StringBuffer buf = new StringBuffer();
        int[] selection = getSelectedIndices(model.getMinSelectionIndex(), model
            .getMaxSelectionIndex());
        if (selection.length == 0) {
          buf.append("none");
        } else {
          for (int i = 0; i < selection.length - 1; i++) {
            buf.append(selection[i]);
            buf.append(", ");
          }
          buf.append(selection[selection.length - 1]);
        }
        debugger.setText(buf.toString());
      }
    }
    protected int[] getSelectedIndices(int start, int stop) {
      if ((start == -1) || (stop == -1)) {
        return new int[0];
      }
      int guesses[] = new int[stop - start + 1];
      int index = 0;
      for (int i = start; i <= stop; i++) {
        if (model.isSelectedIndex(i)) {
          guesses[index++] = i;
        }
      }
      int realthing[] = new int[index];
      System.arraycopy(guesses, 0, realthing, 0, index);
      return realthing;
    }
  }
}