More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue...

21
More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    2

Transcript of More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue...

Page 1: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

More Swing (Chapter 14)

CS 180 Recitation - April 18, 2008Department of Computer SciencePurdue University

Page 2: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Announcements

There will be no classes or labs next weekThere will be only an overview recitation, so

make sure that you attend it since it will be a very good practice for the final exam.

Also we will make recitation evaluation on that overview recitation.

Due date for project 8: 04/23/2008

Page 3: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Menus

Allows the user to navigate the GUI easily3 key words to remember: Menu Bar, Menu,

Menu ItemOne can place a menu bar almost anywhere. But

if setMenuBar method of JFrame is used it is placed at the top by default.

Menus can be added to a menu bar which can then have menu items or other menus.

Page 4: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Menus

JMenu is derived from JMenuItem (i.e. JMenu is also a JMenuItem)

So we can add a JMenu to another JMenu as if we are adding a JmenuItem

Since JButton is also derived from JAbstractButton, JMenuItem have many similar methods with JButton.

Page 5: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Menus

//Create the menu bar.menuBar = new JMenuBar();

//Build the first menu.menu = new JMenu("A Menu");menuBar.add(menu);

//a submenumenu.addSeparator();submenu = new JMenu("A submenu");

menuItem = new JMenuItem("An item in the submenu");

submenu.add(menuItem);

menuItem = new JMenuItem("Another item");submenu.add(menuItem);menu.add(submenu);

...

frame.setJMenuBar(theJMenuBar);

Example taken from here

Page 6: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Setting the Action Commandfor a Menu Item

If you do not wish to use the text for a JMenuItem as the default action command, you can set the action command usingMenu_Item_Object.setActionCommand(Action_Command_String);

This is useful if you are implementing actionPerformed in the same actionListener for all the components in your program but it is not recommended. (i.e. implement a separate actionListener just for the menu bar.)

Page 7: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Placing an Icon and a Stringon a Label (or Button)

ExampleJButton helloButton = new JButton(“Hello”);ImageIcon dukeWavingIcon = newImageIcon(“dukeWaving.gif”);helloButton.setIcon(dukeWavingIcon);

As you can see text “Hello” is by default centered vertically and to the right of the icon.

Page 8: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

JScrollPane

JScrollPane is used to limit the visible part of a component, text or an image in GUI

A JViewport instance is used to handle the visible portion of JScrollPane

Page 9: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Making a JPanel Scrollable

JPanel tmp = new JPanel();

//checkBoxes is an ArrayList that holds all

//the checkboxes that we will display

for (JCheckBox ch : checkBoxes){tmp.add(ch);

}

JScrollPane listScroller = new JScrollPane(tmp);

listScroller.setPreferredSize(new Dimension(250, 80));

mainPanel.add(listScroller);

Page 10: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

JScrollPane

If the frame is enlarged enough, scroll bars will disappear in the default behavior. You can change that behavior by using scroll pane's scroll bar policy.

Two of the constructors let you set the policy or you can use mutator methods of the class.JScrollPane(Component, int, int)JScrollPane(int, int)setHorizontalScrollBarPolicy (int)setVerticalScrollBarPolicy (int)

Page 11: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

JScrollPane Policies

VERTICAL_SCROLLBAR_AS_NEEDEDHORIZONTAL_SCROLLBAR_AS_NEEDED

The default. The scroll bar appears when theviewport is smaller than the client anddisappears when the viewport is larger thanthe client.

VERTICAL_SCROLLBAR_ALWAYSHORIZONTAL_SCROLLBAR_ALWAYS

VERTICAL_SCROLLBAR_NEVERHORIZONTAL_SCROLLBAR_NEVER

Always display the scroll bar. The knobdisappears if the viewport is large enough toshow the whole client.

Never display the scroll bar. Use this option ifyou don't want the user to directly controlwhat part of the client is shown, or if you wantthem to use only non-scroll-bar techniques(such as dragging).

Page 12: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

CardLayout

Card Layout is very useful if two or more of your components (usually JPanels) will use the same display place.

Component that shares the same display space is similar to playing cards in a deck. That is why this layout is called CardLayout.

Page 13: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

CardLayoutpublic void addComponentToPane(Container pane) { JPanel comboBoxPane = new Jpanel()

//BUTTONPANEL = "Card with JButtons" //TEXTPANEL = "Card with JTextField"

String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; JComboBox cb = new JComboBox(comboBoxItems); cb.setEditable(false); cb.addItemListener(this); comboBoxPane.add(cb); //Create the "cards". JPanel card1 = new JPanel(); card1.add(new JButton("Button 1")); card1.add(new JButton("Button 2")); card1.add(new JButton("Button 3")); JPanel card2 = new JPanel(); card2.add(new JTextField("TextField", 20)); //Create the panel that contains the "cards". cards = new JPanel(new CardLayout()); cards.add(card1, BUTTONPANEL); cards.add(card2, TEXTPANEL); pane.add(comboBoxPane, BorderLayout.PAGE_START); pane.add(cards, BorderLayout.CENTER);} public void itemStateChanged(ItemEvent evt) {

CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, (String)evt.getItem());}

Example taken from here

Page 14: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

CardLayout

Method Purpose

first (Container parent) Flips to the first card of the container.

next (Container parent) Flips to the next card of the container. If the currently visible card is the last one, thismethod flips to the first card in the layout.

previous (Container parent) Flips to the previous card of the container. If the currently visible card is the first one, this method flips to the last card in the layout.

last (Container parent) Flips to the last card of the container.

show (Container parent, String name) Flips to the component that was added to this layout with the specified name, using the addLayoutComponent method.

Page 15: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

WindowListener

The WindowListener interface makes the window itself the listener just as the ActionListener interface makes a window a button listener.

There are 7 methods in WindowListener interface. Remember that we need to implement all of them if we want to use WindowListener interface.

But you will be able to use the methods of your program inside the methods of WindowListener interface since they are in the same class. For instance you may want to close a PrintWriter instance when the user clicks the close button.

Page 16: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Methods in theWindowListener Interface

Page 17: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

WindowListener vs. WindowAdapter

WindowAdapter implements interface WindowListener by giving every method an empty body. So you won't have to implement all the 7 methods of WindowListener.

If you want to handle the window inside of the class that is drived from JFrame, you need to implement WindowListener. You cannot use WindowAdapter. (i.e. A class cannot extend two classes)

Page 18: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Window Closing

If you invoke setDefaultCloseOperation, by default the window disappears but the program does not end.

Simply reprogramming method windowClosing does not cancel the default action.

So, first addsetDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSING);

to the constructor. You may want to usesetDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);

But in this case you won't be able to take extra actions for closing (i.e. Popping up a confirmation window on window closing.)

Page 19: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Method validate()

• Every container class has a method validate for updating the container.

• Method validate causes the container to lay out its components again and redisplay them.

• Some changes, such as adding components or changing visibility require an invocation of method validate.

Page 20: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

More Details on UpdatingGUIs

• Most changes to a GUI windowing system are done automatically by the repaint manager.

• However, a change in the visibility of a component requires an invocation of method validate.

• Method pack causes the window to be resized to an approximation of the “preferred size.”

• Method repaint repaints the window. If your program uses the same display space for

different set of components each time, CardLayout should be used instead of using setVisible(), validate(), and repaint() each time.

Page 21: More Swing (Chapter 14) CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.

Quiz

In what situation do you prefer using WindowListener over WindowAdapter?