SetSelectedItem doesnt jump to selected item after clicking the box - java

I have a combobox which I fill and pick the needed item with selectedItem but not with selectedIndex.
In case of selectedIndex it sets the needed value in the combobox, and highlights the selection. but in case of selectedItem it selects what I need, but when i press the box it shows list from the very begining.
how to set the selected item without using the setSelectedIndex ?

It would have been better if u had posted code here. but yet.. Please read below text from javaDocs about setSelectedItem:
Sets the selected item in the combo box display area to the object in the argument. If anObject is in the list, the display area shows anObject selected.
If anObject is not in the list and the combo box is uneditable, it will not change the current selection. For editable combo boxes, the selection will change to anObject.
If this constitutes a change in the selected item, ItemListeners added to the combo box will be notified with one or two ItemEvents. If there is a current selected item, an ItemEvent will be fired and the state change will be ItemEvent.DESELECTED.If anObject is in the list and is not currently selected then an ItemEvent will be fired and the state change will be ItemEvent.SELECTED.

Related

implement a button in table, when clicked, open a dialog box, displays the values of a specific cell as Dropdown/ checkbox button

So I want to implement a button for each row in my table.
Now, The functionality that I want my button to provide is:
CLICK -> OPENS A DIALOG BOX -> DISPLAY THE VALUE FROM A SPECIFIC CELL (which is in the same row as the button) -> DISPLAYS THE VALUES AS A DROPDOWN OR CHECKBOX BUTTONS.
The value in the cell are being populated by using ResultSet.getString method taking the values from a database.
Thanks.

JComboBox event for when value is changed by choosing a new value from the drop-down lost

I have a form type application in which the JComboBox I use to represent the title(Mr/Mrs/Ms/etc.) of the client. I want to trigger an event when the combo box has changed values due to the user clicking it to open the drop-down list, and then choosing a new value. I also have the client's names in a left panel. Clicking on a client's name will change the JComboBox to the value stored for that client, but I don't want to trigger the event when the value of combo box changes this way.
Both ActionListener and ItemChangeListener will trigger an event for both of these cases
The MousePressed method from the MouseListener will trigger the event for when the JComboBox is clicked, but not when the arrow next to the JComboBox is clicked. Also, the MousePressed event will trigger regardless of whether or not the selected item in the combo box has changed.
Before you trigger the event when the client's name changes remove the ItemChangeListener and add it back afterwards. That way it will "skip" the notification

Get selected menu item & display the corresponding item on frame

I have 3 conditions (3 menu items in menu). In those 3 items, how to get one menu item selected as soon as the frame opens and displays the content of that item by default?
Later on if we select other menu items, then corresponding contents on the frame will be displayed as usual. I have written very lengthy code for this GUI, so unable to paste here. But got stuck up at this point.
Simply invoke the actionPerformed() method of your ActionListener after your GUI is fully constructed. This is particularly easy if you have implemented the Action interface, as shown in How to Use Actions.
Using this example, add the following line near setVisible() to simulate adding a few random nodes to the graph:
gp.control.random.actionPerformed(new ActionEvent(gp, 0, null));
To simulate clicking a button, this line simulates adding a selected node:
gp.control.defaultButton.doClick();
By default all menu items are not selected when they are created. To make a menu item selected before you show it in your application you should change the state of the model. For the JMenu items it's easy by setSelected(true) and setPopupMenuVisible(true). For the JMenuItem items you have to setArmed(true). You can return back to the default state in the actionPerformed.

JTable cell value passed to textarea on single click

On my form I have a jtable and a textarea. My table only has 2 columns - ID and Comment
Is it possible that when a user clicks on a cell in the comment column. The cell value will appear in the textarea in edit mode?
I did set the cell editor to singleclick
selectTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
TableColumn col = selectTable.getColumnModel().getColumn(1);
DefaultCellEditor singleclick = new DefaultCellEditor(new JTextField());
singleclick.setClickCountToStart(1);
col.setCellEditor(singleclick);
I have a method outputSelection() that gets called from a edit button. The method gets the value from the selected cell and puts the value in the textarea for edit.
Can the click activate the method so the user does not have to click a edit button?
You could attach a mouse listener to the table and monitor the mouse clics from there, getting the selected column/row & thus the value
You could supply your own cell editor that updates the text area when the editors value is set
You could extend te jtable & when cell editing is started, update the text area
Yes, this is a process I learned to use after having duplicate code throughout my swing application. I started making standalone methods that did the work I wanted, and then I call those methods from the action events from the button or mouse click. That way they all execute the same code.
Even if you have a tab or enter key command, you can also have it execute your same method as the others for more consistent code.
If your button performs specific code with cell values, just extract all of that code out into a method that takes the cell value as input. Then you can call that same method from any event and pass in the input data you want to display in the text area.

linking jcombobox values with jtextfield values

I've created an application in netbeans IDE 6.9 where I need to set values for each value in the jcombobox. In my pane I've a combobox and below that are the textfields for entering values for each value in the combobox. Can anyone suggest that how do I link the combobox with textfield. I mean there are different values for each value in the combobox. I want that user selects a value in the combobox then its corresponding value should be displayed(if it has already been entered) otherwise a blank space should be shown. I want that all the values for each combobox values should be set in one go(the user should not press the ok button).
-Thanks in advance
Wouldn't you want to use an ActionListener? Then when an ActionEvent happens for the combo box you could fill the text field with the values from the currently selected item? And if blank then allow them to add to the text field and have an ActionListener on that where if the value is not in the list that is in the combo box to add it to the list in the combo box?
I can only guess from the question that each item in your combo box is an object and you want to edit multiple fields of the selected item.
You could use a bunch of individual text fields, one for each "value" in the selected "value in the combobox".
A better UI would be a property pane to list and edit the fields. The commercial PropertyGrid in JIDE Grids can actually combine the combobox and property pane in one place.
You can commit each field value after it is entered, or commit all when the editor loses focus (for example when you select another item in the combo).
jComboBox1.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
String str = (String)jComboBox1.getSelectedItem();
jTextField1.setText(str);
}
});

Categories