在属于 JOptionPane 的 JTextArea 中选择/突出显示文本
Selecting / highlighting text in a JTextArea belonging to a JOptionPane
我的问题如下:
在我的应用程序中,用户单击一个按钮会弹出一个对话框(自定义 jOptionPane)。此对话框包含一个 JTextArea,用户将在其中键入一个响应,然后由应用程序处理,但是我希望这个 JTextArea(它将保存用户的输入并且当前包含 "Write your answer here" 之类的示例文本)是自动突出显示。
我可以通过在 JTextArea 上调用 requestFocusInWindow() 然后调用 selectAll() 来正常执行此操作,但是当使用 JOptionPane 完成此操作时似乎存在问题,我猜这与以下事实有关焦点无法成功转移到 JTextArea。
我已经制作了一个 SSCCE 来清楚地证明这一点,并希望从你们中的一个人那里得到关于我如何使这成为可能的答案。提前致谢!
Class 1/2:主要
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Main extends JFrame{
public static void main(String[] args) {
Main main = new Main();
main.go();
}
private void go() {
JPanel background = new JPanel();
JPanel mainPanel = new ExtraPanel();
((ExtraPanel) mainPanel).setupPanel();
JButton testButton = new JButton("Test the jOptionPane");
testButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
optionPaneTest();
}
});
background.add(mainPanel);
background.add(testButton);
getContentPane().add(background);
pack();
setVisible(true);
}
private void optionPaneTest() {
JPanel testPanel = new ExtraPanel();
((ExtraPanel) testPanel).setupPanel();
int result = JOptionPane.showConfirmDialog(null, testPanel,
"This is a test", JOptionPane.OK_CANCEL_OPTION);
}
}
-------------------------------------------- ------------------------------
Class 2/2 : 额外面板
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class ExtraPanel extends JPanel{
public void setupPanel() {
JTextArea textArea = new JTextArea();
textArea.setText("Write your response here");
textArea.requestFocusInWindow();
textArea.selectAll();
add(textArea);
}
}
只需添加
textArea.getCaret().setSelectionVisible(true)
textArea.selectAll();
之后
如果您希望焦点在 TextArea 中以便用户可以立即开始键入,您可以使用祖先添加事件触发选择。
public void setupPanel() {
final JTextArea textArea = new JTextArea();
textArea.setText("Write your response here");
textArea.addAncestorListener(new AncestorListener() {
public void ancestorRemoved(AncestorEvent event) { }
public void ancestorMoved(AncestorEvent event) { }
public void ancestorAdded(AncestorEvent event) {
if (event.getSource() == textArea) {
textArea.selectAll();
textArea.requestFocusInWindow();
}
}
});
add(textArea);
}
我的问题如下:
在我的应用程序中,用户单击一个按钮会弹出一个对话框(自定义 jOptionPane)。此对话框包含一个 JTextArea,用户将在其中键入一个响应,然后由应用程序处理,但是我希望这个 JTextArea(它将保存用户的输入并且当前包含 "Write your answer here" 之类的示例文本)是自动突出显示。
我可以通过在 JTextArea 上调用 requestFocusInWindow() 然后调用 selectAll() 来正常执行此操作,但是当使用 JOptionPane 完成此操作时似乎存在问题,我猜这与以下事实有关焦点无法成功转移到 JTextArea。
我已经制作了一个 SSCCE 来清楚地证明这一点,并希望从你们中的一个人那里得到关于我如何使这成为可能的答案。提前致谢!
Class 1/2:主要
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Main extends JFrame{
public static void main(String[] args) {
Main main = new Main();
main.go();
}
private void go() {
JPanel background = new JPanel();
JPanel mainPanel = new ExtraPanel();
((ExtraPanel) mainPanel).setupPanel();
JButton testButton = new JButton("Test the jOptionPane");
testButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
optionPaneTest();
}
});
background.add(mainPanel);
background.add(testButton);
getContentPane().add(background);
pack();
setVisible(true);
}
private void optionPaneTest() {
JPanel testPanel = new ExtraPanel();
((ExtraPanel) testPanel).setupPanel();
int result = JOptionPane.showConfirmDialog(null, testPanel,
"This is a test", JOptionPane.OK_CANCEL_OPTION);
}
}
-------------------------------------------- ------------------------------
Class 2/2 : 额外面板
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class ExtraPanel extends JPanel{
public void setupPanel() {
JTextArea textArea = new JTextArea();
textArea.setText("Write your response here");
textArea.requestFocusInWindow();
textArea.selectAll();
add(textArea);
}
}
只需添加
textArea.getCaret().setSelectionVisible(true)
textArea.selectAll();
如果您希望焦点在 TextArea 中以便用户可以立即开始键入,您可以使用祖先添加事件触发选择。
public void setupPanel() {
final JTextArea textArea = new JTextArea();
textArea.setText("Write your response here");
textArea.addAncestorListener(new AncestorListener() {
public void ancestorRemoved(AncestorEvent event) { }
public void ancestorMoved(AncestorEvent event) { }
public void ancestorAdded(AncestorEvent event) {
if (event.getSource() == textArea) {
textArea.selectAll();
textArea.requestFocusInWindow();
}
}
});
add(textArea);
}