How to create my custom SWT control selectable? - java

I can make control extending Composite or Canvas.
How to make it selectable? I.e. how to make it behave like Button? Button is disabled for extend. I see a lot of unportable code inside it.
So how to make Button-like control of myself?
Should I process mouse and keyboard events myself or there is some premade functionality to utilize?

You will have to handle the events yourself. I'll give you a couple of hints and references here:
First of all, make sure you read this: Creating Your Own Widgets using SWT
Have a look at SquareButton. It's a custom Button widget and should contain all the code you need.
Here is a very related SO question.
Hope this helps.

Canvas is for drawing from "inside". So, you have to create your own UI input. As here.

Related

Interactive UI with dynamic areas

Going to make a pretty statistic part for my app and saw a pretty good looking UI (see below). What I'm asking is what's the approach? I have a few ideas, for example drawing multiple ImageButtons (one for every text field) and paint the background the same color as the ImageButtons.
This if i want each of the texts to be clickable, in the example picture for example pressing 18c fires one action and pressing Cambridge another action.
How would you approach this?
What components to use?
Want to do as much as possible in xml, but I'll take suggestions in code too. Whatever is better for you.
EDIT:
Found a good example about how to solve the listView part:
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

How to make "pages" in a java program

I have a question.
I just started with Java and may have some small basic things. Now I wonder how a kind of pages (sections) in a program makes.
I do not mean some kind of tabbed panel, or if you click on a button that a text is visible.
I mean that for example all over the screen a separate part of the program looks. As the main menu of a game.
There is nothing else than the main menu visible at that time. If you for example a button from that menu click. The game is loading.
(I'm using the building of a standard game as an example)
If you for example the main menu click on another button (eg "Settings")
Then wort settings "page" is visible, and there is nothing else that the program is really doing.
I do not know how this type of navigation is called. But almost every program does have something.
How can I do this too? What should I do for example, as a new file, import the classes of a particular page, or something?
You seem to be searching for CardLayout. As shown here.
I think you should look for "state machines", which is a way for structuring your code, and implement your menu changing swing components (like JPanel, for example) in a JFrame. If I understand what you want, I think this can be an option.
There is no short answer, but based on your question, you need to read alot. I would suggest the swing tutorial It explains use of Panels, Frames, Layout managers and other containers.
You can also use the Matisse builder in netbeans (relevant plugin in eclipse)

Java: How to do GUI animations?

In all my time so far working with Java and its Swing GUI framework, I've never quite figured out (or even attempted to try) how to make the interface animate components.
Say I wanted the screen to slide left into the next screen or have a JLabel "fly" to a new location. Perhaps you want a menu to smoothly open in an animated fashion. How does this work?
Do you have to use SwingWorker? Even if that's the case... how can you control the painting of components if the layout manager is already doing that?
Have a look at the book Filthy Rich Clients, you will find some really good answers there.
I think that there no reason for use SwingWorker, SwingWorker is designated for running long Backgroung Task(s) on output would be on Event dispatch Thread,
For animations in Swing is there javax.swing.Timer, examples here
Take a look at Trident library. You can use it to interpolate various properties in your class.

Creating mixed components in swing + java

Is there any way to create a custom component in swing. By custom I mean say right now I am able to create a circle and do actions like dragging it etc.
But now I also want that along with the circle a text label with its number is also present. Can we combine them into a new type of component where say we can do actions on it collectively?
If yes please give me pointers on how to do so.
Yes, just extend JComponent and handle painting and interaction however you want. If there is a component that already does most of what you want you could extend that class and tweak it slightly.
I would suggest reading some of the tutorials about how JComponent works:
http://download.oracle.com/javase/tutorial/uiswing/components/jcomponent.html
Here is a similar question but I am sure you can find more specific ones that have been asked once you get deeper into it.
How to create a custom Swing Component
The Swing tutorial as a whole if very useful to understand how other components work etc.
http://download.oracle.com/javase/tutorial/uiswing/components/index.html
You can create your own Swing component extending JComponent. Several question here on Stack on the topic like this one.
Personally, I advise you to get this book: Java Swing as it contains an excellent guide on how to create Swing components.

Listening to all JInternalFrame events - Java

I'm trying to internationalise a Java applet and with that, support scripts which are written from right to left. I want to set up component orientations for all java components added to the view automatically.
My solution so far has to listen to all AWTEvent's using the windows mask:
c.getToolkit().addAWTEventListener(listener, AWTEvent.WINDOW_EVENT_MASK);
...and then setting the c/o on each window added, as well as adding component listeners to set c/o on any components added to the window at a later point.
My issue is that JInternalFrames are not handled by this solution, I want to be able to add another listener for these events, much like I have done for windows. Any ideas?
Or alternatively, are there any better approaches to handling script direction for all components in an applet?
Add a ContainerListener to the JDesktopPane. As a component is added to the desktop you can change its orientation.
Do you have a handle on all those JInternalFrames? If so, try the internal frame listener.
http://java.sun.com/javase/6/docs/api/javax/swing/event/InternalFrameListener.html
It notes that it's the analogue to the AWT WindowListener.
AWTEventListener on the current Toolkit will only give you events coming from the toolkit. Generally events generated by lightweight components will have been caused by mouse or key events.
Asking for all of something in a process is usually a very bad sign. A low-level piece of code is making policy for the whole program. A much better approach is to add listeners near to where you create the component, before it is "realised". This is repeated code, but then you probably already have repeated code. So factor out into a method. Then you have only one place to update, unless you have any cases where it doesn't apply which would have broken the global approach.

Categories