Google Tag Manager

2017/12/26

Automatically extend its height when JTextArea gains focus

Code

textArea.addFocusListener(new FocusAdapter() {
  @Override public void focusLost(FocusEvent e) {
    String text = textArea.getText();
    label.setText(text.isEmpty() ? " " : text);
    cardLayout.show(cp, "TextField");
  }
});
label.addFocusListener(new FocusAdapter() {
  @Override public void focusGained(FocusEvent e) {
    cardLayout.show(cp, "TextArea");
    textArea.requestFocusInWindow();
  }
});

References

2017/11/24

When JTree's node is expanded, collapse all other sibling nodes

Code

JTree tree = new JTree(makeModel());
tree.setRootVisible(false);
tree.addTreeWillExpandListener(new TreeWillExpandListener() {
  private boolean isAdjusting;
  @Override public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException {
    if (isAdjusting) {
      return;
    }
    isAdjusting = true;
    collapseFirstHierarchy(tree);
    tree.setSelectionPath(e.getPath());
    isAdjusting = false;
  }
  @Override public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
    //throw new ExpandVetoException(e, "Tree collapse cancelled");
  }
});
//...
public static void collapseFirstHierarchy(JTree tree) {
  TreeModel model = tree.getModel();
  DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
  Enumeration e = root.breadthFirstEnumeration();
  while (e.hasMoreElements()) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
    boolean isOverFirstLevel = node.getLevel() > 1;
    if (isOverFirstLevel) { // Collapse only nodes in the first hierarchy
      return;
    } else if (node.isLeaf() || node.isRoot()) {
      continue;
    }
    tree.collapsePath(new TreePath(node.getPath()));
  }
}

References

2017/10/27

Set Sudoku style border lines created by MatteBorder as cell ruled lines of JTable

Code

static class SudokuCellRenderer extends DefaultTableCellRenderer {
  private final Font font;
  private final Font bold;
  private final Border b0 = BorderFactory.createMatteBorder(
      0, 0, BORDERWIDTH1, BORDERWIDTH1, Color.GRAY);
  private final Border b1 = BorderFactory.createMatteBorder(
      0, 0, BORDERWIDTH2, BORDERWIDTH2, Color.BLACK);
  private final Border b2 = BorderFactory.createCompoundBorder(
      BorderFactory.createMatteBorder(0, 0, BORDERWIDTH2, 0, Color.BLACK),
      BorderFactory.createMatteBorder(0, 0, 0, BORDERWIDTH1, Color.GRAY));
  private final Border b3 = BorderFactory.createCompoundBorder(
      BorderFactory.createMatteBorder(0, 0, 0, BORDERWIDTH2, Color.BLACK),
      BorderFactory.createMatteBorder(0, 0, BORDERWIDTH1, 0, Color.GRAY));
  private final Integer[][] data;
  protected SudokuCellRenderer(Integer[][] src, Font font) {
    super();
    this.font = font;
    this.bold = font.deriveFont(Font.BOLD);
    Integer[][] dest = new Integer[src.length][src[0].length];
    for (int i = 0; i < src.length; i++) {
      System.arraycopy(src[i], 0, dest[i], 0, src[0].length);
    }
    this.data = dest;
  }
  @Override public Component getTableCellRendererComponent(
      JTable table, Object value, boolean isSelected, boolean hasFocus,
      int row, int column) {
    boolean isEditable = data[row][column] == 0;
    super.getTableCellRendererComponent(
        table, value, isEditable && isSelected, hasFocus, row, column);
    if (isEditable && Objects.equals(value, 0)) {
      this.setText(" ");
    }
    setFont(isEditable ? font : bold);
    setHorizontalAlignment(CENTER);
    boolean rf = (row + 1) % 3 == 0;
    boolean cf = (column + 1) % 3 == 0;
    if (rf && cf) {
      setBorder(b1);
    } else if (rf) {
      setBorder(b2);
    } else if (cf) {
      setBorder(b3);
    } else {
      setBorder(b0);
    }
    return this;
  }
}

References

2017/09/28

Add a JComboBox that selects the encoding at the bottom of JFileChooser

Code

class EncodingFileChooserUI extends MetalFileChooserUI {
  public final JComboBox< String> combo = new JComboBox<>(
      new String[] {"UTF-8", "UTF-16", "Shift_JIS", "EUC-JP"});
  protected EncodingFileChooserUI(JFileChooser filechooser) {
    super(filechooser);
  }
  @Override public void installComponents(JFileChooser fc) {
    super.installComponents(fc);
    JPanel bottomPanel = getBottomPanel();

    JLabel label = new JLabel("Encoding:") {
      @Override public Dimension getPreferredSize() {
        return SwingUtils.stream(bottomPanel)
          .filter(JLabel.class::isInstance).map(JLabel.class::cast)
          .findFirst()
          .map(JLabel::getPreferredSize)
          .orElse(super.getPreferredSize());
      }
    };
    label.setDisplayedMnemonic('E');
    label.setLabelFor(combo);

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
    panel.add(label);
    panel.add(combo);

    // 0: fileNamePanel
    // 1: RigidArea
    // 2: filesOfTypePanel
    bottomPanel.add(Box.createRigidArea(new Dimension(1, 5)), 3);
    bottomPanel.add(panel, 4);

    SwingUtils.stream(bottomPanel)
      .filter(JLabel.class::isInstance).map(JLabel.class::cast)
      .forEach(l -> {
        l.setHorizontalAlignment(SwingConstants.RIGHT);
        l.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
      });
  }
}

References

2017/08/22

Apply sort order cycle with ascending, descending, and unsorted in TableRowSorter with multi-key sorting

Summary

A three-state sorter(ascending, descending, none) example. Primary sort column is highlighted and the next ones have their sort-order number in brackets as shown on the screenshot below.
  • Left click: Change sort order in ascending, descending, unsorted
  • Right click: Unsorted

Code

/**
 * @author ssr
 */
jTable1.getTableHeader().setDefaultRenderer(new TestTableCellRenderer(jTable1));
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(jTable1.getModel()) {
  @Override
  public void toggleSortOrder(int column) {
    int keyIndex;
    if (column >= 0 && column < getModelWrapper().getColumnCount() && isSortable(column)) {
      List<RowSorter.SortKey> keys = new ArrayList<>(getSortKeys());
      boolean found = false;
      for (keyIndex = 0; keyIndex < keys.size(); keyIndex++) {
        RowSorter.SortKey sortKey = keys.get(keyIndex);
        if (sortKey.getColumn() == column) {
          found = true;
          break;
        }
      }
      if (isSortable(column)) {
        if (!found) {
          // Key doesn't exist
          RowSorter.SortKey sortKey = new RowSorter.SortKey(column, SortOrder.ASCENDING);
          keys.add(sortKey);
          System.out.println("ADDED: ");
          keys.stream().forEach(k -> System.out.println(k.getColumn() + ":" + k.getSortOrder()));
        } else {
          RowSorter.SortKey sortKey = keys.get(keyIndex);
          if (sortKey.getSortOrder() == SortOrder.DESCENDING) {
            keys.remove(keyIndex);
            System.out.println("REMOVED: ");
            keys.stream().forEach(k -> System.out.println(k.getColumn() + ":" + k.getSortOrder()));
          } else {
            keys.set(keyIndex, new RowSorter.SortKey(
                column, sortKey.getSortOrder() == SortOrder.ASCENDING
                  ? SortOrder.DESCENDING
                  : SortOrder.ASCENDING));
            System.out.println("MODIFIED: ");
            keys.stream().forEach(k -> System.out.println(k.getColumn() + ":" + k.getSortOrder()));
          }
        }
        if (keys.size() > getMaxSortKeys()) {
          keys = keys.subList(0, getMaxSortKeys());
        }
        setSortKeys(keys);
      }
    }
  }
};
sorter.setMaxSortKeys(4);
jTable1.setRowSorter(sorter);

References

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

2017/02/27

Click on the already selected JToggleButton and clear all selections in the ButtonGroup

Code

class ToggleButtonGroup extends ButtonGroup {
  private ButtonModel prevModel;
  private boolean isAdjusting;
  @Override public void setSelected(ButtonModel m, boolean b) {
    if (isAdjusting) {
      return;
    }
    if (m.equals(prevModel)) {
      isAdjusting = true;
      clearSelection();
      isAdjusting = false;
    } else {
      super.setSelected(m, b);
    }
    prevModel = getSelection();
  }
}

References

2017/01/27

Change the tab shape of JTabbedPane to trapezoid

Code

class IsoscelesTrapezoidTabbedPaneUI extends BasicTabbedPaneUI {
  private static final int ADJ2 = 3;
  private final Color selectedTabColor = UIManager.getColor("TabbedPane.selected");
  private final Color tabBackgroundColor = Color.LIGHT_GRAY;
  private final Color tabBorderColor = Color.GRAY;

  @Override protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) { //NOPMD
    int tabCount = tabPane.getTabCount();

    Rectangle iconRect = new Rectangle(),
    textRect = new Rectangle();
    Rectangle clipRect = g.getClipBounds();

    for (int i = runCount - 1; i >= 0; i--) {
      int start = tabRuns[i];
      int next = tabRuns[(i == runCount - 1) ? 0 : i + 1];
      int end = next != 0 ? next - 1 : tabCount - 1; //NOPMD
      // for (int j = start; j <= end; j++) {
      // https://stackoverflow.com/questions/41566659/tabs-rendering-order-in-custom-jtabbedpane
      for (int j = end; j >= start; j--) {
        if (j != selectedIndex && rects[j].intersects(clipRect)) {
          paintTab(g, tabPlacement, rects, j, iconRect, textRect);
        }
      }
    }
    if (selectedIndex >= 0 && rects[selectedIndex].intersects(clipRect)) {
      paintTab(g, tabPlacement, rects, selectedIndex, iconRect, textRect);
    }
  }
  @Override protected void paintTabBorder(
      Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
    // Do nothing
  }
  @Override protected void paintFocusIndicator(
      Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex,
      Rectangle iconRect, Rectangle textRect, boolean isSelected) {
    // Do nothing
  }
  @Override protected void paintContentBorderTopEdge(
      Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
    super.paintContentBorderTopEdge(g, tabPlacement, selectedIndex, x, y, w, h);
    Rectangle selRect = getTabBounds(selectedIndex, calcRect);
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setColor(selectedTabColor);
    g2.drawLine(selRect.x - ADJ2 + 1, y, selRect.x + selRect.width + ADJ2 - 1, y);
    g2.dispose();
  }
  @Override protected void paintTabBackground(
      Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    int textShiftOffset = isSelected ? 0 : 1;

    Rectangle clipRect = g2.getClipBounds();
    clipRect.grow(ADJ2 + 1, 0);
    g2.setClip(clipRect);

    GeneralPath trapezoid = new GeneralPath();
    trapezoid.moveTo(x - ADJ2,     y + h);
    trapezoid.lineTo(x + ADJ2,     y + textShiftOffset);
    trapezoid.lineTo(x + w - ADJ2, y + textShiftOffset);
    trapezoid.lineTo(x + w + ADJ2, y + h);
    //trapezoid.closePath();

    g2.setColor(isSelected ? selectedTabColor : tabBackgroundColor);
    g2.fill(trapezoid);

    g2.setColor(tabBorderColor);
    g2.draw(trapezoid);

    g2.dispose();
  }
}

References