Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Graphical user interfaces and event handling in Java  RSS feed

 
Simeone Marini
Greenhorn
Posts: 3
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my Java application where I use Java SWT libraries, I have my GUI "is waiting" for a user event, which for example It can be generated by a click inside a box of the game of tris .

I implemented my own version of MouseAdapter in myMouseAdapter and I can to intercept the event and handle it:I must then implement the method mouseUp from which I call a method of a static object.

In the case of a little software program, I think it is a good solution, but I want to know if it is possible to manage an event of this type without using a reference to a static object.

What is the best way to handle this kind of events? There are patterns for this type of problem?

A piece of my Java code:
 
Stephan van Hulst
Saloon Keeper
Posts: 6969
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Simeone, welcome to CodeRanch!

Simeone Marini wrote:In the case of a little software program, I think it is a good solution

Not really. You should always avoid globally accessible data.

Event handlers should be very simple. Personally, I usually just run one statement in them, that delegates the action to an object that knows how to do the checking, and that can execute the action. In your case, the adapter could look like this:
Here, Controller is whatever class you use to do all the checking, and to make sure that actions are performed in the proper way. In your case this would be GuiThread. You should then eliminate all the static fields, and create an instance of GuiThread instead, that uses normal fields.

Another thing, it looks like you're doing a lot of messing with pixels. This can cause lots of problems for different users with different screen sizes and resolutions. You should probably build up your GUI out of components that are managed by a LayoutManager (if SWT has such a thing, I'm more familiar with Swing). The region where you want the actions to happen will then have its own coordinate space, and using event listeners will become more effective.
 
Simeone Marini
Greenhorn
Posts: 3
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks very much.

I wish the delegate object to perform action is not the object of the UI (in this case GuiTestSWT), but another object, in this case Partita.

But in this way, I create two instances of GuiTestSWT! And this does not seem right to me!?!?

 
Stephan van Hulst
Saloon Keeper
Posts: 6969
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why not just do the following?
 
Simeone Marini
Greenhorn
Posts: 3
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this way, the compiler(eclipse) report a error:

Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.checkWidget(Unknown Source)
at org.eclipse.swt.widgets.Composite.layout(Unknown Source)
at GuiTestSWT.run(GuiTestSWT.java:47) <------ in this text at line 38
at java.lang.Thread.run(Thread.java:744)

I'm sorry that I do not understand. The code of GuiTestSWT is generated by WindowBuilder(plug-in Eclipse)and why upset the structure of code?
 
Stephan van Hulst
Saloon Keeper
Posts: 6969
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, the problem apparently is that the shell has to be laid out and opened by the same thread as the one that created the shell. So I would move shell.layout() and shell.open() to the end of the init() method, and then instead of calling init() from the constructor, call gui.init() after you've created the gui instance.

The reason I've moved things around, is because in the constructor you should set up the class for use, but preferably not perform any actions. I don't know why the eclipse plugin generates the code this way, but to me it looks horrible.

Is there a particular reason you're using SWT, rather than Swing, for example?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!