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
Related
I need to create a number of dialogs that are of the same basic structure that looks like this:
There will be a varying number of rows, each with a labeled checkbox and two combo boxes that have integers, the range of which varies. The check box just enables the combo boxes. When the selection in the first combobox is changed, the second one gets initialized and enabled.
Since I have to do over 50 over these I'd like automate the programming. I believe some of the code can be handled with loops, selecting combobox names from preset string arrays. What I can't figure out is how to parameterize things like action listeners.
First question is can this be done at all. If it can, how?
Ed
First create a notional RowModel containing a Boolean value for the checkbox, a String for the label and two instances of ComboBoxModel, one for each of the combos. Handle the combo dependency as shown here. Let your program maintain a List<RowModel> for each distinct dialog. You can manage an arbitrary number of rows in a suitable TableModel and display them is a JTable as shown here.
Actually, I just want to say that MadProgrammer provided an answer that worked for me in his Comment.
Say a game allows you to choose a number of players, and then asks for each player's name based on that number.
The first user inputs their name in a JTextField, and clicks a JButton (which stores their name in a list).
After the button is clicked, the JPanel erases the user's name from the JTextField so that the second person may enter their name, and so on.
How would I do this? Would I use a loop?
How would I do this? Would I use a loop?
Nope. Using a loop is a construct that you'd use in a linear console-driven programming environment to get repeated input from the user. Instead Swing is an event-driven library, and in this situation you have to think differently since here you'd use state-dependent interaction with the user.
So say you wanted to get 5 records of information from the user, you'd use a counter, and each time the submit button is pressed, you'd get the input from the GUI components, create an object with them add them to whatever collection is holding the information, perhaps an ArrayList or a JTable's model, and then when the maximum number is reached by the counter (the state variable that you'd be monitoring), you'd stop getting input, and perhaps even change the GUI view entirely to reflect this change in state, something that you could use a CardLayout to help with.
I am trying to create a text based adventure in Java I need help in combining a Scanner with JPanel so that the user can type into the panel directly rather than choosing from a list of options. Is there any way of doing this or would a dialogue be needed and if so is there a way of doing so within the JPanel?
For doing so, you need to add some components to your panel, such as, JtextField or JTextArea.
Then you have to add a action listener to your text field/area.
You can find a lot of advices with a simple google search.
Sorry, I'm kind of a beginner to GUI interfaces (well, a beginner to java, really), and I was wondering: How does one make a list of items that the user can add items to or remove items from, with the press of a + or - button?
What I really want (sorry if I'm being a little vague here) is one of those lists you sometimes see in application windows, which looks like a text box but cannot be typed in. Right now my application (it's a small app to organize the schedule of a hospital) just reads from a text file and writes to another text file. No GUI, no window, the user just writes a bunch of names in the text file, one per line, then runs the jar and opens the output file and the schedule is there. I want them to just be able to add or remove names from a list with buttons — adding a name by clicking 'plus' and typing the name, and removing a name by selecting the name and clicking 'minus'. I still, however, want it to save to a text file, so that the next time the user opens the app, all the names are still on the list.
Just to be clear, I don't want a list displaying the output (i.e. the names organized into a schedule), just one containing the input.
Thanks a lot for any help you can give.
You probably want a JList with a couple of JButton instances to add/remove items.
See:
How to Use Lists
How to Use Buttons, Check Boxes, and Radio Buttons
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... :)