CS 235 - Week 5 Lecture - 2015-09-23
* Java's Swing packages also include support for adding
borders to components, using classes in the package
javax.swing.border
* playing with borders a bit in DrJava's interactions window:
Welcome to DrJava. Working directory is /Users/smtuttle/humboldt/f15cs458/458lectures/458lect05
> // Java Swing provides another package, just for supporting
> // customer borders
> import javax.swing.border.*;
// (and we'll also use some components from the package javax.swing,
// such as JFrame, JPanel, JLabel)
> import javax.swing.*;
> // set up a frame to play with
> JFrame myFrame = new JFrame();
> myFrame.setSize(500, 500);
> JPanel myPanel = new JPanel();
> // I can set a border for a number of components
> // using a setBorder method
> myPanel.setBorder(new TitledBorder(new EtchedBorder(),
"JPanel Border"));
> myFrame.add(myPanel);
> JLabel myLabel = new JLabel("A looooonnnnnnnggggggg label border");
> myLabel.setBorder(new TitledBorder(new EtchedBorder(),
"JLabel Border"));
// and, to see my now-set-up frame, with its panel with a label:
> myFrame.setVisible(true);
// you can force a re-layout of a JFrame and its components
// using its revalidate method -- here, I add the label
// to my panel, and then call revalidate on the frame to
// be able to see this newly-added label
// (NOTE: not all changes, especially when in the context
// of a running application, require a call to revalidate to
// make them visible -- but if a change you expect is not
// showing up, it is a possibility to try)
> myPanel.add(myLabel);
> myFrame.revalidate();
// seeing how this looks, and what happens with the label's
// titled border, for different label contents
> myLabel.setText("a lonnnnnng label with a border!");
> myFrame.revalidate();
> myLabel.setText("Yo");
> myFrame.revalidate();
another component: JTextField
* we can talk about input components,
output components, and input/output components;
JLabel? more of an output component
JButton? typically an input component
JTextField? really reasonable to use for both
input and output -- so, input/output
* JTextField - intended for one line of
input/output only
...if more than 1 line? JTextArea, for example
(btw: for a password, JPasswordField)
(and more components are available, also)
* three of JTextField's constructors:
JTextField(int numColumns) -
you'd like this textfield to be "about"
numColumns columns long,
where a column is the expected width of
one character in its current font
JTextField(String initText) -
initially displays the field with that text
JTextField(String initText, int numColumns)
initially displays initText in a textfield
about numColumns columns wide
* NOW continuing experimenting in DrJava's Interactions
window, playing with JTextField
> JTextField myText1 = new JTextField(15);
> myPanel.add(myText1);
> myFrame.revalidate();
// someone asked: what happens if you give this constructor
// a negative column length? Let's find out:
> JTextField negTest = new JTextField(-3);
java.lang.IllegalArgumentException: columns less than zero.
at javax.swing.JTextField.<init>(JTextField.java:233)
at javax.swing.JTextField.<init>(JTextField.java:198)
// seeing how a zero-column-width JTextField appears:
> JTextField zeroTest = new JTextField(0);
> myPanel.add(zeroTest);
> myFrame.revalidate();
// trying the constructor that expects initial contents
> JTextField stringTest = new JTextField("Moooooooooo");
> myPanel.add(stringTest);
> myFrame.revalidate();
// trying the constructor that expects initial contents and
// desired column width
> JTextField stringColTest = new JTextField("Yo!?", 18);
> myPanel.add(stringColTest);
> myFrame.revalidate();
// and JTextField's have a getText method that returns
// a String containing the textfield's current contents
// (here, I had typed more into stringColText's textfield
// before calling getText)
> stringColTest.getText()
"Yo!? How are you? I am fine?"
// setText can set the text displayed in a JTextField
> stringColTest.setText("ARRRRGGGGG!");
// setEditable can be used to set whether a JTextField can
// be edited by the user (true) or not (false) --
// here, I am changing stringColTest so the user
// now CANNOT type into it
> stringColTest.setEditable(false);
// it IS nice to somehow set the background of an uneditable
// textfield to cue the user that they cannot type in it
> stringColTest.setBackground(Color.PINK);
Static Error: Undefined name 'Color.PINK'
// oops, Color is in package java.awt
> import java.awt.*;
> stringColTest.setBackground(Color.PINK);
>