// Initialize JTable
  private void initTable() {
    DialogTableModel tblModel = new DialogTableModel(students);
    studentsTbl.setModel(tblModel);

    studentsTbl.setAutoCreateRowSorter(true);
    studentsTbl.setRowSelectionAllowed(true);
    studentsTbl.getRowSorter().toggleSortOrder(1);
    studentsTbl.setGridColor(Color.gray);
    studentsTbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    studentsTbl.setRowHeight(22);

    TableColumn tc = studentsTbl.getColumnModel().getColumn(0);
    tc.setCellEditor(studentsTbl.getDefaultEditor(Boolean.class));
    tc.setCellRenderer(studentsTbl.getDefaultRenderer(Boolean.class));
    tc.setHeaderRenderer(
        new CheckBoxTableHeader(
            new ItemListener() {
              @Override
              public void itemStateChanged(ItemEvent e) {
                Object source = e.getSource();
                if (source instanceof AbstractButton == false) {
                  return;
                }
                boolean checked = e.getStateChange() == ItemEvent.SELECTED;
                for (int x = 0, y = studentsTbl.getRowCount(); x < y; x++) {
                  studentsTbl.setValueAt(new Boolean(checked), x, 0);
                }
              }
            }));
  }
Example #2
0
  public void init() {
    _persistentRequested = false;
    this.addMouseListener(new MouseAdapt(this));

    super.setShowGrid(true); // --> Not reliable with Nimbus
    super.setGridColor(new Color(160, 160, 160));
    ;
    // super.setShowGrid(false); // override default mode of any LAF
    // this.addHighlighter(new BorderHighlighter(null,SplitTableDefaults.innerCellBorder(),false));

    super.setSortOrderCycle(SortOrder.ASCENDING, SortOrder.DESCENDING, SortOrder.UNSORTED);

    JTableHeader h = super.getTableHeader();
    h.addMouseListener(new zc3MouseAdaptForHeader(this));
    h.addMouseMotionListener(new zc3MouseAdaptForHeader(this));
    h.getColumnModel()
        .addColumnModelListener(
            new TableColumnModelListener() {
              public void columnAdded(TableColumnModelEvent e) {
                storeIfRequested();
              }

              public void columnRemoved(TableColumnModelEvent e) {
                storeIfRequested();
              }

              public void columnMoved(TableColumnModelEvent e) {
                storeIfRequested();
              }

              public void columnMarginChanged(ChangeEvent e) {
                storeIfRequested();
              }

              public void columnSelectionChanged(ListSelectionEvent e) {
                storeIfRequested();
              }
            });

    super.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    super.getSelectionModel()
        .addListSelectionListener(
            new ListSelectionListener() {
              public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                  int index = getSelectedRow();
                  int indices[] = getSelectedRows();
                  if (index == -1) {
                    informUnselect();
                  } else if (indices.length > 1) {
                    Vector<Integer> rows = new Vector<Integer>();
                    for (int i : indices) {
                      rows.add(i);
                    }
                    informSelect(rows);
                  } else {
                    informSelect(index);
                  }
                }
              }
            });

    _listeners = new HashSet<SelectionListener>();
    _mlisteners = new HashSet<MultipleSelectionListener>();
    _hlisteners = new HashSet<HeaderListener>();

    addKey(
        KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            if (JX2Table.this.getSelectedRow() != -1) {
              JX2Table.this.deleteEvent();
            }
          }
        });

    addKey(
        KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0),
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            if (JX2Table.this.getSelectedRow() != -1) {
              JX2Table.this.insertEvent();
            }
          }
        });

    addKey(
        KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.VK_CONTROL),
        new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            if (JX2Table.this.getSelectedRow() != -1) {
              JX2Table.this.enterEvent();
            }
          }
        });

    readPrefs();
  }