1

Is there a way to make the title in TitledBorder editable so the user can edit what the title is? Or if there's another border that may allow this, what is it?

Box.setBorder(new TitledBorder(editableString));

EDIT:

Here's a link to my more specific question of editing based on a double click: Make TitledBorder editable upon double click

DarkHark
  • 614
  • 1
  • 6
  • 20
  • See if this helps : https://stackoverflow.com/questions/29304680/how-to-change-border-title-on-button-click – Arnaud Jul 18 '18 at 14:20
  • Border is not a component, so it cannot get focus, and therefore cannot be directly edited by the user. It's possible to do something which looks like thing what you want, but it's not so easy. – Sergiy Medvynskyy Jul 18 '18 at 14:23
  • Do I have to extend the TitledBorder class and make it so it excepts a JTextBox as a title? Is that possible? – DarkHark Jul 18 '18 at 14:25
  • 1
    No. As I already said, Border is not a component. and cannot get focus. – Sergiy Medvynskyy Jul 18 '18 at 14:27
  • If all else fails, I'll create an extra box on top of my current box then set the current boxes border, but I like the design of the TitledBorder more. – DarkHark Jul 18 '18 at 14:30
  • Try to override onMouseClicked listener on the JPanel and check the coordinates if it is at the title and open an JOptionPane to ask a new title. Using a EditText at the position would be another solution but you had to draw all borders – Marcos Vasconcelos Jul 19 '18 at 14:31

1 Answers1

1

Basically, keep a reference to the titled border, change it when needed, then repaint the component with the border. As below: change the text in the text field then hit Enter to see the effect.

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class EditableTitledBorder {

    private JComponent ui = null;
    private TitledBorder titledBorder;

    EditableTitledBorder() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        String initialTitle = "Dark Image";
        titledBorder = new TitledBorder(initialTitle);
        ui.setBorder(titledBorder);
        final JTextField titleField = new JTextField(initialTitle);
        ActionListener changeTitleListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                titledBorder.setTitle(titleField.getText());
                ui.repaint();
            }
        };
        titleField.addActionListener(changeTitleListener);
        ui.add(titleField, BorderLayout.PAGE_START);
        ui.add(new JLabel(new ImageIcon(
                new BufferedImage(200,40,BufferedImage.TYPE_INT_RGB))));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                EditableTitledBorder o = new EditableTitledBorder();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I want the title itself to be clickable and changed. I don't really like the look of two titles. Even if that means right clicking or double clicking the Border to open a TextField that changes the border. I'm also using a BoxLayout and Boxes instead. Thank you though. – DarkHark Jul 19 '18 at 14:13
  • *"I want the title itself to be clickable and changed."* O..K I suggest you mark this question as answered and ask a new, more specific question that gives the extra requirement. – Andrew Thompson Jul 19 '18 at 14:36
  • Will do. My fault for not being specific enough in the question. – DarkHark Jul 19 '18 at 14:57