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);
}
});
Related
I am kinda new to JavaFX, so maybe this is very easy to do. I have a ListView<String>, and what I want is that I can have a button which when pressed, basically turns the selected String into a sort of text field in which you can edit the name of that specific item. I hope it is clear what I want. I already tried list.setEditable(true), but that didn't change anything.
In addition to making the ListView editable you also need to make sure to use a cellFactory providing editable cells:
list.setEditable(true);
list.setCellFactory(TextFieldListCell.forListView());
This way you can start a edit on a double click of a cell.
First, I'd use in this case a VBox filled with the TextFields. Those TextFields should have the editable property set to false. When you click on the button, you can call:
((TextField)vbox.getChildren().get(numberofTextFieldInList)).setEditable(true);
and now it should be editable. If you want the TextFields to look like Strings when not being editable, I would modify the aesthetic properties.
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.
Have a problem with GUI.
How to add text in jTextFiled that will disappear when I click on this field?
Have no idea how to realize this. jTextField has only one field for this property.
Thank you!
You can use the Text Prompt class which adds this functionality to any text field.
You have a property to control when the prompt is displayed.
The class basically adds a child (JLabel) to the text field which is painted when the text field doesn't have any text.
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.
I recently had a problem where I needed to have a field that can wrap the text and increase the height of the row as the text is wrapped, similar to Microsoft Excel. I managed to get it working correctly, the only problem is that the table contains multiple JComboBoxes. When the row's height increases from the field that wraps the text, the size of the JComboBox window and ArrowButton also increase. I am using a DefaultCellEditor for the JComboBox fields, and created my own Editor/Renderer to be used with the JTextArea field. Once the JComboBox's value is selected, the value gets displayed correctly in the field, the only problem is while I am selecting the value, the JComboBox window and ArrowButton could be HUGE depending on the size of the row. Is there any way to increase the height of the row, but have the JComboBox field height remain the same instead of growing to fill the column that it's in? I am thinking I might need to make a Custom Cell Editor for the JComboBox fields as well instead of using the default. Thanks in advance!
I am thinking I might need to make a
Custom Cell Editor for the JComboBox
fields as well instead of using the
default
That would probably be the solution since the size of the editor is determined by the size of the cell.
I would try using a JPanel with a BorderLayout as the editor component. Then you add your editor to the North of the panel.
It would be the easiest editor to create since all the mouse events and key events are passed to the editor I believe, which means the panel will get the events, not the combo box. So I guess you would need to forward these events to the combo box.
First, is the JComboBox in a BorderLayout and set to BorderLayout.CENTER?
If so, I'd change it to a different layout such as AbsoluteLayout so it does not stretch to fill the cell.
Also, I will also refer you to this post Putting JComboBox into JTable