• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/33

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

33 Cards in this Set

  • Front
  • Back

What are some of the problems associated with using GUI's in Java

- In (Abstract Windowing Toolkit) AWT classes Java feeds information to the operating system's infrastructure to create GUI environments consistnt with the operating system. Problem is different operating systems offer different feature so Java can only choose the options consistant amoung all.


- Also AWT classes are difficult to extend bc they are feeding into the operating systems infrastructure and so are not flexible for augmentation


- Lastly, infrastructure classes can have bugs which requires testing for each infrastructure application used to ensure the programmer is getting what he/she wants

- What is the difference between Swing Classes and AWT classes?

- Swing feeds programmers information into the operating systems infrastructure and is used to make web-based applets


- Swing classes are a set of GUI classes that java have created and provides excellent flexibility and all the features we know and love (extending classes ect) bc its not hamstrung by having to directly communicate with the infrastructures GUI system

How to use JOptionPane Dialog boxes method

- must import javax.Swing


- JOptionPane.showMessageDialog(null*, "String that appears in the middle of the box for the user to read", "Tittle message that appears on the tittle bar", JOptionPane.MESSAGE_TYPE);




* A graphical component can be passed here which the message will be displayed in (need clarification on that)


- MESSAGE_TYPEs are static fields with int values in the JOptionPane class which tell the method which message to display:


- ERROR_MESSAGE


- INFORMATION_MESSAGE


- WARNING_MESSAGE


- QUESTION_MESSAGE


- PLAIN_MESSAGE

How the user interacts with Dialog boxes

- each time a dialog box is displayed, the user must press ok or enter ect before the next one appears, the code is paused until the user makes his/her input, just like Scanner keyboard

How to use JOptionPane.showInputDialog methods

- two overloaded options (in this book), both return a string from the user, if cancel is selected, null is returned.


- 1st option puts the default string "input" in the message header and displays the text you pass it with a text box for the user to type something in




String name = JOptionPane.showInputDialog("Whats you name")




- 2nd option allows you to pass it a graphical componet, message, message tittle, and message_type




String name = JOptionPane.showInputDialog(null, "Whats your name?", "name input", JOptionPane.MESSAGE_TYPE

How to implement JOptionPane.showConfirmDialog

- contains two overloaded options (in this book) that have 3 options, yes, no and cancel each returning an int,


- 1st accepts a parent graphical component (just enter null) and a string message to the user displayed in the center of the box.


int proceed = JOptionPane.showConfirmDialog(null, "Do you want to proceed?");




- 2nd accepts the same args as the other two dialog box types




int proceed = JOptionPane.showConfirmDialog(null, "Do you want to proceed", "proceed?", JOptionPane.MESSAGE_TYPE);




MESSAGE_TYPEs:


- YES_NO_OPTION


- YES_NO_CANCEL_OPTION

How to use JOptionPane.showConfirmMessage

When dealing with replies from the user, use the static methods in the JOptionPane class


- YES_OPTION


- NO_OPTION


- CANCEL_OPTION




you don't need to know what these values are, just use them, they'll be consistent bc they're final fields and its more obvious what you code is doing

System.exit(0) with JOptionPane

You must always use System.exit(0) with any GUI application because Java Swing classes open a thread elsewhere in the system and you must shut that down once the program complete or else it will continue to run and use up memory

How to use the JFrame class independently

- JFrame window = new JFrame("A Simple Window");


window.setSize(WINDOW_WIDTH*, WINDOW_HEIGHT*);


* Use final variables for the window dimensions so they can be altered easily

How to use JFrame by extending (the better method)

extend JFrame to your class and create customize your window to suit your needs.


- satisfy the super constructor with the String that will be be displayed in the window header


- Establish standards for the height and width of your window


- JFrame static fields for everything you can


- override setSize method


- override setDefaultCloseOperation method


- override setVisible method




setSize(WINDOW_WIDTH, WINDOW_HEIGHT);


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


setVisible(true);




**Create these in a specific project thats titled something memorable so you can just import that project anytime you're doing a GUI**

Difference between a panel, pane and frame

Ask in class

How to add elements to your JFrame (which serves as your entire GUI interface)


Step 1: fields and imports

First start by establishing the fields that will be used throughout the application this requires forthought


Declare classes used in the app


- private JPanel panel;


- private JLabel messageLabel;


- private JTextField userInput;


- private JButton pressMe;


- private final int WINDOW_HEIGHT;


- private final int WINDOW_WIDTH;


- private final int TEXT_FIELD_LENGTH;




Everything except window dimentions are classes in the Javax.Swing package and will be explained in better detail in the latter cards.


**you will need to import Javax.Swing*; and impot Java.awt.event*; **

How to build elements to your JFrame


Step 2: Build constructor

- First things first, satisfy the super constructor.


- then set the window size


- define what the close button does


- Call the super class's buildPanel() method to create a panel that will hold the JTextFields and JButtons ect..


- then add the panel by calling the super class method add() and pass it the JPanel variableName you defined as one of your fields. this must be done after you build the panel using the buildPanel() method (i think)


- set the visiblity to true


public WindowClassName{


super("message header tittle");


setSize(WINDOW_WIDTH, WINDOW_HEIGHT);


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


buildPanel();


add(JPanelVariableName);


SetVisibility(true);


}

How to build elements to your JFrame


Step 3: Build the components of your JPanel using the overriden method buildPanel()

this must use the same signature as shown below so that it override the JFrame buildPanel() method.


- Declare new JLabel and pass it the string message you want to tell the user


- declare a new JTestField and pass it an int (or field if you made one) representing the number of characters long you want the text field to be


- declare a new JButton and pass it the string name you want the button to be, then add an action listener to the button (defined next card)


- create your JPanel object and add all the previously defined components to it


public void buildPanel(){


messageLabel = new JLabel("Enter ur stuff into my textBox")


userInput = new JTextBox(TEXT_FIELD_LENGTH);


pressMe = new JButton("Touch ME");


pressMe.addActionListener(new CalcButtonListener());


panel = new JPanel();


panel.add(messageLabel);


panel.add(userInput);


panel.add(pressMe);


}

How to build elements to your JFrame


Step 4: build ActionListener as a class inside of the class for the window that implements the ActionListener interface




(assume we are calculating a kilometers to miles conversion)

- This class is declared inside the class for the window and looks like a method declaration except it has class in the header(p 767-8). The class name can be any name you want as long as its the same as the new object you declare as a parameter for the addActionListener() method from the JButton class


- inside the curly braces define a method that will execute the code you want and display it to the user. The method will be added automatically bc its part of the ActionListener interface


- use the variables defined earlier in the program to carry out your operations just as you in a regular java main method


public class CalcButtonListener implements ActionListener{




public void actionPerformed(ActionEvent e){


final double CONVERSION = .6214;


String input = userInput.getText();


double miles = Double.parseDouble(input) * CONVERSION;


JOptionPane.showMessageDialog(null, input + " kilometers is " + miles + "miles", "Miles Conversion");


}


}


**You must create a new class that implements ActionListener for each JComponent if you want it to behave differently. You can use 1 ActionListener if you want them all to behave the same or similarly. Sometimes you can create 1 action listener that has many functions by using its methods detailed in later cards**


***action listeners are only used if you want to execute code as soon as something occurs, you can omit them otherwise if you are tallying cost ect..***



How to build elements to your JFrame


Step 5: include a main method declaration at the end of the class

- this allows your window GUI application to run autonomously (i think). You simply write a main method header and then declare a new object of the type you're making




public static void main(String args[]){


new WindowClassName();


}

More on the ActionListener Interface usage

- When we implemented the ActionListner interface in step 4 of building the JFrame we had to accept and ActionEvent object defined as e in the method header. Although we did not use the variable e a all, we still need to include it or else we would have a different method signature and the interface would get mad and tell us to override that method with ActionEvent as a parameter


- we can also use that variable e (or whatever we want to call it). It operates similarly to catching exceptions and it has a few methods




- e.getActionCommand() will return the action command. In other words, since our JButton was called touchMe, it would return the text touchMe so we know which button was pressed




- e.getSource() will return the object that was the source of the event. In other words it will return a reference to the touchMe JButton object

how to use the ActionListener variable in the ActionListener interface method




e.getActionCommand();


e.getSource();

e.getAction returns a string


String action = e.getActionCommand();


if(action.equals("StringOnButton")){


JOptionPane.showMessageDialog(null, "You clicked the 'StringOnButton' button");


// this refers to all objects that use ActionListeners, not just buttons




e.getSource()


if(e.getSource() == touchMe){


JOptionPane.showMessageDialog(null, "ha ha you clicked the touchMe button you perv");


}


touchMe is a reference variable to a JButton object in this example and is being checked to see if it is the same object as e.getSource(). again this does not only work with JButtons. You will have access to all the JButton methods with e.getSource() as well.

Defining colors of your components




Assume we want to modify the color of a JButton





Component is a superclass that all the other JComponents inherit from. Component is part of the Java.awt package and contains methods that are available to is subclasses. a static class called Colors has a bunch of static fields with predefined colors stored as ints. below is an example for defining the colors of a JButton. The .setBackground() method sets the color of the background on the button while the .setForground() sets the color of the text. They both accept ints which you can pass it a static field of predefined colors from the Color class




JButton butt = new JButton("tits");


butt.setBackground(Color.BLUE);


butt.setForground(Color.GREEN);



changing the color of the content pane

- sometimes the panel does not cover up the entire content pane, and therefore you might want to change the color of the content pane. This is not common but can happen so use the method below in the constructor (i think)




getContentPane().setBackground(Color.blue);


(idk why blue is not in all caps)





How to set layout managers for a JPanel and JFrame

JPanel


- you must declare a JPanel and then call its setLayout() method and pass it a layout manager object. FlowLayout is the default layout manager for JPanel so if thats what you want you need not declare it


JPanel panel = new JPanel();


panel.setLayout(new FlowLayout());




- for JFrame you can call its method directly in the constructor. BorderLayout is the default layout manager for JFrames.


setLayout(new BorderLayout());


Layout Managers are in the Java.awt package


setLayout(new BorderLayout);

Different types of Layout Managers:




FlowManager()

FlowLayout(): organizes everything in rows from left to right in the order they were added. All rows are aligned to the center with 5 pixel gap between components by default. You can change the alignment and spacing by calling the overloaded constructor


setLayout(new FlowLayout(FlowLayout.LEFT, 10, 7));




1st is a field with a predefined value for left alignment, the other two parameters are horizontal gap and vertical gap respectively

Different types of Layout Managers:




BorderManager()

BorderLayout(): organizes everything in 5 regions defined as North, south, east, west and center. You must define the region that you want the component to be added when you add it to a JFrame or JPanel by using a static field in the BorderLayout() class. The components will be stretched to fill the entire region. The center region is the default region if no other region is defined. Only 1 component can be added to a region at a time




JPanel panel - new JPanel();


JButton touchMe = new JButton("Click it!");


panel.setLayout(new BorderLayout());


panel.add(touchMe, BorderLayout.EAST);




If you have more than 5 components to add or you want your buttons to retain their size, you can define a the JFrame with a border manager and fill the regions with JPanels that have flow managers or grid managers. Then the "nested" JPanel will be expanded to fill the region but the buttons will retain their shape.


JPanel babyPan = new JPanel();


babyPan.setLayout(New FlowLayout(FlowLayout.CENTER, 5, 5));


babyPan.add(touchMe);


setLayout(new BorderLayout());


setLayout(babyPan, BorderLayout.CENTER)

The pack() method

automatically resizes the the window to accommodate the components contained inside of it




call this method right before you called the setVisible() method

Different types of Layout Managers:




GridManager()

GridLayout(): organizes everything in rows and columns. everything is sized equally and takes on the size of the largest component. All components are expanded to fill all the space and only one component per space. You cannot choose which cells are filled, they are filled in the order they are added starting at the upper left and filling across before down


- you must define the size of the grid, putting a zero is permitted for either columns or rows but not both


- you may nest JPanels to get around sizing constraints as shown in the previous card


setLayout(new GridLayout(3,3));


JPanel babyPan1 = new JPanel();


JButton button = new JButton("touchIt");


babyPan1.setLayout(new FlowLayout(FlowLayout.Center, 5, 5));


babyPan1.add(button)


add(babyPan1);



JRadioButton: about/syntax

Take the form of check boxes indicating whether a choice is true or false. has two overloaded constructors. Only 1 radio button in a group can be selected at a time


JRadioButton rad = new JRadioButton("Text");


JRadioButton rad2 = new JRadioButton("Text", true);




- The 1st constructor accepts text that will be displayed in proximity to the button.


- The second accepts the same and accepts a boolean that will show the buttons initial state as selected if true is passed in.




JRadioButtons must be managed by a ButtonGroup (next card). You must add JRadioButtons to a JPanel individually. JRadioButtons generate ActionEvents so each needs to add either the same or different ActionEvent objects


rad2.addActionListener(new ActionListenerName());

JRadioButton: ButtonGroup object

The ButtonGroup object manages a group of JRadioButtons and ensures all RadioButtons accept one is selected. if a different button is selected, it will deselect the previous




ButtonGroup groupJob = new ButtonGroup();


groupJob.add(button1);


groupJob.add(button2); ect...




ButtonGroups cannot be added to a JPanel, you must ass JRadioButtons individually




You can add a ButtonGroup to a JPanel

JRadioButton: methods

.isSelected()


- method that returns true if that particular radiobutton is selected




.doClick()


will turn the radio button to true just as if a user clicked it. It also causes an ActionEvent to be generated

JCheckBox: about

operates every similarly to JRadioButton class except more than one can be clicked at a time. Add them to a JPanel the same way as any other component. Has the same overloaded constructors as JRadioButton and the same methods




Generates an ItemEvent which operates very similarly to an ActionEvent but is different (covered next card);

ItemListener interface

- operates very similarly to ActionEvent interface




- class that implements ItemListener must override itemStateChanged(ItemEvent e) method. again only used if you want an action to take place at the time of the event




- All the same steps (create class, create overridden method, add listener to JCheckBox, have code that executes inside method when ItemEvent is generated)

Borders and BorderFactory class

- borders can be added to any JComponent whether a JFrame or a JPanel and even a JRadioButton.


- to set a border use the setBorder(border b) method contained in every JComponent


- use the static class BorderFactory to pass a border to the setBorder() method thru use of its many static methods (for a list off the static methods and their attributes/syntax refer to page 811-2)


JPanel panel = new JPanel();


panel.setLayout(new FlowLayout());


JButton button1 = new JButton("a");


JButton button2 = new JButton("b"); ect..


panel.setBorder(BorderFactory.createTitledBorder("Description of whats contained int he border"));

Overview

- you may use JButtons without generating action listeners if you want to tally charges but not execute any particular code upon tallying


- create a separate class that extends JPanel for each panel if you are making multiple panels and add them to your class that extends JFrame


- figure out what BuildButtonPanel() method does


- find out what the difference between a component panel and JPanel is


- find out how to execute GUI classes (whether they need to be called in a main method or not)

Spash Screen

Basically puts a picture of a logo while the user waits for the GUI app to load




java -splash: graphicFileName ClassFileName




say graphic name is pic.jpg and class name is CoffeeHouseGUI


java -splash: pic.jpg CoffeeHouseGUI




the graphic file name should be stored in the same folder as the class because these are not path names so it will only be pulling from the fold in the Class's filePath