[英]How to add text area on JOptionPane
Currently I have a JOptionPane
. 目前我有一个
JOptionPane
。 On clicking a button I am executing following line. 单击按钮时,我正在执行以下行。
JOptionPane.showInputDialog(this,"Enter your message","Messages",2);
It opens a popup with a text box. 它会打开一个带有文本框的弹出窗口。 This text box accepts around 40/50 characters.
此文本框可接受大约40/50个字符。 In fact my requirement is to take long message (upto 300 characters) in this text box which is stopping this.
实际上我的要求是在这个文本框中取消长消息(最多300个字符),这样就可以了。 So we want to come up with a text area on this input dialog?
所以我们想在这个输入对话框中找到一个文本区域? Is it possible how?
有可能怎么样? Is there any better solution?
有没有更好的解决方案? Thanks in advance.
提前致谢。
将JTextArea
放入JOptionPane.showConfirmDialog()
( QUESTION_MESSAGE
类型),然后在放置对话框后查询文本区域的内容(并检查返回结果以确保用户未取消该对话框)。
While I'm not sure whether using the JOptionPane is the best option for the purpose, you can add a text area, or any other form component into an OptionPane. 虽然我不确定使用JOptionPane是否是最适合此目的的选项,但您可以将文本区域或任何其他表单组件添加到OptionPane中。 Since you have asked specifically for a TextArea, I have added a JTextField and a JTextArea in the following example.
由于您专门询问了TextArea,因此我在以下示例中添加了JTextField和JTextArea。
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Option Pane Text Area Example");
final SpringLayout layout = new SpringLayout();
final JPanel panel = new JPanel(layout);
panel.setPreferredSize(new Dimension(250, 160));
JLabel lblName = new JLabel("Name");
panel.add(lblName);
JTextField txtName = new JTextField(10);
txtName.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(txtName);
JLabel lblAddress = new JLabel("Address");
panel.add(lblAddress);
JTextArea txtAddress = new JTextArea();
txtAddress.setBorder(BorderFactory.createLineBorder(Color.black));
txtAddress.setLineWrap(true);
txtAddress.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(txtAddress,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(250, 100));
panel.add(scrollPane);
layout.putConstraint(SpringLayout.WEST, lblName,
0,
SpringLayout.WEST, panel);
layout.putConstraint(SpringLayout.NORTH, lblAddress,
10,
SpringLayout.SOUTH, lblName);
layout.putConstraint(SpringLayout.WEST, txtName,
25,
SpringLayout.EAST, lblName);
layout.putConstraint(SpringLayout.NORTH, scrollPane,
10,
SpringLayout.SOUTH, lblAddress);
int result = JOptionPane.showConfirmDialog(frame, panel,
"Text Box and Text Area Example", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
System.out
.println(txtName.getText() + ",\n" + txtAddress.getText());
} else {
System.out.println("Canceled");
}
System.exit(0);
}
}
However, I suggest its better you use a JDialog instead of the JOptionPane for complex input forms like these. 但是,我建议你更好地使用JDialog代替JOptionPane来处理复杂的输入形式。
/**
*
* @param obj
* @param text
* @param title
* @return newText String
*/
public static String textAreaDialog(Object obj, String text, String title) {
if(title == null) {
title = "Your input";
}
JTextArea textArea = new JTextArea(text);
textArea.setColumns(30);
textArea.setRows(10);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setSize(textArea.getPreferredSize().width, textArea.getPreferredSize().height);
int ret = JOptionPane.showConfirmDialog((Component) obj, new JScrollPane(textArea), title, JOptionPane.OK_OPTION);
if (ret == 0) {
return textArea.getText();
} else {
MyDialogs.Toast("Canceled by user\nChanges not saved", "Your choise");
}
return null;
}
public static void Toast(Object msg, String title) {
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.OK_CANCEL_OPTION);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.