⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basictableui.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
  /**   * Creates and returns a key listener for the JTable.   *   * @return a key listener for the JTable   */  protected KeyListener createKeyListener()  {    return new KeyHandler();  }  /**   * Return the maximum size of the table. The maximum height is the row     * height times the number of rows. The maximum width is the sum of     * the maximum widths of each column.    *     *  @param comp the component whose maximum size is being queried,    *  this is ignored.    *  @return a Dimension object representing the maximum size of the table,    *  or null if the table has no elements.   */  public Dimension getMaximumSize(JComponent comp)   {    int maxTotalColumnWidth = 0;    for (int i = 0; i < table.getColumnCount(); i++)      maxTotalColumnWidth += table.getColumnModel().getColumn(i).getMaxWidth();    if (maxTotalColumnWidth == 0 || table.getRowCount() == 0)      return null;    return new Dimension(maxTotalColumnWidth, table.getRowCount()*table.getRowHeight());  }  /**   * Return the minimum size of the table. The minimum height is the row     * height times the number of rows. The minimum width is the sum of     * the minimum widths of each column.    *     *  @param comp the component whose minimum size is being queried,    *  this is ignored.    *  @return a Dimension object representing the minimum size of the table,    *  or null if the table has no elements.   */  public Dimension getMinimumSize(JComponent comp)   {    int minTotalColumnWidth = 0;    for (int i = 0; i < table.getColumnCount(); i++)      minTotalColumnWidth += table.getColumnModel().getColumn(i).getMinWidth();    if (minTotalColumnWidth == 0 || table.getRowCount() == 0)      return null;    return new Dimension(minTotalColumnWidth, table.getRowCount()*table.getRowHeight());  }  public Dimension getPreferredSize(JComponent comp)   {    int width = table.getColumnModel().getTotalColumnWidth();    int height = table.getRowCount() * table.getRowHeight();    return new Dimension(width, height);  }  protected void installDefaults()   {    LookAndFeel.installColorsAndFont(table, "Table.background",                                     "Table.foreground", "Table.font");    table.setGridColor(UIManager.getColor("Table.gridColor"));    table.setSelectionForeground(UIManager.getColor("Table.selectionForeground"));    table.setSelectionBackground(UIManager.getColor("Table.selectionBackground"));    table.setOpaque(true);    rendererPane = new CellRendererPane();  }  protected void installKeyboardActions()   {    InputMap ancestorMap = (InputMap) UIManager.get("Table.ancestorInputMap");    InputMapUIResource parentInputMap = new InputMapUIResource();    // FIXME: The JDK uses a LazyActionMap for parentActionMap    ActionMap parentActionMap = new ActionMapUIResource();    action = new TableAction();    Object keys[] = ancestorMap.allKeys();    // Register key bindings in the UI InputMap-ActionMap pair    for (int i = 0; i < keys.length; i++)      {        KeyStroke stroke = (KeyStroke)keys[i];        String actionString = (String) ancestorMap.get(stroke);        parentInputMap.put(KeyStroke.getKeyStroke(stroke.getKeyCode(),                                                  stroke.getModifiers()),                           actionString);        parentActionMap.put (actionString,                              new ActionListenerProxy (action, actionString));      }    // Set the UI InputMap-ActionMap pair to be the parents of the    // JTable's InputMap-ActionMap pair    parentInputMap.setParent      (table.getInputMap       (JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).getParent());    parentActionMap.setParent(table.getActionMap().getParent());    table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).      setParent(parentInputMap);    table.getActionMap().setParent(parentActionMap);  }  /**   * This class is used to mimmic the behaviour of the JDK when registering   * keyboard actions.  It is the same as the private class used in JComponent   * for the same reason.  This class receives an action event and dispatches   * it to the true receiver after altering the actionCommand property of the   * event.   */  private static class ActionListenerProxy    extends AbstractAction  {    ActionListener target;    String bindingCommandName;    public ActionListenerProxy(ActionListener li,                                String cmd)    {      target = li;      bindingCommandName = cmd;    }    public void actionPerformed(ActionEvent e)    {      ActionEvent derivedEvent = new ActionEvent(e.getSource(),                                                 e.getID(),                                                 bindingCommandName,                                                 e.getModifiers());      target.actionPerformed(derivedEvent);    }  }  /**   * This class implements the actions that we want to happen   * when specific keys are pressed for the JTable.  The actionPerformed   * method is called when a key that has been registered for the JTable   * is received.   */  class TableAction extends AbstractAction  {    /**     * What to do when this action is called.     *     * @param e the ActionEvent that caused this action.     */    public void actionPerformed (ActionEvent e)    {      DefaultListSelectionModel rowModel = (DefaultListSelectionModel) table.getSelectionModel();      DefaultListSelectionModel colModel = (DefaultListSelectionModel) table.getColumnModel().getSelectionModel();      int rowLead = rowModel.getLeadSelectionIndex();      int rowMax = table.getModel().getRowCount() - 1;            int colLead = colModel.getLeadSelectionIndex();      int colMax = table.getModel().getColumnCount() - 1;            String command = e.getActionCommand();            if (command.equals("selectPreviousRowExtendSelection"))        {          rowModel.setLeadSelectionIndex(Math.max(rowLead - 1, 0));          colModel.setLeadSelectionIndex(colLead);        }      else if (command.equals("selectLastColumn"))        {          rowModel.setSelectionInterval(rowLead, rowLead);          colModel.setSelectionInterval(colMax, colMax);        }      else if (command.equals("startEditing"))        {          if (table.isCellEditable(rowLead, colLead))            table.editCellAt(rowLead,colLead);        }      else if (command.equals("selectFirstRowExtendSelection"))        {                        rowModel.setLeadSelectionIndex(0);          colModel.setLeadSelectionIndex(colLead);        }      else if (command.equals("selectFirstColumn"))        {          rowModel.setSelectionInterval(rowLead, rowLead);          colModel.setSelectionInterval(0, 0);        }      else if (command.equals("selectFirstColumnExtendSelection"))        {          colModel.setLeadSelectionIndex(0);          rowModel.setLeadSelectionIndex(rowLead);        }            else if (command.equals("selectLastRow"))        {          rowModel.setSelectionInterval(rowMax,rowMax);          colModel.setSelectionInterval(colLead, colLead);        }      else if (command.equals("selectNextRowExtendSelection"))        {          rowModel.setLeadSelectionIndex(Math.min(rowLead + 1, rowMax));          colModel.setLeadSelectionIndex(colLead);        }      else if (command.equals("selectFirstRow"))        {          rowModel.setSelectionInterval(0,0);          colModel.setSelectionInterval(colLead, colLead);        }      else if (command.equals("selectNextColumnExtendSelection"))        {          colModel.setLeadSelectionIndex(Math.min(colLead + 1, colMax));          rowModel.setLeadSelectionIndex(rowLead);        }      else if (command.equals("selectLastColumnExtendSelection"))        {          colModel.setLeadSelectionIndex(colMax);          rowModel.setLeadSelectionIndex(rowLead);        }      else if (command.equals("selectPreviousColumnExtendSelection"))        {          colModel.setLeadSelectionIndex(Math.max(colLead - 1, 0));          rowModel.setLeadSelectionIndex(rowLead);        }      else if (command.equals("selectNextRow"))        {          rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),                                        Math.min(rowLead + 1, rowMax));          colModel.setSelectionInterval(colLead,colLead);        }      else if (command.equals("scrollUpExtendSelection"))        {          int target;          if (rowLead == getFirstVisibleRowIndex())            target = Math.max              (0, rowLead - (getLastVisibleRowIndex() -                              getFirstVisibleRowIndex() + 1));          else            target = getFirstVisibleRowIndex();                    rowModel.setLeadSelectionIndex(target);          colModel.setLeadSelectionIndex(colLead);        }      else if (command.equals("selectPreviousRow"))        {          rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),                                        Math.max(rowLead - 1, 0));          colModel.setSelectionInterval(colLead,colLead);        }      else if (command.equals("scrollRightChangeSelection"))        {          int target;          if (colLead == getLastVisibleColumnIndex())            target = Math.min              (colMax, colLead + (getLastVisibleColumnIndex() -                                  getFirstVisibleColumnIndex() + 1));          else            target = getLastVisibleColumnIndex();                    colModel.setSelectionInterval(target, target);          rowModel.setSelectionInterval(rowLead, rowLead);        }      else if (command.equals("selectPreviousColumn"))        {          rowModel.setSelectionInterval(rowLead,rowLead);          colModel.setSelectionInterval(Math.max(colLead - 1, 0),                                        Math.max(colLead - 1, 0));        }      else if (command.equals("scrollLeftChangeSelection"))        {          int target;          if (colLead == getFirstVisibleColumnIndex())            target = Math.max              (0, colLead - (getLastVisibleColumnIndex() -                             getFirstVisibleColumnIndex() + 1));          else            target = getFirstVisibleColumnIndex();                    colModel.setSelectionInterval(target, target);          rowModel.setSelectionInterval(rowLead, rowLead);        }      else if (command.equals("clearSelection"))        {          table.clearSelection();        }      else if (command.equals("cancel"))        {          // FIXME: implement other parts of "cancel" like undo-ing last          // selection.  Right now it just calls editingCancelled if          // we're currently editing.          if (table.isEditing())            table.editingCanceled(new ChangeEvent("cancel"));        }      else if (command.equals("selectNextRowCell")               || command.equals("selectPreviousRowCell")               || command.equals("selectNextColumnCell")               || command.equals("selectPreviousColumnCell"))        {          // If nothing is selected, select the first cell in the table          if (table.getSelectedRowCount() == 0 &&               table.getSelectedColumnCount() == 0)            {              rowModel.setSelectionInterval(0, 0);              colModel.setSelectionInterval(0, 0);              return;            }                    // If the lead selection index isn't selected (ie a remove operation          // happened, then set the lead to the first selected cell in the          // table          if (!table.isCellSelected(rowLead, colLead))            {              rowModel.addSelectionInterval(rowModel.getMinSelectionIndex(),                                             rowModel.getMinSelectionIndex());              colModel.addSelectionInterval(colModel.getMinSelectionIndex(),                                             colModel.getMinSelectionIndex());              return;            }                    // multRowsSelected and multColsSelected tell us if multiple rows or          // columns are selected, respectively          boolean multRowsSelected, multColsSelected;          multRowsSelected = table.getSelectedRowCount() > 1 &&            table.getRowSelectionAllowed();                    multColsSelected = table.getSelectedColumnCount() > 1 &&            table.getColumnSelectionAllowed();                    // If there is just one selection, select the next cell, and wrap          // when you get to the edges of the table.          if (!multColsSelected && !multRowsSelected)            {              if (command.indexOf("Column") != -1)                 advanceSingleSelection(colModel, colMax, rowModel, rowMax,                                        (command.equals                                        ("selectPreviousColumnCell")));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -