Transcript Chapter 14

Chapter 14- More Swing, Better
looking applications.
Overview








Menus
Icons (again)
Scroll Panes.
Borders
Look and Feel
More Layouts
Inner Classes
Review
Menus
The use of menus



Menus are basically collections of
buttons.
Each button in the collection is in the
same category (like all the entries in the
File menu are usually related to files).
Java deals with menu selections the
same way as it deals with buttons.
Menu hierarchy





Create JMenuItems and register an action
listener for them (just like JButtons).
Add JMenuItems to JMenus.
Add JMenus to other JMenus(to make menus
within menus).
Add your finished JMenu to a JMenuBar.
Add JMenuBar to the JFrame.
JMenuItem



Create it with some text to set the label
of the menu item. Also sets the action
command for the menu item.
Register an action listener (usually
this)
Add the item to the JMenu with the
add(JMenuItem) method of JMenu.
JMenuItem example
JMenu menu = new JMenu(“Mugglets”);
JMenuItem mi = new JMenuItem(“Blar!”);
mi.addActionListener(this);
menu.add(mi);
JMenu




Construct a JMenu also with a string to set
the label of the button you push to bring up
the contents of the menu (“File”).
JMenu inherits from JMenuItem, so you can
actually add JMenus to other JMenus (nested
menus).
Use the add method to add items to the
JMenu.
When you are done, use the add method of
JMenuBar to add the JMenu to the menu bar.
JMenu example
JMenu menu = new JMenu(“Mugglets”);
JMenu overMenu= new JMenu(“Snubbles”);
JMenuItem mi = new JMenuItem(“Blar!”);
mi.addActionListener(this);
menu.add(mi);
overMenu.add(menu);
JMenuBar theBar = new JMenuBar();
theBar.add(overMenu);
JMenuBar



No string in its constructor, as there is
no label to display for a menu bar.
Use add method to add JMenus to the
menu bar.
When finished with JMenuBar, use
setJMenuBar(JMenuBar) on the
JFrame, or add the JMenuBar to the
content pane of the JFrame.
JMenuBar example.
JMenu menu = new JMenu(“Mugglets”);
JMenu overMenu= new JMenu(“Snubbles”);
JMenuItem mi = new JMenuItem(“Blar!”);
mi.addActionListener(this);
menu.add(mi);
overMenu.add(menu);
JMenuBar theBar = new JMenuBar();
theBar.add(overMenu);
setJMenuBar(theBar);
//or
getContentPane().add(theBar);
Menu Review




What are the smallest items that we add
to menus?
Can we add menus to menus?
What do we add menus to?
How do we get the whole menu system
to display in a JFrame (2 ways)?
Icons
A picture is worth a thousand words.



Icons can make things more interesting
to look at as well as easier to
understand.
We can add icons to our Java GUIs.
We usually add them to labels and
buttons and menu items.
ImageIcon



We create icons by constructing
ImageIcon objects.
When you construct the object, you
pass it the name of an image file.
Most popular image formats can be
read by Java (JPG, GIF, etc.).
Adding Icons



We can make an ImageIcon the sole
displayed item in a button, label, or
menu item by passing an ImageIcon in
the constructor of the object.
We can also use setIcon(ImageIcon).
We can also have both images and text.
ImageIcon example.
ImageIcon blar = new ImageIcon(“someImg.gif”);
JButton howdy = new JButton(“Howdy”);
howdy.setIcon(blar);
// or
JButton howdy = new JButton(blar);
howdy.setText(“Howdy”);
// or
JButton howdy = new JButton(“Howdy”, blar);
Icon review



What class do we use if we want to
create icons?
What kinds of items can we add icons
to?
What method do we use to add an icon
to an already-existing object?
Scroll Panes
When the screen just isn’t big
enough…



If our information is bigger than a particular
JTextArea that we have, we can add scroll
bars so that we can view all of the
information.
This is done using a view port called
JScrollPane.
A view port is basically a window through
which you can see the information in the
background.
How to create a JScrollPane.


When we construct a JScrollPane, we
want to pass the component that we
want scrollbars on to it (usually some
text area).
Once we have done that, we set the
scroll bar policies and add it to a panel
or frame.
JScrollPane Example
JTextArea someText = new JTextArea(20,40);
JScrollPane viewWindow = newJScrollPane(someText);
viewWindow.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
viewWindow.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//or
viewWindow.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_NEVER);
getContentPane().add(viewWindow);
Scroll Bar Policies



NEVER- No scroll bar is ever displayed
in the indicated direction.
AS_NEEDED- Scroll bar only appears
when what is being viewed becomes big
enough to require scrolling.
ALWAYS- Scroll bar is always there.
Whether it is needed or not.
Scroll Bar Review


What class do we use to create
scrollable text areas?
How do we set the scroll bar policies?
To what do we set them too?
Borders
Borders

We can also set the borders of any
JComponent (Just about anything we
have been dealing with) to almost any
kind of border we might want (and quite
a few we don’t).
Borders



First, import javax.swing.border.*
Then use the setBorder(Border) method on
whatever component you want to put a
border on.
You can specify lots of kinds of borders:
EtchedBorders, EmptyBorders, BevelBorders,
LineBorders, MatteBorders,
SoftBevelBorders, or you can make your own.
Border Examples
JButton arg = new JButton(“Argh!”);
arg.setBorder(new EtchedBorder(Color.yellow,
Color.orange));
arg.setBorder(new LineBorder(Color.black, 5));
…
Borders Review



What library do we have to import to be
able to use borders?
What method of JComponent do we use
to change the border of an object?
What are some examples of Border
classes?
Look And Feel
Changing the look and feel of Java.





We can change the whole appearance of
Java using pluggable “look and feel” designs.
The standard default one is called Metal.
There is another called Motif that looks more
like a UNIX X-Windows system
There is yet another standard one called
Windows which looks a lot like Windows.
You could even make your own if you were in
to that kind of thing.
Changing the Look and Feel.



You usually do it in the constructor of
your JFrame.
You will need to catch a fair number of
exceptions when you try to change the
look and feel(or you can catch them all
as Exception).
You will need to use some special
statements seen in the example.
Look and Feel Example
try
{
UIManager.setLookAndFeel(SomeLookAndFeelClass);
//like “javax.swing.plaf.metal.MetalLookAndFeel”
//“com.sun.java.swing.plaf.motif.MotifLookAndFeel”
//”com.sun.java.swing.plaf.windows.WindowsLookAndFeel”
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e)
{
…
}
Look and Feel Review

Just remember that you can change the
look and feel of Java and that you
usually do it in the constructor of your
JFrame.
More Layouts
Layouts and more layouts


We mentioned before that there were
some more layouts.
Now we will look at some more layouts
in depth and see what they produce.
BoxLayout


A horizontal BoxLayout is like a FlowLayout,
while a vertical BoxLayout is somewhat like a
GridLayout with 1 column.
When we construct a BoxLayout, we pass
two things to it, the container you want to set
the layout for, and the orientation of the Box.
JPanel somePanel = new JPanel();
somePanel.setLayout(new BoxLayout(somePanel,
BoxLayout.X_AXIS)); //or Y_AXIS.
The power of BoxLayout



BoxLayout is not all that impressive until
you start adding struts and glue.
Struts are commands to Java on how
much space to put between
components (rigid, no change).
Glues are requests to Java on how
much space to put between
components (they can be squished).
Creating Struts (and glue).




Make them of type Component.
Use Box.createHorizontalStrut(int) or
Box.createVerticalStrut(int) or
Box.createHorizontalGlue() or
Box.createVerticalGlue().
Then just use the usual add method to put
the strut or glue into the layout.
Only uses struts and glue in BoxLayout or
Box(coming up next). They don’t work so well
with other layout managers.
Box- An easier way.



Notice that we used a few static methods to
create the struts and glue off of the Box class.
We can use this Box class to automatically
create a JPanel that has a BoxLayout.
Don’t have to use a constructor for this, but
you can.
Box aBox = Box.createHorizontalBox();
Box aBox = new Box(BoxLayout.X_AXIS);
Box


After you have created a Box, you can
then use it like you would any other
JPanel.
You can of course add struts and glue
to the Box.
CardLayout.



This layout is quite different than the other
layouts we have seen.
When we add things to a CardLayout panel,
we don’t get multiple items in the same panel,
we get multiple panels on top of one another.
We can call any of these panels up to the
front at any time, but only the front one shows
at any time.
CardLayout details



Do not use an anonymous object to set the
layout. You will need a CardLayout object
later to show different cards.
When we add things to the panel, we first
give it a label to identify by, and then the
container we want to add to a card.
We show different cards by calling several
methods off of the CardLayout object that we
saved and giving them the Container we want
to change.
CardLayout example
JPanel somePanel = new JPanel();
CardLayout aDeck = new CardLayout();
somePanel.setLayout(aDeck);
JPanel panel = new JPanel();
JLabel aLabel = new JLabel(“Howdy”);
panel.add(aLabel);
somePanel.add(“aFirstCard”, panel);
…
aDeck.show(somePanel, “aFirstCard”);
More CardLayout details


Initially, the first card you add is the one that
is displayed.
We can move amongst the cards with other
methods besides show:
-first(Container)
-next(Container)
-prev(Container)
-last(Container)
Layout Review



What is a quicker way of doing
BoxLayout in a panel?
What special pieces do we use in
BoxLayout to space components apart?
What is the difference between the
pieces?
How does CardLayout work?
Inner Classes
Inner Classes



We have already seen a little bit of inner
classes (for the usual way of closing
windows, etc.).
We can also define an inner class by having a
second class inside of the same file, this one
private.
Inner classes have access to all the methods
and instance variables of the outer classes.
Another way to close windows



Instead of creating an inner class that inherits
from the WindowAdapter, we could instead
implement the WindowListener interface.
We can implement as many interfaces as we
want.
If you implement the WindowListener, you
need to implement all 7 functions of
WindowListener.
WindowListener methods–
–
–
–
–
–
–
windowOpened(WindowEvent)
windowClosing(WindowEvent)
windowClosed(WindowEvent)
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
Closing Windows


Another way to close a single JFrame is to
call the dispose() method of the JFrame. This
is useful in a multiple window program, so it
doesn’t close all of Java.
You can reprogram the close button to act like
any button you like, you will want to reset the
close button action by using
setDefaultCloseOperation(WindowConstanst
s.DO_NOTHING_ON_CLOSE) on the frame.
Visibility and updating the GUI


If you ever want to make a particular
component disappear for a while, you can
setVisible(false) on the component.
Whenever you make a change to the GUI and
Java doesn’t automatically refresh the GUI for
you, you can force it with the validate(),
repaint(), or pack() methods. Currently you
probably want to use validate().
Inner Class Review






How are inner classes useful?
What is the new way to do inner classes that
we just learned?
If we didn’t want to control the window
through a WindowAdapter class, how could
we do it?
How can we close a single window?
How do we cause components to disappear?
How do we force a refresh of the GUI?
Review
Review



What are the smallest items that we add
to menus?
What do we add menus to?
How do we get the whole menu system
to display in a JFrame (2 ways)?
Review



What class do we use if we want to
create icons?
What method do we use to add an icon
to an already-existing object?
What class do we use to create
scrollable text areas?
Review



What library do we have to import to be
able to use borders?
What method of JComponent do we use
to change the border of an object?
What are some examples of Border
classes?
Review




How does BoxLayout work?
What is another way of getting a
BoxLayout panel?
What components do we need to use to
make BoxLayouts useful?
How does CardLayout work?
Review





How are inner classes useful?
What is the new way to do inner classes
that we just learned?
How can we close a single window?
How do we cause components to
disappear?
How do we force a refresh of the GUI?