Java SWING Component Reference -...

12
1 Java SWING Component Reference Methods for JButton will need to create an ActiveListener function so each button can trigger an event when a button is pushed many Java Components have the same methods found below!!! o like JRadioButton, JCheckBox, etc… a complete list of methods for JButton can be found at o http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/AbstractButton.html o http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JComponent.html Constructors JButton(String Text) JButton(ImageIcon picture) JButton(String Text, ImageIcon picture) Popular Methods void setText(String text) // sets the text ON the button button1.setText(“Mr. L stinks”); button1.setText(“<html>E<br>N<br>T<br>E<br>R<br></html>”); // no kidding void requestFocus( ) void setIcon(ImageIcon icon) void setBackground(Color c) // background of the button button1.setBackground(Color.BLUE); // Color covered later void setForeground(Color c) // text of the button void addActionListener(ActionListener object) void setDisabledIcon(ImageIcon); button1.setDisabledIcon(dimmedIcon); //dimmedIcon already created ImageIcon void setVerticalTextPosition(JButton); button1.setVerticalTextPosition(AbstractButton.CENTER); void setActionCommand(String); button1.setActionCommand(“Hit Me”); boolean isEnabled(); if(button1.isEnabled())

Transcript of Java SWING Component Reference -...

Page 1: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

1

Java SWING Component Reference

Methods for JButton

will need to create an ActiveListener function so each button can trigger an

event when a button is pushed

many Java Components have the same methods found below!!!

o like JRadioButton, JCheckBox, etc…

a complete list of methods for JButton can be found at o http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/AbstractButton.html

o http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JComponent.html

Constructors JButton(String Text)

JButton(ImageIcon picture)

JButton(String Text, ImageIcon picture) Popular Methods

void setText(String text) // sets the text ON the button

button1.setText(“Mr. L stinks”);

button1.setText(“<html>E<br>N<br>T<br>E<br>R<br></html>”); // no kidding

void requestFocus( )

void setIcon(ImageIcon icon)

void setBackground(Color c) // background of the button

button1.setBackground(Color.BLUE); // Color covered later

void setForeground(Color c) // text of the button

void addActionListener(ActionListener object)

void setDisabledIcon(ImageIcon);

button1.setDisabledIcon(dimmedIcon); //dimmedIcon already created ImageIcon

void setVerticalTextPosition(JButton);

button1.setVerticalTextPosition(AbstractButton.CENTER);

void setActionCommand(String);

button1.setActionCommand(“Hit Me”);

boolean isEnabled();

if(button1.isEnabled())

Page 2: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

2

void setEnabled(Boolean);

button1.setEnabled(true);

Icon getIcon();

String file = “”+button1.getIcon() // return text filename

Inside the action listener, have ONE OF the buttons turn the background RED when

pressed. Compile and Run. -> (SLIP)

JLabel (clear)

used to place labeled (uneditable) text or icons on the container

text alignment called SWING CONSTANTS

o LEFT

o CENTER

o RIGHT

o LEADING

o TRAILING

no Listeners are needed

few Programming notes

o Changing the background of the label may not work

Set the background of the JPanel instead

By default the label is transparent/clear

o You cannot use the same label twice:

Create separate blank labels for the application

Constructors

JLabel(String Text) private JLabel label1 = new JLabel("Lupoli is nutz");

JLabel(ImageIcon icon)

JLabel(String Text, ImageIcon icon, SwingConstants.Align)

Methods Setting Properties

Text

Page 3: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

3

void setFont(Font font) // create font first!!

void setOpaque(true);

void setBackground(Color);

label1.setOpaque(true); // makes it NOT clear

label1.setBackground(Color.blue); void setHorizontalAlignment(JLabel) label1.setHorizontalAlignment(JLabel.CENTER);

// LEFT, RIGHT void setVerticalAlignment(JLabel) label1.setVerticalAlignment(JLabel.CENTER); void setHorizontalTextPosition(JLabel) label1.setHorizontalTextPosition(JLabel.CENTER);

// LEFT, RIGHT void setBorder(BorderFactory);

void setText(String text) label1.setText(“Loading…”);

void setIcon(ImageIcon icon) label1.setIcon(null); // no icon will appear, could

// have ANOTHER ImageIcon

1. Download your GUI II Class Example

2. Use Eclipse to copy and paste or import

3. The label is already created in the private section. In the constructor:

a. change the text to “Lupoli is da man”

b. the background color to Blue

c. the foreground color to Yellow

d. add it to the Panel and run the project

Page 4: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

4

JTextField

1. a textbox that a user can input info, or a program to display data

o edit a SINGLE line of text

2. can be editable, or locked

3. <ENTER> generates an action event

4. there IS a listener associated with this GUI component

5. one thing to watch is how large the JTextField is compared to the actual

JFrame/JPanel size

6. Placing values in a JTextField

o MUST USE “getText” or “setText” to retrieve or set text in a

JTextfield

Constructors

JTextField(String Text)

JTextField(int cols) private JTextField textField1 = new JTextField(30);

JTextField(String Text, int cols) Methods Setting properties

void setFont(Font font)

void setEditable(boolean) text.setEditable(false);

void setText(String text) text.setText(“Lupoli is da man”);

void setBackground(Color c)

void setLineWrap(Boolean) // ***** text.setLineWrap(false);

void setForeground(Color c) Methods getting properties

String getText( ); String answer = text.getText(); Other important methods

void insert(String text, int pos)

1. The JTextField is already created. In the constructor, create the code:

a. change the text to “Lupoli is da man”

b. the background color to Blue

c. make it uneditable

Page 5: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

5

d. add it to the Panel and run the project

Page 6: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

6

JTextArea

7. a textbox that a user can input info, or a program to display data

o edit a MULTIPLE lines of text

8. can be editable, or locked

9. <ENTER> generates an action event

10. may also want to associate a JSlider with the JTextArea

11. there is NO listener associated with this GUI component

12. used USUALLY with a BorderLayout( ) design

13. one thing to watch is how large the JTextArea is compared to the actual

JFrame size

Constructors

JTextArea(String Text)

JTextArea(int rows, int cols) private JTextArea textArea1 = new JTextArea(20, 30);

JTextArea(String Text, int rows, int cols) Methods Setting properties

void insert(String text, int pos)

void append(String text)

void setFont(Font font)

void setEditable(boolean) text.setEditable(false);

void setText(String text) text.setText(“Lupoli is da man”);

void setBackground(Color c)

void setLineWrap(Boolean) // ***** text.setLineWrap(false);

void setForeground(Color c) Methods getting properties

String getText( ); String answer = text.getText();

Page 7: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

7

Making a JTextArea into a ListBox

You can make the JTextArea list items on different line

Creating it is just the same but you must use the APPEND command and a ‘n’

to denote an end to THAT line

textbox.append(String + '\n');

2. The JTextArea is already created. In the constructor, create the code:

a. change the text to “Lupoli is da man”

b. ADD the text on a new line “Well, not really”

c. the background color to Blue

d. make it uneditable

e. add it to the Panel and run the project

JScrollPane

ALL CODE IN THE CONSTRUCTOR!!!

a textarea/field must be already created

this is “added” to it

then add the SCROLL to the panel/pane

// set grid1 to textArea

JTextArea textbox = new JTextArea(20,30);

textbox.setEditable(true);

textbox.setBackground(Color.white);

textbox.setForeground(Color.blue);

textbox.setLineWrap(true);

JScrollPane x = new JScrollPane(textbox);

content.add(x);

if you are creating a chat program and want the textarea/scrollpane scroll

down automatically when an item is added use the code below:

JTextArea textbox = new JTextArea(20,30);

textbox.setCaretPosition(textbox.getText().length);

add a scrollPanel to our TextArea

Page 8: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

8

JComboBox

ComboBox has multiple options

o all options are contained in a String array

uses an ActionListener

o but unique, covered below

option you select will create an event

Constructors

JComboBox(String items[ ]) private String items[] = {"Test 1", "Test 2", "Exit"};

private JComboBox combo = new JComboBox(items);

Methods Getting properties

int getSelectedIndex(int itemNum) int selected = combo.getSelectedIndex( );

Object getSelectedItem( ); String selected = (String) combo.getSelectedItem( );

Methods setting properties

void setSelectedIndex(int itemNum); combo.setSelectedIndex(0);

void setSelectedItem(String text); // or use this combo.setSelectedItem(“Exit”); Other important Methods

void addItem(Object anObject) combo1.addItem("Lupoli is da man");

void

addActionListener(ActionListener l) combo1.addActionListener(new ComboListener());

ButtonGroups

button groups are for RadioButtons, ToggleButtons, etc… where you only

want ONE selected out the of many JToggleButton JRadioButton

INDEX

[0]

[1]

[2]

Page 9: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

9

ButtonGroup Procedure

private JRadioButton choiceA1 = new JRadioButton("Celsius");

private JRadioButton choiceB1 = new JRadioButton("Fahrenheit");

private JRadioButton choiceC1 = new JRadioButton("Kelvin");

private JRadioButton choiceD1 = new JRadioButton("None");

// WITHIN CONSRUCTOR!!

ButtonGroup question1 = new ButtonGroup();

ButtonGroup question2 = new ButtonGroup();

ButtonGroup question3 = new ButtonGroup();

centerPanel.add(choiceA1); // added to JPanel

centerPanel.add(choiceB1);

centerPanel.add(choiceC1);

centerPanel.add(choiceD1);

question1.add(choiceA1); // added to ButtonGroup

question1.add(choiceB1);

1. create

ButtonGroup(s)

after GUI

Components,

but in same

section

2. Add GUI to

JPanel as normal

3. Then add GUI

to ButtonGroup

as well

1. The JRadioButtons are already created. In the constructor, create the code:

a. Create ONE button group named “question” for all JRadioButtons

b. ADD all buttons to the PANEL

c. ADD all buttons to the ButtonGroup

Page 10: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

10

ImageIcons

A few GUI components can be Images

Must create an ImageIcon before it can be added to a JButton, JPanel, JLabel

NOTICE, the JButton(ImageIcon)’s will be determined by the size of the

picture unless otherwise set

Constructors

JComboBox(String source) ImageIcon back = new ImageIcon("./src/Blue

hills.jpg");

private JButton icon = new JButton("None");

Methods Getting Properties

getIconWidth() // returns int in pixels int width = front.getIconWidth();

getIconHeigth()// returns int in pixels int heigth = front.getIconHeigth ();

getImage() // return text filename String filename = “”+front.getImage();

Methods Setting Properties

setImage(String filename) front.setImage(“6.jpg”);

Setting up a ImageIcon on a JButton public GUI_II_Examples()

{

ButtonGroup question1 = new ButtonGroup();

icon.setIcon(back); // adds back pictures to Button Icon

add(icon); // add Icon to JPanel

Page 11: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

11

ImageIcons and Text

An image CAN have text on top if it

The size and middle of the JComponent will be determined by the image

methods needed

button2.setVerticalTextPosition(SwingConstants.CENTER);

button2.setHorizontalTextPosition(SwingConstants.CENTER);

Without setting Position Setting Positions

add the code to place a button. You will need to copy and paste a picture in the src

folder first.

then add the code to center the text on the button

Color Selection

must import java.awt.*;

predefined choices are limited

place below into syntax for either setBackground( ) or setForeground( ) Colors in Java

Color.black Color.magenta

Color.blue Color.orange

Color.cyan Color.pink

Color.darkGray Color.red

Color.gray Color.white

Color.green Color.yellow

Color.lightGray

button.setForeground(Color.black);

can set color manually

o button.setForeground(new Color(r, g, b));

r g b range from 0 to 255

Page 12: Java SWING Component Reference - faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/Java/supplements/Java Component Reference.pdf1 Java SWING Component Reference Methods for JButton

12

Fonts

a font can be added to nearly every GUI Component we are about to list

many books forget to cover fonts with GUI Components

o convert fonts in Applets

Procedure

o First decide which GUI Component you wish to give a special font

o Second, decide on a Font

Minimal Font types available in Java

Serif Monospaced SansSerif Courier

There are others, just try!!

o Third, pick a style

Complete list of Font Styles in Java

Font.PLAIN Font.BOLD Font.ITALIC

o Fourth, pick a Font size

1 (smallest)

o Fifth, create GUI component

private JLabel label = new JLabel("1st GUI with Motors and Java", null,SwingConstants.CENTER);

o JUST BEFORE adding the GUI Component to a Layout, set FONT,

and Color (color optional)

Font font1 = new Font("Monospaced", Font.PLAIN, 20);

// Font font1 = new Font("Type", Style, Size);

label.setFont(font1);

label.setForeground(new Color(13, 34, 34)); // setting color (optional)

add(label); // always add to JPanel last