All I want to do is have a JOptionPane inputDialog with a JTextArea instead of a JTextField.
I tried putting the JTextArea inside of the Message parameter like so
Object[] inputText = new Object[]{new JLabel("Enter Graph Information"),
newJTextArea("",20,10)};
graphInfo=(String)JOptionPane.showInputDialog(null,
inputText,
"Create Graph",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"");
But it still has the text field at the bottom and I cannot get the text from the JTextArea.
Is there any way to either remove the original text field and get the text from the jtextarea or replace the text field with the text area completely? I'm trying to avoid having to make a custom dialog if possible and this "seems" like something that should be easy to do?
You're on the right lines; you just need to use showConfirmDialog instead of showMessageDialog, which allows you to pass any Component as your "message" and have it displayed within the JDialog. You can then capture the contents of the JTextArea if the user clicks OK; e.g.
int okCxl = JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(this),
textArea,
"Enter Data",
JOptionPane.OK_CANCEL_OPTION)
if (okCxl == JOptionPane.OK_OPTION) {
String text = textArea.getText();
// Process text.
}
If you want to show a JLabel in conjunction with your JTextArea you can create and pass in a JPanel containing both Components; e.g.
JTextArea textArea = ...
JPanel pnl = new JPanel(new BorderLayout());
pnl.add(new JLabel("Please enter some data:"), BorderLayout.NORTH);
pnl.add(textArea, BorderLayout.CENTER);
JOptionPane.show...
Related
I have a JPanel containing a JButton Like The Following Photo:
The problem is that if I typed a text like "Test Name" it is OK because the size of the button is fit!
But if I typed a text like "Test Name Example" or any other big text I need it to do like the following if it is possible!
Note: Code for the button
JButton btnfast = new JButton();
btnfast.setSize(120, 120);
btnfast.setMinimumSize(btnfast.getSize());
btnfast.setBackground(Color.white);
btnfast.setForeground(Color.black);
btnfast.setFont(Classes.Setting.fontDef);
btnfast.setText("Test Name Example");
btnfast.setAlignmentX(Component.CENTER_ALIGNMENT);
pd2.FastAddPanal.add(btnfast);
When i type the text on textfield1 same text need to be display on textfield2 in java. i tried but i couldn't do it. if i remove the text on textfield1 same text should remove on textfield2.what i tried already so far i attached below.
this is code i tried
private void txtnameFocusGained(java.awt.event.FocusEvent evt) {
String name = txtname.getText();
txtname2.setText(name);
}
enter image description here
So I'm trying to make a simple dialog where the user can input some information... My problem is that I'm trying to make the whole background white; I got MOST of it, but there's a gray line behind the buttons that I don't know how to fix (make white as well). How can I fix it? :(
What it looks like:
What I want:
Code:
JPanel all = new JPanel();
all.setLayout(new BorderLayout());
all.add(names, BorderLayout.NORTH);
all.add(academic, BorderLayout.CENTER);
all.setBackground(Color.WHITE);
all.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); //int top, int left, int bottom, int right
Object [] options = {"SAVE", "EXIT"};
JOptionPane jop = new JOptionPane(all, JOptionPane.PLAIN_MESSAGE , JOptionPane.YES_NO_OPTION, null, options, null);
final JDialog dialog = jop.createDialog(null, "Username Information");
jop.setBackground(Color.WHITE);
dialog.setBackground(Color.WHITE);
dialog.setLocation(585, 300);
dialog.setVisible(true);
String choice = (String) jop.getValue();
I am trying to make a JTextArea and a JTextField that has pre-set text in it already so when the user clicks in the box, they can edit it so something else.
Here's what I have for my text field:
private JTextField gridSizeField = new JTextField(6);
and my text area:
private JTextArea status = new JTextArea(20, 20);
private JScrollPane statusScroller = new JScrollPane(status);
You can use one of the overloaded constructors that accepts a String as an argument.
JTextField(String text, int columns)
JTextField gridSizeField = new JTextField("Your Text", 15);
Same for JTextArea
JTextArea(String text, int rows, int cols)
JTextArea status = new JTextArea("Your status", 20, 20);
You should have referred to the JavaDocs first
More on JTextArea and JTextField
In order to have text already set inside the JTextArea, it is a parameter of the JTextArea constructor. For example, to have the string "Text already in JTextArea", you just have to do
private JTextArea status = new JTextArea("Text already in JTextArea", 20, 20);
private JTextField field = new JTextField("Text already in JTextField", 6);
Hope it helps!
I have the following code that displays a JDialog, it shows a text field, I assume it's a JTextField.
How do I set the text in it at the beginning when the dialog opens?
JOptionPane pane = new JOptionPane("", JOptionPane.QUESTION_MESSAGE);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(null, "Test");
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setVisible(true);
Use:
pane.setInitialSelectionValue("foo");
to set the input value that is initially displayed as selected to the user.