Deactivate components until an element is selected from JComboBox - java

I am working on Java swing application using data base with MySQL
I need to know if I can deactivate components until select an element from JComboBox? I must know the choice of the 1st jcombobox to fill the 2nd JComboBox; the 1st choice is a foreign key on the 2nd, like that :
ResultSet res = st.executeQuery("SELECT NomF FROM famille_de_type");
while (res.next()) {
comboBox_Fam_innewT.addItem(res.getString(1));
}
this is my example :

Of course, you can. When you start work call setEnabled(false) to second comboBox. And add to 1st combobox ItemListener. It will be listen item selection.
firstComboBox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange()==ItemEvent.SELECTED)
{
Object selectedItem = e.getItem(); // new item selected
// TODO select values for 2nd combobox
// TODO fill 2nd combobox
secondComboBox.setEnabled(true);
}
}
});

So in the ActionListenr of the JComboBox, simply call the setEnabled methods, passing false to disable them, or true to enable them

I need to know if i can deactivate components until select an element from jcombobox ?
YES. Why not Component.SetEnabled(false)?
Also you might want to look at ItemListener interface to achieve your goal. Here is more about Handling Events on a Combo Box.

Related

JComboBox remembering other selections

I have a JTable and in one column I have a JComboBox for each of the rows. I am dynamically adding rows when I press a button. The selection made in the combobox will determine what calculation is carried out for that particular row. For arguments sake lets say that the options for the combobox are: option 1, option 2, option 3 and option 4.
The issue I am having is as follows:
Say I have added 2 rows and select any option from the combobox for row 1, when I go to make a selection in the combobox for row 2 the same selection is ticked as was made for row 1. There seems to be some kind of memory. How can I disable this, so that the default selection is always -1 (i.e. non of the options selected)? I would like to have complete control over this.
Here is an example snippet of code just considering option 1:
String labels[] = {"Option 1", "Option 2", "Option 3", "Option4"};
JComboBox comboBox = new JComboBox(labels);
comboBox.setSelectedIndex(-1);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
int state = itemEvent.getStateChange();
ItemSelectable is = itemEvent.getItemSelectable();
if (selectedString(is) == "Option 1" & state == ItemEvent.SELECTED){
System.out.println("A");
}
}
};
comboBox.addItemListener(itemListener);
Thanks very much for your time and help :)
First of all don't use "==" when comparing strings. Instead you should be using the equals(...) method:
if (someString.equals(anotherString))
// do something
However, that is not the cause of the problem.
You are using the JComboBox incorrectly for a JTable. You should NOT be using a ItemListener (or any listener).
The combo box is just used as an editor for the table. That means when you select a value from the combo box, the TableModel of the table is updated. So if you have custom logic based on the selected value you need to override the setValueAt(...) method of your TableModel.
#Override
public void setValueAt(Object value, int row, int column)
{
super.setValueAt(value, row, column);
// add your custom logic here
}
How can I disable this, so that the default selection is always -1
The value displayed in the combo box is taken from the TableModel. So if you set the default value to be null the combo box will not have a selection when you start editing.
Read the section from the Swing tutorial on How to Use Tables for more information and working examples. Keep the tutorial link handy for future reference on Swing basics.

getSelectedRow on a combobox cell editor

I need a listener on a CombobBox which is a cellEditor on a JTable.
This listener must give me the new selected value and the row id.
Problem with my below solution is that the listner is linked to all rows, so when I change one ComboBox value in one row, then move to another row (with a different combo value) an event is raised, but the selected row has not yet changed. How can I get rid of this case ?
Thanks
column = jTableCheck.getColumnModel().getColumn(9);
JComboBox comboBox = new JComboBox(comboGenre);
comboBox.addItemListener(new ItemListener(){
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
int row = jTableCheck.getSelectedRow();
Popup.info(e.getItem() + " SELECTED, row="+row);
}
}
});
column.setCellEditor(new DefaultCellEditor(comboBox));
Don't use an ItemListener on the combo box.
Instead you should be using a TableModelListener. An event will be fired whenever the data in the TableModel is changed. So you add the TableModelListener to the TableModel of your JTable.
The TableModelEvent will give your row/column of the cell that changed. You can get the changed value from the TableModel.
Or maybe you would want to use a Table Cell Listener which is similar to the TableModelListener except the code is only invoked when the value is actually changed and you use an Action to do the processing.
In fact, I already used a TableCellListener on another table, but forgot about that!
I found out a usefull class here: http://tips4java.wordpress.com/2009/06/07/table-cell-listener/

ListSelectionEvent, firing an event when clicking the currently selected item in JList

Let 'x' be an item in the JList. When I click it for the first time, the event fires, when I click it again, the event does not fire. I have to click some other item and then come back to 'x'.
How can I fire the event repeatedly from 'x' without having to deal with other items.
This is my code:
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {} else {
String clicked = (String)list.getSelectedValue();
//method to fire is here
}
}
updateDisplays();
}
The ListSelectionListener reflects changes to the lists selection, you could use a MouseListener instead...
For example...
MouseListener ml = new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent evt) {
if (SwingUtilities.isLeftMouseButton(evt) && evt.getClickCount() == 1) {
if (list.getSelectedIndex() != -1) {
int index = list.locationToIndex(evt.getPoint());
System.out.println("You clicked item # " + index);
}
}
}
}
list.addMouseListener(ml);
You can add a MouseListener and watch for clicks. Note that a click that changes the selection will fire both the MouseListener and your ListSelectionListener.
Another option is to immediately clear the selection from your ListSelectionListener; that way the next click will reselect and retrigger, although you will lose the ability to navigate through items with the keyboard.
It seems like sort of an unusual UX decision, though, to assign significance to a click on an already selected item in a list.
Adding based on your question comments: If you go the MouseListener route, I recommend looking for double-clicks instead of single-clicks if the click is going to execute an action (especially if the action changes data and is not undoable). Also note that your ListSelectionListener will execute actions as you navigate through the list with the keyboard, which may not be what you intend.
If your commands in your history list are typed, you could also consider using a drop-down combo box for both command entry and the history list, where a selection from history fills in the command text but does not execute. You'd also have an opportunity to add auto-complete from command history.

how can I detect which combobox state changed?

I have two JComboBox in a form and I added an ItemListener to them and I should overwrite itemStateChanged(), now I wanna say if the first JComboBox items selected do something and else if the second JComboBox items selected do another thing, but I don't know how? Maybe code can help you.
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED)
picture.setIcon(pics[box.getSelectedIndex()]);
}
In the second line of code I don't know how to recognize which JComboBox state has changed.
You can use ItemEvent#getSource()
Example:
public void itemStateChanged(ItemEvent e) {
if(e.getSource() instanceof JComboBox){
JComboBox combo = (JComboBox) e.getSource();
//rest of code
}
Now for distinct combo1 from combo2 , you have 2 options , you can set names to that components like this.
combo1.setName("combo1");
combo2.setName("combo2");
And in the itemListener
if(e.getSource() instanceof JComboBox){
JComboBox combo = (JComboBox) e.getSource();
if("combo1".equals(combo.getName())){
// your code
}
.
.// rest of code
}
Or if you know that they are the same instance, then you can always use ==.
if(combo1 == e.getSource() ){
// your code
}else if (combo2 == e.getSource()){
//code for combo 2
}
There are two ways to do that, the first is to check the source on the event object and see which combo box it matches to.
The alternative is to add a different listener into each combo box, then you know that any calls going into one listener are from the corresponding control. This is a good use for an anonymous inner class.

Pass information from popup to main window on Oracle ADF

I have a popup that show a table with data, I am able to select a row, and by pressing a OK button I can retrieve the idNo of the selected row in the table.
What I want to do is to pass this idNo to the window that is calling the popup and update a outputText that is on this window.
Can some one help me?
Code for the button:
newBean Class for the button:
public String b1_action() {
// Add event code here...
System.out.println("Select One Button has been Clicked");
// Get bindings associated with the current scope, then access the one that we have assigned to our table - e.g. OpenSupportItemsIterator
DCBindingContainer bindings =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding dcItteratorBindings =
bindings.findIteratorBinding("NameView1_1Iterator");
// Get an object representing the table and what may be selected within it
ViewObject voTableData = dcItteratorBindings.getViewObject();
// Get selected row
Row rowSelected = voTableData.getCurrentRow();
// Display attriebute of row in console output - would generally be bound to a UI component like a Label and or used to call another proces
System.out.println(rowSelected.getAttribute("IdNo"));
setOutputText("" + rowSelected.getAttribute("IdNo") + "");
closePopup("p1");
return null;
}
I want that my function: setOutputText() which is not implemented yet to be able to update my outputText on the main Window.
Thanks
Best Regards
Put the "IdNo" in view or page flow scope depending on how you want to keep the value.
//view scope
AdfFacesContext.getCurrentInstance().getViewScope().put("IdNo", value);
//or page flow scope
AdfFacesContext.getCurrentInstance().getPageFlowScope.put("IdNo", value);
In the window bean, write a listener for the popup dialog:
public void dialogCloseListener(DialogEvent dialogEvent) {
if (dialogEvent.getOutcome().equals(DialogEvent.Outcome.ok)) {
String idNo = AdfFacesContext.getCurrentInstance().getViewScope().get("IdNo");
//now you have the idNo, do whatever you want
}
}
You can also use the returnListener inside the button or link that is invoking the popup like in this article

Categories

Resources