Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "More Swing (Chapter 14)‏ CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University."— Presentation transcript:

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

2 Announcements There will be no classes or labs next week There 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

3 Menus Allows the user to navigate the GUI easily 3 key words to remember: Menu Bar, Menu, Menu Item One 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.

4 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.

5 Menus //Create the menu bar. menuBar = new JMenuBar(); //Build the first menu. menu = new JMenu("A Menu"); menuBar.add(menu); //a submenu menu.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 herehere

6 Setting the Action Command for 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 using Menu_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.)‏

7 Placing an Icon and a String on a Label (or Button)‏ Example JButton 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.

8 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

9 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);

10 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)‏

11 JScrollPane Policies VERTICAL_SCROLLBAR_AS_NEEDED HORIZONTAL_SCROLLBAR_AS_NEEDED The default. The scroll bar appears when the viewport is smaller than the client and disappears when the viewport is larger than the client. VERTICAL_SCROLLBAR_ALWAYS HORIZONTAL_SCROLLBAR_ALWAYS VERTICAL_SCROLLBAR_NEVER HORIZONTAL_SCROLLBAR_NEVER Always display the scroll bar. The knob disappears if the viewport is large enough to show the whole client. Never display the scroll bar. Use this option if you don't want the user to directly control what part of the client is shown, or if you want them to use only non-scroll-bar techniques (such as dragging).

12 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.

13 CardLayout public 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 herehere

14 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, this method 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.

15 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.

16 Methods in the WindowListener Interface

17 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)‏

18 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 add setDefaultCloseOperation (WindowConstants.DO_NOTHING_ON_CLOSING); to the constructor. You may want to use setDefaultCloseOperation (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.)‏

19 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.

20 More Details on Updating GUIs 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.

21 Quiz In what situation do you prefer using WindowListener over WindowAdapter?


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

Similar presentations


Ads by Google