双击使TitledBorder可编辑

时间:2018-07-19 15:16:05

标签: java swing titled-border

这是对Make a TitledBorder title editable

的扩展

更具体地说,我希望双击标题时可以编辑TitledBorder。我将边框放置在使用BoxLayout的java Box周围。双击最好在该位置打开一个JTextField,但如果无法完成,则可以打开另一个窗口来编辑标题。

TitledBorder editableBorder = new TitledBorder(editableString);
editableBorder.setTitleJustification(TitledBorder.CENTER);

Box containerBox = new Box(BoxLayout.PAGE_AXIS);
containerBox.setBorder(new CompoundBorder(editableBorder, new EmptyBorder(10, 0, 10, 0)));

Box insideBox = new Box(BoxLayout.PAGE_AXIS);
insideBox.add(new JLabel(new ImageIcon(new BufferedImage(200,40,BufferedImage.TYPE_INT_RGB))));
containerBox.add(insideBox);

1 个答案:

答案 0 :(得分:2)

这里是一个例子。 MouseListener使用JPopupMenu来显示文本字段。这意味着您可以使用Escape键取消编辑。

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

public class TitledBorderListener extends MouseAdapter
{
    private JPopupMenu editPopup;
    private JTextField editTextField;
    private TitledBorder titledBorder;

    @Override
    public void mouseClicked(MouseEvent e)
    {
        if (e.getClickCount != 2)
            return;

        //  Edit the border on a double click

        JComponent component = (JComponent)e.getSource();
        Border border = component.getBorder();

        if (border instanceof TitledBorder)
        {
            titledBorder = (TitledBorder)border;
            FontMetrics fm = component.getFontMetrics( titledBorder.getTitleFont() );
            int titleWidth = fm.stringWidth(titledBorder.getTitle()) + 20;
            Rectangle bounds = new Rectangle(0, 0, titleWidth, fm.getHeight());

            if (bounds.contains(e.getPoint()))
            {
                if (editPopup == null)
                    createEditPopup();

                //  Position the popup editor over top of the title

                editTextField.setText( titledBorder.getTitle() );
                Dimension d = editTextField.getPreferredSize();
                d.width = titleWidth;
                editPopup.setPreferredSize(d);
                editPopup.show(component, 0, 0);

                editTextField.selectAll();
                editTextField.requestFocusInWindow();
            }
        }
    }

    private void createEditPopup()
    {
        editTextField = new JTextField();

        //  Add an Action to the text field to save the new title text

        editTextField.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String value = editTextField.getText();
                titledBorder.setTitle( value );
                editPopup.setVisible(false);
                editPopup.getInvoker().revalidate();
                editPopup.getInvoker().repaint();
            }
        });

        //  Add the editor to the popup

        editPopup = new JPopupMenu();
        editPopup.setBorder( new EmptyBorder(0, 0, 0, 0) );
        editPopup.add(editTextField);
    }

    private static void createAndShowUI()
    {

        JPanel panel = new JPanel();
        panel.setBorder( new TitledBorder("Double Click to Edit") );
        panel.addMouseListener( new TitledBorderListener() );

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}