Google Tag Manager

Showing posts with label JOptionPane. Show all posts
Showing posts with label JOptionPane. Show all posts

2020/05/02

Disable the JOptionPane OK button until text is entered in the JTextField

Code

JPanel panel2 = new JPanel(new GridLayout(2, 1));
JTextField field2 = new JTextField();
Border enabledBorder = field2.getBorder();
Insets i = enabledBorder.getBorderInsets(field2);
Border disabledBorder = BorderFactory.createCompoundBorder(
    BorderFactory.createLineBorder(Color.RED),
    BorderFactory.createEmptyBorder(i.top - 1, i.left - 1, i.bottom - 1, i.right - 1));
String disabledMessage = "Text is required to create ...";
JLabel label2 = new JLabel(" ");
label2.setForeground(Color.RED);
panel2.add(field2);
panel2.add(label2);
if (field2.getText().isEmpty()) {
  field2.setBorder(disabledBorder);
  label2.setText(disabledMessage);
}
field2.addHierarchyListener(e -> {
  Component c = e.getComponent();
  if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && c.isShowing()) {
    EventQueue.invokeLater(c::requestFocusInWindow);
  }
});
field2.getDocument().addDocumentListener(new DocumentListener() {
  private void update() {
    boolean verified = !field2.getText().isEmpty();
    JButton b = field2.getRootPane().getDefaultButton();
    if (verified) {
      b.setEnabled(true);
      field2.setBorder(enabledBorder);
      label2.setText(" ");
    } else {
      b.setEnabled(false);
      field2.setBorder(disabledBorder);
      label2.setText(disabledMessage);
    }
  }

  @Override public void insertUpdate(DocumentEvent e) {
    update();
  }

  @Override public void removeUpdate(DocumentEvent e) {
    update();
  }

  @Override public void changedUpdate(DocumentEvent e) {
    update();
  }
});
JButton button2 = new JButton("show");
button2.addActionListener(e -> {
  Component p2 = log.getRootPane();
  EventQueue.invokeLater(() -> {
    JButton b = field2.getRootPane().getDefaultButton();
    if (b != null && field2.getText().isEmpty()) {
      b.setEnabled(false);
    }
  });
  int ret = JOptionPane.showConfirmDialog(
      p2, panel2, "Input text", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  if (ret == JOptionPane.OK_OPTION) {
    log.setText(field2.getText());
  }
});

References

2013/03/30

JTree node edit only from JPopupMenu

Code

tree.setCellEditor(new DefaultTreeCellEditor(
    tree, (DefaultTreeCellRenderer) tree.getCellRenderer()) {
  @Override public boolean isCellEditable(EventObject e) {
    return !(e instanceof MouseEvent) && super.isCellEditable(e);
  }

  // @Override protected boolean canEditImmediately(EventObject e) {
  //   // ((MouseEvent) e).getClickCount() > 2
  //   return (e instanceof MouseEvent) ? false : super.canEditImmediately(e);
  // }
});
tree.setEditable(true);
tree.setComponentPopupMenu(new TreePopupMenu());

// ...
public TreePopupMenu() {
  super();
  add(new JMenuItem(new AbstractAction("Edit") {
    @Override public void actionPerformed(ActionEvent e) {
      JTree tree = (JTree) getInvoker();
      if (path != null) {
        tree.startEditingAtPath(path);
      }
    }
  }));
  add(new JMenuItem(new AbstractAction("Edit Dialog") {
    @Override public void actionPerformed(ActionEvent e) {
      JTree tree = (JTree) getInvoker();
      if (path == null) {
        return;
      }
      Object node = path.getLastPathComponent();
      if (node instanceof DefaultMutableTreeNode) {
        DefaultMutableTreeNode leaf = (DefaultMutableTreeNode) node;
        textField.setText(leaf.getUserObject().toString());
        int result = JOptionPane.showConfirmDialog(
            tree, textField, "Rename",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
          String str = textField.getText();
          if (!str.trim().isEmpty()) {
            ((DefaultTreeModel) tree.getModel()).valueForPathChanged(path, str);
          }
        }
      }
    }
  }));
  // ...

References

2012/08/17

JFileChooser with file already exists Dialog

Code

JFileChooser fileChooser = new JFileChooser() {
  @Override public void approveSelection() {
    File f = getSelectedFile();
    if (f.exists() && getDialogType() == SAVE_DIALOG) {
      String m = String.format(
          "<html>%s already exists.<br>Do you want to replace it?",
          f.getAbsolutePath());
      int rv = JOptionPane.showConfirmDialog(
          this, m, "Save As", JOptionPane.YES_NO_OPTION);
      if (rv != JOptionPane.YES_OPTION) {
        return;
      }
    }
    super.approveSelection();
  }
};

References