如何隐藏 JEditorPane 的插入符号?

问题描述 投票:0回答:1

我有一个 JEditorPane,有时不应该显示其插入符号。
在下面的代码中,我尝试根据 JCheckBox 状态切换插入符号可见性 - 但徒劳。

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

public class HideCaretInEditorPane extends JFrame {

  public HideCaretInEditorPane() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(230, 200);
    setLocationRelativeTo(null);

    JEditorPane pane = new JEditorPane("text/html",
    "To see the caret's visiblity, the EditorPane must have the focus.");
    pane.setEditable(false);
    pane.getCaret().setVisible(false);
    add(pane, BorderLayout.CENTER);

    JPanel p= new JPanel();
    JCheckBox cbVisible= new JCheckBox("Visible");
    cbVisible.addActionListener(e -> {
//    Failing attempt to toggle caret visibility.
      boolean b= !pane.getCaret().isVisible();
      System.out.println(b); // Always true, i. e. the caret is erroneously reported as not visible.
//      pane.getCaret().setVisible(b);
      pane.getCaret().setVisible(false); // No effect.
      pane.requestFocusInWindow();
    });
/*
    cbVisible.addActionListener(e -> {
//    Toggle both editability and caret visibility. -> Caret blinks when editable,
//    but is always visible.
      boolean b= !pane.isEditable();
      System.out.println(b);
      pane.setEditable(b);
      pane.getCaret().setVisible(b);
      pane.requestFocusInWindow();
    });
*/
/*
//  Workaround
    cbVisible.addActionListener(e -> {
      Color color= pane.getCaretColor();
      pane.setCaretColor(color==Color.WHITE ? Color.BLACK : Color.WHITE);
      pane.requestFocusInWindow();
    });
*/
    p.add(cbVisible);
    add(p, BorderLayout.SOUTH);
    setVisible(true);
  }
 
  public static void main(String args[]) {
    EventQueue.invokeLater(HideCaretInEditorPane::new);
  }

}

第二次尝试和解决方法已作为评论找到。
我仍然想知道,为什么我第一次尝试在插入符上使用 setVisible(...) 不起作用。

Win10、Java 22

java swing caret jeditorpane
1个回答
0
投票

我创建了一个简单的 GUI 来说明删除

JEditorPane
插入符号。

example

当 GUI 首次出现时,编辑器窗格是可编辑的。插入符号在文本末尾闪烁。按下底部的按钮后,编辑器窗格将不可编辑。没有插入符号。

再次按下底部按钮后,编辑器窗格再次可编辑。但是,没有插入符号。您必须在编辑器的某个位置左键单击才能使插入符号再次出现。

这是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Caret;

public class JEditorPaneExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JEditorPaneExample());
    }

    private JEditorPane editorPane;

    private JPanel editorPanel;

    @Override
    public void run() {
        JFrame frame = new JFrame("JEditorPane Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        editorPanel = createEditorPanel();
        frame.add(editorPanel, BorderLayout.CENTER);
        frame.add(createButtonPanel(), BorderLayout.SOUTH);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createEditorPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        editorPane = new JEditorPane();
        editorPane.setText("This is a test of the emergency broadcast system.");
        JScrollPane scrollPane = new JScrollPane(editorPane);
        scrollPane.setPreferredSize(new Dimension(300, 200));

        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createButtonPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JButton button = new JButton("Not Editable");
        panel.add(button);

        button.addActionListener(event -> setEditorState(event));

        return panel;
    }

    public void setEditorState(ActionEvent event) {
        JButton button = (JButton) event.getSource();
        String text = button.getText();
        if (text.equals("Not Editable")) {
            editorPane.setEditable(false);
            button.setText("Editable");
        } else {
            editorPane.setEditable(true);
            button.setText("Not Editable");
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.