SWT Combo Box Default selection - java

Im a newbie to swt..I have an SWT Combo box with 2 entries. Currently, it shows a blank selection as the user did not select any. Is there any way to make sure that even if the user does not select any entry in the combo box, he still gets to see the default value selected, is this even possible?..

Use the Combo's select method to select the default option when you create it.

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.

Java JComboBox selection checked but the combobox doesn't show the selected item

I have a situation that I want some advice on. The listeners of a JComboBox all work as I'd expect.
The JComboBox is created using the constructor that takes an array as argument. The argument is an ArrayList which is converted using the toArray method. The JComboBox is setEditable(false) and setEnabled(false). I'm not sure what else to tell you. JDK 1.8, Mac OS X 10.9.5 and (mis)using NetBeans 8.0.2
What is wrong is that although the underlying values and computations happen when you select, say "-4", i.e. all listeners get the value correctly, the GUI does not display "-4" but rather, it displays the previous selection.
This does not happen every time. When it does happen, if I (change the focus?) Cmd-Tab (on a Mac) away or click on another window (any window) and come back, the value displays correctly.
If I click on the drop-down and see the list, the check mark is definitely on the selected item, not the one currently displayed. And when I let go (without selecting) the screen is corrected.
I've done some things to try to get it to update, most are grasping at straws (ItemListener that sets the selected item to the one it just got; repaint() here and there...).
This is annoying to the users. They doubt they have selected, so they select twice. Or they select and move. All logs and debug show the selected item is the one they intended.
One of these JComboBox (dropdowns) has a button beside it, that takes the name selected and loads a set of values from a setup file. One can watch the displayed (and not selected) name change to the selected name when the button is pressed.
Thanks for any advice.

Populate a JComboBox with list of Users from a text file

I have created a JFrame application in Netbeans, which has user creation and user log in for a tutorial application I am making, how would I populate a JComboBox from a text file when a user is created?
I have created the application/user creation, But I am stuck on the JComboBox selection from the file... I have seen multiple things on how to, but nothing is working at this time.
The application takes a persons:
First Name
Last Name
Desired Username (The thing I want in the JComboBox)
And the log-in is literally just selecting the user from the JComboBox and listing all users created and clicking a button to log-in. The text file is located: F:\ProjectDecember\Users.txt
Any help would be appreciated.
Thank You.
Check out Combo Box With Custom Renderer.
It shows how you can create a custom renderer for your custom Object that you add to the combo box. It also shows an approach that you can use so you don't break the default functionality of the combo box when you use this custom renderer.

java swing combo box

I am working on a UI design project in java swing where in I am expected to query a database based on the value selected from the Combo box. I have five combo boxes and a text field. I use .getSelectedItem() to include the value selected from the combo box in the where condition. If I run the query, apparently the first entry in the combo box which according to my design is Select.. gets selected and it returns nothing.I want the first entry to be a null value so that unless and until I select a value from combo box it remains null. Please help me out. Thank you.
I want the first entry to be a null value so that unless and until I select a value from combo box it remains null.
You should not use a null item in the combo box. However, after you load the data into the combo box you can use:
comboBox.setSelectedIndex(-1);
and no item will be selected in the combo box.
You may also want to consider using the Combo Box Prompt class. It will display a "hint" for the usage of the combo box until the user selects an item.
You can create a wrapper class that wrap the actual value like:
public class MyComboItem {
public MyComboItem(Object actualValue) {
this.actualValue = actualValue;
}
public Object getActualValue() {
return this.actualValue;
}
}
You may need to override toString() in this class so the combo box knows how to render the items property.

How can double-clicking be disabled for just one portion of a JTable?

I have a JTable with row selection enabled. Before today, my desired functionality was for double-clicking on any given row to open up a new window. And before today, that worked just fine.
I've just added a column of JCheckBoxes to the table. Selecting and deselecting individual checkboxes in the new column works fine, in general. However, if I select a checkbox and quickly deselect it, the table interprets my actions as a double-click on the checkbox's row, which is not what I want.
Is there a way to disable the double-clicking behavior for just the checkboxes, but keeping row selection enabled otherwise? If not, how about disabling the behavior for just one column of a table? If so, how?
If not, how about disabling the behavior for just one column of a table?
Use the table.columnAtPoint(...) method to ignore double clicks on the columm with the checkbox.
In the MouseListener you could check the state of the checkbox of the selected row for each click and if the second click's state doesn't match the first click's state then don't open a new window.

Categories