Can i disable temporary the action listener on JComboxes?

Hi all,

i would be glad if somebody could help me out here.

To save the date in a database, i use three

JComboBoxes:

jComboBoxYear

jComboBoxMonth

jComboBoxDay.

These 3 fields are filled with the days, months and years at the initialization of a dialogbox by the method:

public void fillDateBoxes(JComboBox jComboBoxDay,JComboBox jComboBoxMonth,JComboBox jComboBoxYear)

To verify if the date is correct, i wrote a method

public boolean isDate(JComboBox jComboBoxDay,JComboBox jComboBoxMonth,JComboBox jComboBoxYear) that returns true if the date is correct. If the day is incorrect, a message box is displayed.

If now the user modifies a field, i call the method isDate(...) triggered by the Action Listener event: action performed.

My problem is the following:

unfortunately, the method isDate is also called when the the JComboBoxes are filled. I instanciated the action listener after I filled the boxes, but that has the same effect.

The action events mouse released, key released... on the boxes have no effect. Only item state and action performed trigger an action.

Is it possible to desactivate the listener at the beginning of my program and to enable it after the initialization of all the fields?

Or is there another solution?

Marc

[1373 byte] By [mkreins] at [2007-9-26 1:38:45]
# 1

Since you wrote the listener you should have no trouble setting a flag that indicates that initialisation is taking place. This would instruct your actionListener to perform no action.

Alternatively you'll need to initialise your combo boxes before you add your action listener(s) to them.

KPSeal at 2007-6-29 2:27:05 > top of Java-index,Archived Forums,Swing...
# 2

A better idea (in my opinion) would be to take your combo boxes and put them in a container (such as an extension of JPanel) and have the container manage the actions.

In other words, the JPanel listens to the combo boxes, your program listens to the JPanel. You can build a simple ChangeListener or ActionListener mechanism for detecting when the underlying data changes. This also allows you to return, say a java.util.Date object from the container and have the whole process of evaluating the values encapsulated.

Mitch Goldstein

Author, Hardcore JFC (Cambridge Univ Press)

mdgoldstein@hotmail.com

mitchg at 2007-6-29 2:27:05 > top of Java-index,Archived Forums,Swing...
# 3

For the moment I have so far written separated classes to manage my program.

DBAccess which does ALL database access,

DateHandler for managing nearly all possible Date operations (does also return Date objects, used by methods as dateToMySQLDate or dateAccessDateLU and so on). For all major operations of my program i use vectors, managed by a parent class vectorHandler. (Stores database ids, data, advantage: can be used in nearly all Swing constructors...). In the beginning, i thought this would be a good idea. Now my little program has become bigger and bigger and the separation of all tasks (classes) gives me sometimes a little headache :).

So far I never worked with collections. I already bought a book which covers a small part of the Collections. But i could not figured out for what purposes it would serve in real applications. Could you give me

an example how you implement a such container, lets say with a jPanel? I like the idea of putting all my visualization objects in a container (perhaps even my data). In fact, this could be far more effective to control the access to these containers and to determine which elements can be changed at which time. For the moment i use int values (static final int modifxy = 1 etc). A class RunLevel is used to control the access to the panels. This class doesn't control this by itself. It is only used as, lets say a storage of global variables (even if declared as private variables) with public access methods. Although it is quite effective to do the comparaison: if (application.runlevel.isState(application.runlevel.modifxy) {} else {}), i must write this

into the code of my panels and dialogs at numerous places. I consider this for the moment as the the big disadvantage of my program, because futher changes of my program are very timeconsuming.

A better solution would definitely that the panel itselft controls its access by the means you suggested. I still considers myself as new in JAVA,

even if i become an addict of it. One reason

for that is the large range of JAVA application. Yep, you dont have to learn a new syntax when you write

Wap applications, servlets for the net or a huge inhouse

database application.

Ok, i will stop here with my addiction, this is not the purpose of this forum :).

I like the idea you proposed. But i don't know how to implement this in a simple manner. Could u give me short example of a such

implementation?

Marc Kreins

Software engineer

Luxembourg

mkreins at 2007-6-29 2:27:05 > top of Java-index,Archived Forums,Swing...
# 4

In JComboBox:

protected void selectedItemChanged()

If you extend JComboBox with your own class, and override this method with a flag based approach:

protected void selectedItemChanged()

{

if (flag == true)

super.selectedItemChanged();

else return;

}

then if you add a method of your own such as:

public addItemNoEvent(Object o)

{

try{

flag = false;

super.addItem(o);

}

finally {flag = true; }

}

you'll be able to use that method no add items with triggering events.

lynnja at 2007-6-29 2:27:05 > top of Java-index,Archived Forums,Swing...