Custom Swing component: questions on approach - java

I'm trying to build a new java swing component, I realise that I might be able to find one that does what I need on the web, but this is partly an exercise for me to learn ow to do this.
I want to build a swing component that represents a Gantt chart. it would be good (though not essential for people to be able to interact with it (e.g slide the the tasks around to adjust timings)
it feels like the best approach for this is to subclass JComponent, and override PaintComponent() to 'draw a picture' of what the chart should look like, as opposed to doing something like trying to jam everything into a custom JTable.
I've read a couple of books on the subject, and also looked at a few examples (most notably things like JXGraph) - but I'm curious about a few things
When do I have to switch to using UI delegates, and when can I stick to just fiddling around in paintcomponent() to render what I want?
if I want other swing components as sub-elements of my component (e.g I wanted a text box on my gantt chart)
can I no longer use paintComponent()?
can I arbitrarily position them within my Gantt chart, or do I have to use a normal swing layout manager
many thanks in advance.
-Ace

I think that the article i wrote a few years ago for java.net is still correct today. Doing everything in one monolithic class gets you going faster in the beginning, but becomes a mess quite fast. I highly recommend doing the separation between the model (in your main class) and the view (UI delegate). The view is responsible for:
interaction with the user - mouse, keyboard etc.
painting
creating "worker" subcomponents as necessary
In the medium and long run this is the approach that has been validated over and over again in the Flamingo component suite, which you can use as an extra reference point (in addition to how core Swing components are implemented).

Using UI delegates is a good idea if you think that your component should look different for different Look And Feels. Also it is generally a good idea from design point of view to separate you presentation from your component
Even when overrding paintComponent you can still put any sub components on it.
Using null layout you arbitrarey position your components. Alternatively you can use layouts too.
Here is a very good starting point for you.

Related

Writing an AWT widget

I fear I'm just making a ten-times duplicate question, but I've been trying to Google this for quite some time without finding anything, neither in general nor even here on SO, so I'm starting to think it's worth a shot.
I'm wondering, is there a good guide on what is necessary to implement an AWT widget in Java? What needs to be implemented in order to respond to such things as redrawing, resizing, placement, focus behavior and all other such things as widgets may or should do, in a proper manner? All the articles I manage to find describe the process from the perspective of a user of widgets, never from the perspective of the one implementing them.
Also, what would be the primary differences between implementing an AWT widget and implementing a Swing widget? Is there even a difference from the implementer's point of view?
Probably the best way to learn how to write an AWT widget would be to look at the source code for an AWT widget. Here's the source code for the java.awt.Frame class.
I haven't worked with AWT much, but Swing is much better from the GUI developer's point of view. The only difference that I know about from the implementer's point of view is Swing's usage of Listeners.
Again, you can look at the source code to see what's different. Here's the source code for the javax.swing.JFrame class
Not too much difference aside from the fact that AWT widgets are simpler.
You extend one of the 4 classes: Component, Container, Canvas, Panel. The first two are heavyweight, the latter two are lightweight (don't have native window system peers). Override paint(Graphics) (or update()) and getPreferredSize() to make it visible, then look into javadocs for overriding event handling methods.
Sure you know this link, http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html

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.

Does anyone have a example of slot machine in Java using swing?

I want to get some examples about the GUI of an slot machine written in java, I know that it's kind of specific thing, but i want to know if it's possible to do some animations with java swing, or something like that, greetings
It isn't a slot machine simulation, but you may find related features in this example.
Addendum: The example cited uses Unicode glyphs for variety, but another trick is to implement the Icon interface, which works nicely with some components. This example decorates a JButton for use in a game, while this example extends a child of JLabel to render a table.
This won't answer your question exactly (because I don't have a direct example), but I hope it at least tells you that it's certainly possible.
I've done some games using Java and Swing and such. None of them have been too heavy on the animations. Most of the animations I did happened through the Java 2D Graphics libraries, while more UI stuff happened through Swing.
For example, you can take all of the functionality of a JButton, but override the paintComponent(Graphics g) method to make it look however you want. This was really useful in past projects for me.
The most graphics-intensive Swing-related game I ever built was a "Who Wants to Be a Millionaire?" game. I used animations and graphics and sounds and everything, so I can assure you that it is possible, but it certainly isn't as easy as some other libraries might be.
If you decide to pursue this, I've found that Swing Hacks has some great tips on doing weird things with Swing. Although it isn't focused on gaming, I've found enough interesting things that you can do (Drag and Drop, a bunch of weird things) to make it a worthwhile purchase.

How to make an Advanced user interface in Android?

i wonder how you can make an advanced Android User interface where you can add for example a drag drop and more graphics options? is that by using OpenGl ?!
this is example of UI in iPhone Apps.
example 1
example 2
Thanks
Your examples just seem to be composed of a lot of nice images. Your first example looks pretty static and could probably be made from buttons with custom images and setting lots of backgrounds on your layout items. The second looks like you would need to make a custom Gallery and do a little more manipulation and composition of images so it might be worth your time to go a little lower level for performance.
Basically, you're looking at using a lot of images. You can make them work with existing widgets and components and get the functionality more easily, or you can use OpenGL ect. To get some more flexibility and performance at the cost of having to code all the functionality in yourself.
If you're looking at drag and drop this post points to the source for a ListView with some rudimentary drag and drop functionality.

Categories