Checkbox List in JDialog - java

I'm trying to use a JDialog box that has a search text field that as text is entered, it shortens a list to those that match. On this list I would like to display each line with a checkbox that can be selected. I might also want the ability for it to function like a select all list where Ctrl + Click on the line would select the item and check its box. How do I get this done?

Jlist is exactly what you're looking for. You can use a list of checkboxes and ctrl-click them.

I would use a JTable with two columns. The first column can be your check box and the second column could be the text.
Read the section from the Swing tutorial on "How to Use Tables" in particular the part on Sorting and Filtering for an example.

Related

What text to put to a JComboBox where you want to prompt the user to selection?

I am using multiple JComboBox in a single JPanel. I was wondering what is the go to initial JComboBox selection text?
For example I can put "None" but that will seem as if some value is already selected in the JComboBox. I am using labels for which type of data they are so writing their type is unnecessary.
I can either put "" but something like "----" seems better. Is there a standard text when you want the user to select some value from JComboBox?
If you want to set an initial value to JComboBox that should be non-selectable (as soon as user selects another option, it will not be possible to select the initial one again), than it has been already answered here.
One way is to use a custom renderer. The renderer will simply display a default prompt when no item of the combo box has been selected.
Check out Combo Box Prompt for an example of this approach.

Create a right click popup menu in a text area only if it is not empty and getting the selected index

I've seen the question answered in How do I create a right click context menu in Java Swing?
and seen how I can create a menu.
However, when I associate it with the text area, it doesn't matter if it has anything written on it or not, it always shows the menu.
I use the text area to reveal how many objects I have created from a specific class (which I save in a ArrayList). What I need, is a menu that when clicking on a specific line of text, it can have the index of the object in the ArrayList and use that menu to Edit/Remove that specified object from the ArrayList.
Is that possible with a Text Area or should I use a different type of displaying component?
As an Example:
Text Area:
Object 1.
Object 2.
Object 3.
When I select , for example, Object 1 with a right click, it shows the menu with Edit and Remove. But when I dont select any of them, the menu does not show.
And when it Shows, it can access the index od the object (object 1 -> index 0, object 2 -> index 1 , etc.)
Thanks a lot for your help,
Nhekas
I use the text area to reveal how many objects I have created from a specific class
Don't use a JTextArea.
Instead I would suggest you should be using a JList. Read the section from the Swing tutorial on How to Use Lists for more information and examples.
The JList has a locationToIndex(...) method which will give you the row where the mouse was clicked. Then you can get the object from the list.

Hide some items from JList Java

I'm trying to hide (not remove) some items from a JList Java, but I can't find the way.
The process I'm trying to realize is a list with JCheckbox and a search form (textfield). When the user fire some text I filter the list, but I need to keep the state of the checkbox, so I can't remove the item, but only hide or show it.
Thanks for the help.
When the user fire some text I filter the list,
JList does not support filtering.
You can use a single column JTable. JTable does support filtering.
Read the section from the Swing tutorial on Sorting and Filtering for more information and working examples.

String Tokenizer.nextElement for JComboBox

I'm using
int TxtAge = Integer.parseInt(tfAge.getText().trim());
to get value from my textfield and search it in database.
Then, I'm using Integer age = Integer.parseInt(stringTokenizer.nextElement().toString()); to go to next attributes in my database.
I have no problem using those codes for textfield but when I'm using the JComboBox the result won't display. How to use the StringTokenizer.nextElement() for JComboBox? Is is the same with TextField?
String sex=(String) stringTokenizer.nextElement();
I tried this code but still failed :(
You seem to have left out the relevant portions of your code, i.e. how you are handling setting/getting items in the JComboBox. Whether you read these values from a database, a file or have them hardcoded is irrelevant to the question.
Since you do ask whether it is the same as with a JTextField, I can at least answer this; it is not the same. The question indicates that you're quite new to Swing. You would probably benefit from working through a basic Swing tutorial, just to get a grip on how to work with these basic GUI elements. For JComboBox, check out Oracles own How to Use Combo Boxes.
Anyways, when working with JComboBox, you will need to first populate it with the values that users can choose from and set the currently selected value. Retrieving the currently selected value is just a simple method call.
Further, you have the possibility of making a combobox editable. This means that the user can edit the text in the combo box to something that was not pre-populated. By default, this option is turned off.
I'll provide some examples.
Initialize:
JComboBox sexComboBox = new JComboBox();
sexComboBox.addItem("Not selected");
sexComboBox.addItem("Male");
sexComboBox.addItem("Female");
sexComboBox.addItem("Do not want to disclose");
By default, the first item you added is selected. To select another one, you need to add one of the following lines:
sexComboBox.setSelectedIndex(1); // zero-based index, "Male" is selected item
sexComboBox.setSelectedItem("Female"); // sets the selected item to "Female"
To enable user to edit the contents to something that was not pre-defined, just add the line:
sexComboBox.setEditable(true);
To retrieve the currently selected value:
String selectedItem = (String) sexComboBox.getSelectedItem();

Working with radio button in java

I've two jRadioButtons in Java application. Let's say Male and Female. Initially one is selected. While selecting another, the previous should be unselected and vice-versa. How to make it work? Also how can it be used to store in database?
You can give same name to all the radio buttons, from which you want to select only one. That way, you will be able to select just one button out of all. This you can do by creating a ButtonGroup.
Now, if you want to add the selected item to the database, just get the value corresponding to the name of Button Group, you will get the value of selected button.
See documentation and tutorial
What you need to use is a ButtonGroup component. Have a look at: How to Use the ButtonGroup Component

Categories