I am a student at ISU. I was working on a homework assignment where I wanted to get text that was selected (highlighted) in a TextArea to appear in a JOptionPane dialog. I tried many of the methods for JOptionPane, but I could not get any of them to place the text the user selected in the input field of the dialog.
I guess that I could make a one element String array and pass this to the JOptionPane constructor listed.
JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options, Object initialValue)
This is not way I wanted to implement the JOptionPane. Please give me any help you can. Thanks and there is no hurry as the assignment has been handed in.
This tutorial shows how to use JOptionPane and how to get input from the user.
Read it to see if works for you or not.
How to make dialogs
I guess that I could make a one element String array and pass this to the JOptionPane constructor listed.
Sounds correct to me.
This is not way I wanted to implement the JOptionPane.
What's wrong with that? Or how do you want to implement it?
Take a look at that article perhaps you could manage to write something like:
JOptionPane.showMessageDialog(frame, getSelectedTextFrom( someTextArea ) , "Message");
Where the method:
String getSelectedTextFrom( JTextArea )
Will return... well the selected text from the text area... :)
Related
I am creating a log in form in Java. I have already completed the entire structure of the program, as well, I have designed it's purpose and perfected it's functionality. However, now I am focusing on styling the program. I'm proud of it as it is and it's fine if I don't add this feature, however I really would like to and cannot discover how.
In summary, when a username is typed into the login form, I have a void that runs after the JTextField loses focus. This void searches for possible invalid characters such as spaces. I can successfully change the color of the border on my JTextField, and other attributes I wish to change, however I also would like to have a small dialogue box to hover over the JTextField to say what specifically is wrong with what was typed. (e.g. "Your username cannot contain spaces!").
Ideally, it would be a rectangle that could simply be filled with text, and only appear when the username is deemed incorrect, and be able to disappear when it is fixed ( I can handle the appearance and disappearance most likely, I just need help with creating this box thing ).
Is there any such thing as like a "JHoverBox" or something that I could add to my JTextField?
Well, you can have, as Gene said, a label that's normally empty near the text field, but you can have it coloured like the background colour. This will make the user not able to see it, and when you detect something wrong, you can change the colour and add the text. Simple!
I'm having trouble figuring out how to make a Tabbed Joptionpane for user input. Basically I need to take multiple values from user input, but instead of multiplied dialog boxes, I would like them to be able to enter all the separate data into one box. Is this possible? I can't seem to figure it out. In Java btw.
You can create custom JOptionPane. Basically you define a JPanel that will contain the input fields, and organize this panel as you want. See : Multiple input in JOptionPane.showInputDialog
This one may be easy for you. But I'm stuck and can't figure out an algorithm for doing that. I want to show a JTextField and change the text on the JButton to "Hide" if it's "Search". If
the text on the JButton is "Search" a JTextBox should appear and vice versa, if the text is "Hide" make the JTextField invisivle and change the text on JButton to "Search"
This is how I have done it:
private void switchBtnText(){
searchTxtField.setVisible(true);
btnSearch.setText("Hide");
if(btnSearch.getText().equals("Hide")){
btnSearch.setText("Search");
searchTxtField.setVisible(false);
}
}
If I comment the if section it works to show the JTextField. My problem is to go back to the default settings which is a JButton with "Search" as a text and with an invisible JTextField.
The method is then called in an ActionEvent. I've done this before, in C#, so I know I'm close.
Thank you in advance. The fastest and best answer will get upvoted and accepted.
This should work although I've not tested it.
//btn action
private void toggleVisible(){
String btnVal = btnSearch.getText();
if(btnVal.equals("Search")){
searchTxtField.setVisible(true); // or however you are showing search field
btnSearch.setText("Hide");
}else{
searchTxtField.setVisible(false);
btnSearch.setText("Search");
}
}
Take a look at your execution sequence....
setText to "Hide"
if text equals "Hide", change text to "Show"
Try changing the logic so you check the text first, then make decisions about what should be done...
If text equals "Hide", change text to "Show"
Else, change text to "Hide"
I'm making an xml editor as one of our projects in class, and for adding an attribute, I am currently doing this:
String name = JOptionPane.showInputDialog(this, "Enter the attribute name: ", "Name", JOptionPane.INFORMATION_MESSAGE);
String value = JOptionPane.showInputDialog(this, "Enter the attribute value: ", "Value", JOptionPane.INFORMATION_MESSAGE);
Is there a better way to have just a single dialog box with both those things on it? I looked at some examples but I am having trouble implementing/understanding them. While I am able to correctly add attributes with the current method, it is kind of silly to have two input boxes.
Please let me know if there is some simple solution to this. Thanks
Yes, you can create a JPanel that holds two JTextFields and pop that into a JOtionPane.showConfirmDialog(....), and then when it returns, if the user presses the OK button, extract the text from the JTextFields.
For instance, please check out my code in this answer
So you can but you'll need to use the version that takes an Object( thanks hovercraft) , look at the Java 6 JOptionPage , there are variants that take more than one!
I want to create a pop up box that is activated when I click a "Check" button. What it does is ask the user if they are sure about what they requested. If yes, it does the request. If no, it goes back to it's normal state.
I know what I said is a bit ambiguous but I want to create various types of pop up boxes.
So I was wondering is there a website that has generic pop up boxes with the code given?
I just need a simple code which I can expand on.
I think JOptionPane is what you want.
showConfirmDialog: Asks a confirming
question, like yes/no/cancel.
showInputDialog: Prompt for some
input.
showMessageDialog: Tell the
user about something that has
happened.
showOptionDialog: The Grand
Unification of the above three.
A small example to get a Yes-No popup like you asked for would be:
if (JOptionPane.showConfirmDialog(null, "Are you sure about your request?", "Request",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
== JOptionPane.YES_OPTION)
{
//Do the request
}
else
{
//Go back to normal
}
This solution, however, only works if you are using Swing.
You shoudl check JDialog to create your custom message dialogs or you can use standard message dialogs in JOptionPane.