Google Tag Manager

2017/07/31

Detects that it has reached the bottom of JScrollPane

Code

JScrollPane scroll = new JScrollPane(c);
scroll.getVerticalScrollBar().getModel().addChangeListener(e -> {
  BoundedRangeModel m = (BoundedRangeModel) e.getSource();
  int extent  = m.getExtent();
  int maximum = m.getMaximum();
  int value   = m.getValue();
  if (value + extent >= maximum) {
    check.setEnabled(true);
  }
});

References

2017/06/29

Automatically adjust the height of JTable's row

Code

class TextAreaCellRenderer extends JTextArea implements TableCellRenderer {
  private final List< List< Integer > > rowAndCellHeightList = new ArrayList<>();

  @Override public void updateUI() {
    super.updateUI();
    setLineWrap(true);
    setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    setName("Table.cellRenderer");
  }
  @Override public Component getTableCellRendererComponent(
      JTable table, Object value, boolean isSelected, boolean hasFocus,
      int row, int column) {
    setFont(table.getFont());
    setText(Objects.toString(value, ""));
    adjustRowHeight(table, row, column);
    return this;
  }

  /**
   * Calculate the new preferred height for a given row, and sets the height on the table.
   * http://blog.botunge.dk/post/2009/10/09/JTable-multiline-cell-renderer.aspx
   */
  private void adjustRowHeight(JTable table, int row, int column) {
    // The trick to get this to work properly is to set the width of the column to the
    // textarea. The reason for this is that getPreferredSize(), without a width tries
    // to place all the text in one line. By setting the size with the with of the column,
    // getPreferredSize() returnes the proper height which the row should have in
    // order to make room for the text.
    // int cWidth = table.getTableHeader().getColumnModel().getColumn(column).getWidth();
    // int cWidth = table.getCellRect(row, column, false).width; //Ignore IntercellSpacing
    // setSize(new Dimension(cWidth, 1000));

    setBounds(table.getCellRect(row, column, false));
    // doLayout();

    int preferredHeight = getPreferredSize().height;
    while (rowAndCellHeightList.size() <= row) {
      rowAndCellHeightList.add(new ArrayList<>(column));
    }
    List cellHeightList = rowAndCellHeightList.get(row);
    while (cellHeightList.size() <= column) {
      cellHeightList.add(0);
    }
    cellHeightList.set(column, preferredHeight);
    int max = cellHeightList.stream().max(Integer::compare).get();
    if (table.getRowHeight(row) != max) {
      table.setRowHeight(row, max);
    }
  }
  // ...

References

2017/05/30

Use JComboBox as JTree's node cell editor

Code

class PluginCellEditor extends DefaultCellEditor {
  private final PluginPanel panel;
  private transient PluginNode node;

  protected PluginCellEditor(JComboBox comboBox) {
    super(comboBox);
    panel = new PluginPanel(comboBox);
  }
  @Override public Component getTreeCellEditorComponent(
      JTree tree, Object value, boolean isSelected, boolean expanded,
      boolean leaf, int row) {
    this.node = panel.extractNode(value);
    return panel;
  }
  @Override public Object getCellEditorValue() {
    Object o = super.getCellEditorValue();
    return Optional.ofNullable(node).map(node -> {
      DefaultComboBoxModel m =
        (DefaultComboBoxModel) panel.comboBox.getModel();
      PluginNode n = new PluginNode(panel.pluginName.getText(), node.plugins);
      n.setSelectedPluginIndex(m.getIndexOf(o));
      return (Object) n;
    }).orElse(o);
  }
  @Override public boolean isCellEditable(EventObject e) {
    Object source = e.getSource();
    if (!(source instanceof JTree) || !(e instanceof MouseEvent)) {
      return false;
    }
    JTree tree = (JTree) source;
    Point p = ((MouseEvent) e).getPoint();
    TreePath path = tree.getPathForLocation(p.x, p.y);
    if (Objects.isNull(path)) {
      return false;
    }
    Object node = path.getLastPathComponent();
    if (!(node instanceof DefaultMutableTreeNode)) {
      return false;
    }
    Rectangle r = tree.getPathBounds(path);
    if (Objects.isNull(r)) {
      return false;
    }
    Dimension d = panel.getPreferredSize();
    r.width = d.width;
    if (r.contains(p)) {
      showComboPopup(tree, p);
      return true;
    }
    return delegate.isCellEditable(e);
  }
  private void showComboPopup(JTree tree, Point p) {
    EventQueue.invokeLater(() -> {
      Point pt = SwingUtilities.convertPoint(tree, p, panel);
      Component o = SwingUtilities.getDeepestComponentAt(panel, pt.x, pt.y);
      if (o instanceof JComboBox) {
        panel.comboBox.showPopup();
      } else if (Objects.nonNull(o)) {
        Container c = SwingUtilities.getAncestorOfClass(
            JComboBox.class, (Component) o);
        if (c instanceof JComboBox) {
          panel.comboBox.showPopup();
        }
      }
    });
  }
}

References

2017/04/28

Disable any items in JList

Code

Set disableIndexSet = new HashSet<>();

JList list = new JList<>(model);
list.setCellRenderer(new DefaultListCellRenderer() {
  @Override public Component getListCellRendererComponent(
      JList list, Object value, int index,
      boolean isSelected, boolean cellHasFocus) {
    Component c;
    if (disableIndexSet.contains(index)) {
      c = super.getListCellRendererComponent(
          list, value, index, false, false);
      c.setEnabled(false);
    } else {
      c = super.getListCellRendererComponent(
          list, value, index, isSelected, cellHasFocus);
    }
    return c;
  }
});

initDisableIndex(disableIndexSet);
ActionMap am = list.getActionMap();
am.put("selectNextRow", new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    int index = list.getSelectedIndex();
    for (int i = index + 1; i < list.getModel().getSize(); i++) {
      if (!disableIndexSet.contains(i)) {
        list.setSelectedIndex(i);
        break;
      }
    }
  }
});
am.put("selectPreviousRow", new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    int index = list.getSelectedIndex();
    for (int i = index - 1; i >= 0; i--) {
      if (!disableIndexSet.contains(i)) {
        list.setSelectedIndex(i);
        break;
      }
    }
  }
});
//...
protected final void initDisableIndex(Set set) {
  set.clear();
  try {
    set.addAll(Arrays.stream(field.getText().split(","))
      .map(String::trim).filter(s -> !s.isEmpty())
      .map(Integer::valueOf).collect(Collectors.toSet()));
  } catch (NumberFormatException ex) {
    Toolkit.getDefaultToolkit().beep();
    JOptionPane.showMessageDialog(
        field, "invalid value.\n" + ex.getMessage(),
        "Error", JOptionPane.ERROR_MESSAGE);
  }
}

References

2017/03/30

Make a condensed Font and use it with JTextArea

Code

Font font = new Font(Font.MONOSPACED, Font.PLAIN, 18).deriveFont(
    AffineTransform.getScaleInstance(.9, 1d));
textArea.setFont(font);

References