简体   繁体   English

如何使用在JEditorPane中选择的文本来编辑JComboBox

[英]How to edit a JComboBox with text selected in JEditorPane

I have a UI with two components - a JEditorPane and a JComboBox. 我有一个包含两个组件的UI-JEditorPane和JComboBox。 My goal is to be able to type something into the JEditorPane, select a portion of the text, and while it is still selected type and/or select a value in an editable JComboBox. 我的目标是能够在JEditorPane中键入一些内容,选择文本的一部分,然后在仍选择文本的情况下键入和/或在可编辑的JComboBox中选择一个值。

This is for a text editor type of program where I want to change the font size of just the selected text in the editor pane. 这是针对文本编辑器类型的程序,在这里我想更改编辑器窗格中所选文本的字体大小。 Where the font size is coming from the editable combo box. 字体大小来自可编辑组合框的位置。 To clarify, I'm not asking how to apply styles to the text, I'm asking how to select a value in the combo box without losing the focus/selection in the JEditorPane. 为了澄清,我不是在问如何对文本应用样式,我是在问如何在组合框中选择一个值而不丢失JEditorPane中的焦点/选择。

Here's the code for the UI, but I wasn't sure where to begin doing anything with the focus... 这是UI的代码,但是我不确定从哪里开始着手做任何事情...

public static void main(String [] args)
{
    JFrame frame = new JFrame();
    JPanel contentPane = new JPanel();

    JComboBox<String> combo = new JComboBox(new String [] {"Hello", "World"});
    contentPane.add(combo);

    JEditorPane editor = new JEditorPane();
    contentPane.add(editor);

    frame.setContentPane(contentPane);
    frame.pack();
    frame.setVisible(true);
}

I'm asking how to select a value in the combo box without losing the focus/selection in the JEditorPane. 我在问如何在组合框中选择一个值而不丢失JEditorPane中的焦点/选择。

You don't lose the selection of the text in the editor pane when you select an item from the combo box. 从组合框中选择一个项目时,您不会在编辑器窗格中失去对文本的选择。 The selection remains, but it is just not painted until the editor pane regains focus. 所选内容仍然保留,但直到编辑器窗格重新获得焦点后才被绘制。

So the easiest way to do this is to use a JMenuItem. 因此,最简单的方法是使用JMenuItem。 Read the section from the Swing tutorial on Text Component Features for an example that does this. 阅读Swing教程中有关文本组件功能的部分,以获得执行此操作的示例。

If you still want to use the combo box then you can add Integer values to the combo box then the code in your ActionListener for the combo box would look something like: 如果仍要使用组合框,则可以将Integer值添加到组合框,然后ActionListener组合框的代码将类似于:

@Override
public void actionPerformed(ActionEvent e)
{
    Integer value = (Integer)comboBox.getSelectedItem();
    Action action = new StyledEditorKit.FontSizeAction("Font size", value);
    action.actionPerformed(null);
}

The StyledEditorKit actions extend from TextAction . StyledEditorKit操作从TextAction扩展。 The TextAction knows the last text component that had focus and therefore the font change is applied to that text component. TextAction知道最后一个具有焦点的文本组件,因此将字体更改应用于该文本组件。

If you really want the text field to show the selection then you need to create a custom Caret and override the focusLost method to NOT invoke setSelectionVisible(false) (which is the default behaviour. 如果您确实希望文本字段显示选择内容,则需要创建一个自定义的Caret并重写focusLost方法以调用setSelectionVisible(false) (这是默认行为)。

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

public class DefaultCaretTest extends JFrame
{
    public DefaultCaretTest()
    {
        JTextField textField1 = new JTextField("Text Field1   ");
        JTextField textField2 = new JTextField("Text Field2   ");

        textField1.setCaret(new SelectionCaret());
        textField2.setCaret(new SelectionCaret());

        textField1.select(5, 11);
        textField2.select(5, 11);
        ((DefaultCaret)textField2.getCaret()).setSelectionVisible(true);

        add(textField1, BorderLayout.WEST);
        add(textField2, BorderLayout.EAST);
    }

    static class SelectionCaret extends DefaultCaret
    {
        public SelectionCaret()
        {
            setBlinkRate( UIManager.getInt("TextField.caretBlinkRate") );
        }

        public void focusGained(FocusEvent e)
        {
            setVisible(true);
            setSelectionVisible(true);
        }

        public void focusLost(FocusEvent e)
        {
            setVisible(false);
        }
    }

    public static void main(String[] args)
    {
        DefaultCaretTest frame = new DefaultCaretTest();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

Of course the selection will remain when focus is on any other component, not just the combo box. 当然,当焦点放在任何其他组件而不只是组合框上时,选择仍将保留。

You can also use: 您还可以使用:

comboBox.setFocusable(false);

Since the combo box can't gain focus the focus will remain on the text component, but the problem with this is that the user won't be able to use the keyboard to select a font size from the combo box. 由于组合框无法获得焦点,因此焦点仍将停留在文本组件上,但是问题在于用户将无法使用键盘从组合框中选择字体大小。 A proper GUI design always allows the user to use either the keyboard or the mouse to perform an action. 正确的GUI设计始终允许用户使用键盘或鼠标来执行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM