Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java the UML Way http://www.tisip.no/JavaTheUmlWay/ Texts, Choices and Windows Focus listeners Text components Giving the user the coice between alternatives Choices using check boxes Choices using radio buttons Choices using lists Java Windows The class tree with the windows classes Creating a window The differences between applets and applications Other ways programming listeners versjon 2002-04-17 page 2 page 3-6 page 7-9 page 10 page 11 page 12-13 page 14 page 15 page 16-18 page 19-20 page 21-22 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14 Focus Listeners • • • A focus event occurs when a component gains or loses focus. Useful if, for example, we want to control input immediately after the user leaves an input field. If input is not ok, the focus may be set back to the field again (requestFocus()). A class describing focus listeners must implement the java.event.FocusListener interface: – public void focusGained() – public void focusLost() • Often one of the methods is empty. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 2 Text Components • Object – – – Component • JComponent – – – JTextComponent JTextField JPasswordField • public String getText() public void setText(String text) public void setEditable(boolean open) The javax.swing.JTextField class – – – – – Container JTextArea The javax.swing.text.JTextComponent class public JTextField() public JTextField(String text) public JTextField(int noOfColumns) public JTextField(String text, int noOfColumns) public void setHorizontalAlignment( int horizontalAdjusting) public int getHorizontalAlignment() public void setFont(Font newFont) public void addActionListener( ActionListener aListener) The javax.swing.JPasswordField class – – – – public JPasswordField() public JPasswordField(int noOfColumns) public char[] getText() public String getText() (deprecated! ) Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 3 An Example First: Username, password and some text are entered: Pushing the print button gives the following text at the console: Here is the text: Ok, now I am here, and allowed to enter text. You are allowed to enter text here, if you input the correct password. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 4 Message Exchange username Field password Field textArea thePassword Listener printButton enter ”test” shift focus enter ”test” shift focus focusLost() okPassword() setEnabled(true) setEditable(true) requestFocus() edit the text Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 5 Show program listing 14.1, pp. 418-420 Solve the problem, page 423. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 6 Giving the User a Choice Between Alternatives check boxes list radio buttons Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 7 GUI Componenter for Choices GUI component Class Listener will implement the interface Check box javax.swing.JCheckBox java.awt.ActionListener Radio button javax.swing.JRadioButton, usually placed in a javax.swing.ButtonGroup java.awt.ActionListener List javax.swing.JList, usually placed in a javax.swing.JScrollPane javax.swing.event. ListSelectionListener Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 8 GUI Components for Choices Object ButtonGroup Component Container JComponent JList AbstractButton JToggleButton JCheckBox JRadioButton Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 9 Choices Using Check Boxes Show program listing 14.2 pp. 426-427. The user first clicks on the dinner check box, then on the lunch check box. An ActionEvent is generated for every click. The console: Will have dinner. Output after click on the dinner box Will have dinner. Will have lunch. Output after click on the lunch box Solve the problems, page 428. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 10 Choices Using Radio Buttons Show program listing 14.3 page 429-430. Solve all problems, page 431. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 11 Choices Using Lists List with fixed contents. Show program listing 14.4 page 432-433. The list can be set up such that the user can select only one item, an interval of items, or several intervals of items. Solve problem 1, page 439. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 12 Choices Using Lists Clicking on the line with “Evy Wilde” gives: List with dynamic contents. • • • • The changes have to be done in the list’s ”model”. ”The model” represents the data that will be displayed. ”The model” is described in the DefaultListModel class. The contents of the list displayed are described in the JList class. Show program listing 14.5, p. 436-437. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Solve problem 2, page 439. Chapter 14, page 13 Java Windows Primary Window (the JFrame class) with buttons for minimizing, maximizing, and closing, and with a title bar. Secondary Window or Dialog (the JDialog class), will be closed if the parent window is closed Internal Windows (the JInternalFrame class) with buttons for minimizing, maximizing, and closing. An internal window is clipped when displayed if its extension is outside the primary window’s boundaries. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 14 The Class Tree for the Windows Classes Object Component Container JComponent JInternalFrame Window JWindow Panel Frame Dialog Applet JFrame JDialog JApplet Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 15 Creating a Windows import javax.swing.JFrame; class TestJFrame { public static void main(String[] args) { JFrame myFirstWindow = new JFrame("My First Window"); myFirstWindow.setVisible(true); myFirstWindow.setSize(300, 200); // the size in pixels } } // this program does not work correctly • • • The window is closed, but the Java interpreter keeps running… The Java interpreter may be stopped by using System.exit(0);, but where should this statement be inserted? We have to create an object, listening to the window closing event. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 16 The WindowListener Interface and the WindowAdapter Class • The java.awt.event.WindowListener interface: void windowActivated(WindowEvent event); void windowClosed(WindowEvent event); void windowClosing(WindowEvent event); void windowDeactivated(WindowEvent event); void windowDeiconified(WindowEvent event); void windowIconified(WindowEvent event); void windowOpened(WindowEvent event); • Often all these methods, except for one, have empty bodies, therefore there exists an adapter class (this is true for all awt listener interfaces with more than one method): package java.awt.event; public abstract class WindowAdapter implements WindowListener { public void windowOpened(WindowEvent event) {} public void windowClosing(WindowEvent event) {} // ... and so on, only empty bodies } Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 17 An Example Using the Adapter Class import java.awt.*; import javax.swing.*; import java.awt.event.*; class JFrame2 extends JFrame { public JFrame2() { Container guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); guiContainer.add(new JLabel("This window handles closing. Close the window!")); addWindowListener(new MyWindowListener()); } private class MyWindowListener extends WindowAdapter { public void windowClosing(WindowEvent event) { System.out.println("Here we may write to data files, etc."); System.exit(0); // Remember this! } } } class TestJFrame2 { public static void main(String[] args) { Solve problems JFrame mySecondWindow = new JFrame2(); mySecondWindow.setTitle("My Second Window"); mySecondWindow.setSize(400, 100); mySecondWindow.setVisible(true); } } Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ 2 and 3, page 446. Chapter 14, page 18 Differences Between Applets and Applications • The applet – – – – • The applet is a subclass of JApplet. The applet class must have public access. The user interface is constructed in the init() method. The browser instantiates the applet object and sends the init() message to it. The application – – – – The application is a subclass of JFrame. Most of our applications have package access. The user interface is constructed in the constructor. The instantiation of application objects is coded in our programs, for example in the main() method. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 19 class ColorButtonApplication extends JFrame { private Container guiContainer; public ColorButtonApplication(String title) { super(title); guiContainer = getContentPane(); guiContainer.setLayout(new FlowLayout()); JButton myButton = new JButton("Push here!!"); guiContainer.add(myButton); public class ColorButtonApplet2 extends JApplet { ButtonListener theButtonListener = private Container guiContainer; new ButtonListener(); myButton.addActionListener(theButtonListener); public void init() { } guiContainer = getContentPane(); private class ButtonListener guiContainer.setLayout(new FlowLayout()); implements ActionListener { JButton myButton = new JButton("Push here!!"); public void actionPerformed(ActionEvent event) { guiContainer.add(myButton); guiContainer.setBackground(Color.red); ButtonListener theButtonListener = new ButtonListener(); } myButton.addActionListener(theButtonListener); } } } class TestColorButtonApplication { private class ButtonListener implements ActionListener { public static void main(String[] args) { public void actionPerformed(ActionEvent event) { ColorButtonApplication myWindow guiContainer.setBackground(Color.red); = new ColorButtonApplication("My Window"); } myWindow.pack(); } myWindow.setVisible(true); } myWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } } Differences Between Applets and Applications, an Example Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 20 Other Ways to Program Listeners: 1. The Window as Listener public class ColorButtonAppletThis extends JApplet implements ActionListener { private Container guiContainer; The class describing the public void init() { applet, is the same as the class guiContainer = getContentPane(); implementing the guiContainer.setLayout(new FlowLayout()); ActionListener interface JButton myButton = new JButton("Push here!!"); guiContainer.add(myButton); this is registered as a myButton.addActionListener(this); listener object } public void actionPerformed(ActionEvent event) { guiContainer.setBackground(Color.red); } The inner class } ButtonListener is gone. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 21 Other Ways to Program Listeners: 2. An Anonymous Class new tells us that an object is to be instantiated Here are the parentheses enclosing the argument to the addActionListener method. The class which the object will be an instance of is unnamed. It is anonymous. The class will be an implementation of a listener interface, or it will be a subclass of an adapter class. The name of the interface or the adapter class has to be stated here. myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { guiBeholder.setBackground(Color.red); } } ); This semicolon terminates the myButton.add.... statement. These curly braces enclose the anonymous class. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 14, page 22