예제 #1
0
 /*
  * Don't need to implement this method unless your table's data can
  * change.
  */
 @Override
 public void setValueAt(Object value, int row, int col) {
   Relationship neighbour = (Relationship) getValueAt(0, 0);
   double offset =
       col == 1
           ? Double.parseDouble(((String) value))
           : Double.parseDouble(((String) getValueAt(0, 1)));
   String fwdDefault = Math.max(offset, current.getForwardDefault()) + "";
   if (neighbour.equals(Relationship.FORWARD)) {
     if (col == 0) {
       setValueAt(fwdDefault, 0, 1);
       fireTableChanged(null);
     } else {
       value = fwdDefault;
     }
   }
   super.setValueAt(value, row, col);
 }
예제 #2
0
  /** Create the dialog. */
  public USDialog(ActionListener listener) {
    setIconImages(Simulator.makeIcons());
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(new BorderLayout(0, 0));
    {
      JComboBox typeBox = new JComboBox(new DefaultComboBoxModel(Relationship.values()));
      typeEditor = new DefaultCellEditor(typeBox);

      table =
          new JTable(new USValuesTableModel()) {
            // Overridden to return a combobox for duration type
            @Override
            public TableCellEditor getCellEditor(int row, int column) {
              int modelColumn = convertColumnIndexToModel(column);

              if (modelColumn == 0) return typeEditor;
              else return super.getCellEditor(row, column);
            }
          };
      // Make window close on second enter press
      InputMap map = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
      map.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
      table.getColumnModel().getColumn(1).setPreferredWidth(10);
      table.getColumnModel().getColumn(1).setMaxWidth(10);
      table.doLayout();
      // Make single click start editing instead of needing double
      DefaultCellEditor singleClickEditor = new DefaultCellEditor(new JTextField());
      singleClickEditor.setClickCountToStart(1);
      table.setDefaultEditor(Object.class, singleClickEditor);
      table.setCellSelectionEnabled(false);

      JScrollPane scrollPane = new JScrollPane(table);
      contentPanel.add(scrollPane);
    }
    {
      JPanel buttonPane = new JPanel();
      buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
      getContentPane().add(buttonPane, BorderLayout.SOUTH);
      {
        JButton okButton = new JButton("OK");
        okButton.setActionCommand("OK");
        okButton.addActionListener(listener);
        buttonPane.add(okButton);
        getRootPane().setDefaultButton(okButton);
      }
    }
  }