Java Reference
In-Depth Information
manager is not controlling your component. Since JPanels are controlled by a FlowLayout ,
by default, and bordered containers by a BorderLayout , this will never be the case by default.
To specify sizes, you'll hence to resort to the setMinimumSize , setPreferredSize , and
setMaximumSize methods. Try these first before trying something else.
Sometimes, you'll want to add components while your program is running. You might
encounter cases where your component does not appear. Occassionally, you might note that
the added component suddenly pops into view when resizing its containers. In that case,
check out the revalidate and repaint methods of the component, which will force Java to
draw it after adding it.
You'll note that building GUIs is mostly a matter of creating components and adding them
to each other. For these simple examples, it was easy to just put the complete GUI initializa-
tion in a main method and leave it at that. However, be aware that it is an easy mistake when
building GUIs to suddenly forget everything about Object‐Oriented Programming and end
up with monster classes setting up a huge amount of GUI components. Thus, when build-
ing GUIs, always ask if you might be able to reuse certain parts, and keep in mind you can
extend GUI classes just like you could any class! If you need a bold red label in many places
of your program, why not construct an ErrorLabel that extends the JLabel class that
already contains the right calls to set up the font and size, instead of always directly setting
up a nice-looking JLabel every time you need it? If you need some kind of easy way to create
forms row‐by‐row, consider creating a class‐extending JPanel that uses a GridBagLayout
together with some hand‐rolled methods to make the GUI setup easier. In short, don't be
afraid to abstract GUI aspects. Another even more important issue arising from putting logic
and GUI‐related code into the main method (the main method, in the examples above) is the
fact that you might run into threading issues with Swing. The next section will explain in
detail what this entails and how you can avoid such issues.
Finally, when constructing GUIs, it is a nice idea to sketch out on paper first how your GUI
should look, and derive some component/container tree out of it. Maybe the JFrame should
contain two panels. One panel would contain a JSplitPane , with the left component a
JScrollPane and the right component another panel. The layout manager would stack a
number of different JPanels , each of which would contain certain buttons and labels. Sketch
your ideas on paper first, and don't be afraid to put things into a separate JPanel (or even
better, into a custom class‐extending JPanel ). It takes a little more work at first, but you'll
be thankful later.
understanding events
All the GUI applications you've been building so far do not have any true functionality associated with
them. Sure, you can add a JButton and even click it, but how do you make it actually do something?
To add behavior to your graphical interfaces, you first need an introduction to the concept of events.
introduction to events
An event can be defined as a happening of something, an occurrence, meaning that something
happened somewhere. In graphical user interfaces, events usually result from a user's action. For
 
Search WWH ::




Custom Search