Sunday, February 12, 2012

Creating labels and text

The Swing API provides classes and methods that enable you to create and add images and non-interactive text to an application.

You use the JLabel class to create and display non-interactive text and images.
You can create an instance of the JLabel class using the constructor with no arguments, or you can specify a string or an icon, or both.


public JLabel()
public JLabel(String st)
public JLabel(Icon ic)
public JLabel(String st, Icon ic)


You can also pass the JLabel constructor an int argument that specifies the horizontal alignment of the contents of the label. The integer argument can have one of the constant values - LEFT, CENTER, RIGHT, LEADING, or TRAILING - defined in the SwingConstants interface.


public JLabel(String st, int num)
public JLabel(Icon ic, int num)
public JLabel(String st, Icon ic, int num)


After you create a label, you can place text and images in it. You can do this using the setText, getText, setIcon, and getIcon methods of the JLabel class.


public void setText(String text)
public String getText()

public void setIcon(Icon image)
public Icon getIcon()



You can also use the following methods to set the alignment of a label's contents:
  • setHorizontalAlignment
  • getHorizontalAlignment
  • setVerticalAlignment
  • getVerticalAlignment

public void setHorizontalAlignment(int halign)
public int getHorizontalAlignment()

public void setVerticalAlignment(int valign)
public int getVerticalAlignment()


Suppose you are writing an application with a user interface that has a number of radio buttons and a label.

The text of the label indicates which radio button is selected.
You use the JLabel constructor to create a new label that contains an empty string. You then add the label to the pane.

//Create and add a label
label = new JLabel("    ") ;
pane.add(label, BorderLayout.CENTER) ;

You use the setText method to set the label's text to indicate
which radio button a user selected.

class myRadioListener implements ActionListener{
  public void actionPerformed(ActionEvent e) {
    label.setText(icecreams.getSelection().getActionCommand()) ;
  }
 


Question
Suppose you're writing an application that needs a label that contains the string "Hello" on the user interface.
Complete the code to create the label.

label = MISSING CODE ; pane.add(label, BorderLayout.CENTER) ;
 

Answer

To create the label, you use the code
 
new JLabel("Hello")
 
You use one of Swing's text components to display text and enable users to edit it. Swing's text components enable users to create the following categories of text areas:
  • text controls that can display and edit only one line of text
  • plain text areas that can display multiple editable lines of text
  • styled text components that can display and edit text using more than one font, and sometimes allow embedded images and components
Swing provides six text components, all of which inherit from the JTextComponent superclass.
  • JTextField
  • JPasswordField
  • JFormattedTextField
  • JTextArea
  • JEditorPane
  • JTextPane
JTextField
You use text fields - also known as text controls - to obtain a small amount of text from the user and perform an action once the text entry is complete. Text fields generate action events in the same way as buttons. For example, to log on to your computer, you enter your username. The system logs you in after you've entered your username.

You can create JTextField with no constructor parameters, or you can specify an initial string or the width of the field as an integer, or you can specify both.

The syntax for the constructor includes


public JTextField()
public JTextField(String text)
public JTextField(int size)
public JTextField(String text, int size)
 
JPasswordField
JPasswordField is a subclass of the JTextField class and as such is also a text control. For security purposes, this field does not display the characters that a user enters.

You can create a JPasswordField using a constructor with no arguments, or you can specify an initial string or an initial field size as an integer, number of columns, or both.

The syntax for the constructor includes


public JPasswordField()
public JPasswordField(String text)
public JPasswordField(int size)
public JPasswordField(String text, int size
 


JFormattedTextField
JFormattedTextField is a subclass of the JTextField class that enables you to specify a set of characters that users can enter into a text field. For example, you can specify the order in which users should enter the date - such as yy/mm/dd.

JFormattedTextField has an object value and a formatter that translates the field's value into the text displayed. You can create a JFormattedTextField using a constructor with no arguments, or you can specify an object value, a Format object, or an AbstractFormatter.

The syntax for this constructor includes

public JFormattedTextField()
public JFormattedTextField(Object val)
public JFormattedTextField(Format obj)
public JFormattedTextField(AbstractFormatter abs)
 
JTextArea
You can use JTextArea to display multiple lines of text in any font but all the text in the area must be in the same font. You can use a text area to display unformatted help information or to enable unformatted text of any length to be entered by users.

You can create a JTextArea using a constructor with no arguments, or you can specify an initial string or the width and height of the text area using an integer, number of columns, and rows, respectively. You can also specify both the string and the dimensions of the text area.

The syntax for the constructor includes
public JTextArea()
public JTextArea(String text)
public JTextArea(int width, int height)
public JTextArea(String text, int width, int height)

JEditorPane
The JEditorPane is a styled text component that can display and edit text using more than one font. The JEditorPane knows how to read, write, and edit plain text, HTML, and Rich Text Format (RTF) text. Editor panes are useful for displaying uneditable help information because they can be easily loaded with formatted text from a URL.

You can create a JEditorPane using a constructor that specifies the URL from which to load formatted text, or you can specify a string. If the referenced HTML or RTF file includes images, those images will be displayed in the Editor pane.

A simple web browser can easily be built using JEditorPane.

The syntax for the constructor includes



public JEditorPane(URL)
public JEditorPane(String text)

JTextPane
JTextPane is a subclass of JEditorPane and, as such, is a styled text component. You can use JTextPane to graphically represent attributes within a text component. Images and even components can be displayed in a text pane.

You can create a JTextPane using a constructor with no arguments, or you can specify the text pane's model.

A simple word processor can easily be built using JTextPane.

The syntax for the constructor includes
public JTextPane()
public JTextPane(StyledDocument model)


Suppose you're creating a user interface that contains a text field, a password field, a formatted text field, and a text area.

You've declared a JLabel called label, a JTextField called confirm, a JPasswordField called pass, a JFormattedTextField called format, and a JTextArea, called area.

To create the password field, you create an instance of JPasswordField - called pass - and you specify the length of the field - in this case, 20 columns wide.

 
You also create an instance of JTextField - confirm - that displays the password for a user to confirm. Once the password is confirmed, it is displayed in the text area. When creating this instance, you also specify the size of the field.


// A JPanel will hold the contents
  JPanel pane = new JPanel(new GridLayout(4,1)) ;

  confirm = new JTextField (25) ;
  pass = new JPasswordField(20) ;
  format = new JFormattedTextField(DateFormat.getDateInstance()) ;
  format.setValue(new Date()) ;
  format.setEnabled(false) ;
  area = new JTextArea(10, 20) ;

  pane.add(confirm) ;
  pane.add(pass) ;
  pane.add(format) ;
  pane.add(area) ;

        pass.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      confirm.setText(new String(pass.getPassword())) ;
    }
  }) ;

        confirm.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       area.setText(area.getText() + '\n' + confirm.getText()) ;
    }
  }) ;
 

You want the interface to contain a formatted text area that displays the date.

So you create an instance of JFormattedTextField that has the format of a Date object, DateFormat.
Next you use the setValue method of JFormattedTextField to set the value to be formatted in this format.

Then you use the setEnabled method and pass the boolean literal value of false as a parameter to the method to disable the text field so that users cannot edit it.


// A JPanel will hold the contents
  JPanel pane = new JPanel(new GridLayout(4,1)) ;

  confirm = new JTextField (25) ;
  pass = new JPasswordField(20) ;
  format = new JFormattedTextField(DateFormat.getDateInstance()) ;
  format.setValue(new Date()) ;
  format.setEnabled(false) ;
  area = new JTextArea(10, 20) ;

  pane.add(confirm) ;
  pane.add(pass) ;
  pane.add(format) ;
  pane.add(area) ;

        pass.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      confirm.setText(new String(pass.getPassword()));
    }
  }) ;

        confirm.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       area.setText(area.getText() + '\n' + confirm.getText()) ;
    }
  }) ;

The interface needs to contain a plain text area, so you create an instance of JTextArea - area - and specify the number of rows and columns.


// A JPanel will hold the contents
  JPanel pane = new JPanel(new GridLayout(4,1)) ;

  confirm = new JTextField (25) ;
  pass = new JPasswordField(20) ;
  format = new JFormattedTextField(DateFormat.getDateInstance()) ;
  format.setValue(new Date()) ;
  format.setEnabled(false) ;
  area = new JTextArea(10, 20) ;

  pane.add(confirm) ;
  pane.add(pass) ;
  pane.add(format) ;
  pane.add(area) ;

        pass.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      confirm.setText(new String(pass.getPassword())) ;
    }
  }) ;

        confirm.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       area.setText(area.getText() + '\n' + confirm.getText()) ;
    }
  }) ;


You use the addActionListener method to add an action listener to the password field - pass - so that it can receive and respond to action events.
And you use the getPassword method of JPasswordField in the actionPerformed method of the password field's ActionListener to return the text of the password field as a string and display it in the confirm text field.



// A JPanel will hold the contents
  JPanel pane = new JPanel(new GridLayout(4,1)) ;

  confirm = new JTextField (25) ;
  pass = new JPasswordField(20) ;
  format = new JFormattedTextField(DateFormat.getDateInstance()) ;
  format.setValue(new Date()) ;
  format.setEnabled(false) ;
  area = new JTextArea(10, 20) ;

  pane.add(confirm) ;
  pane.add(pass) ;
  pane.add(format) ;
  pane.add(area) ;

        pass.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      confirm.setText(new String(pass.getPassword()));
    }
  });

        confirm.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       area.setText(area.getText() + '\n' + confirm.getText()) ;
    }
  }) ;

You add an action listener to the confirm field so that it can respond when text is entered into it.
You use the getText and setText methods of JTextComponent in the actionPerformed method of the action listener for confirm. This will add the password, as a string, to the text already displayed in the text area.


// A JPanel will hold the contents
  JPanel pane = new JPanel(new GridLayout(4,1)) ;

  confirm = new JTextField (25) ;
  pass = new JPasswordField(20) ;
  format = new JFormattedTextField(DateFormat.getDateInstance()) ;
  format.setValue(new Date()) ;
  format.setEnabled(false) ;
  area = new JTextArea(10, 20) ;

  pane.add(confirm) ;
  pane.add(pass) ;
  pane.add(format) ;
  pane.add(area) ;

        pass.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      confirm.setText(new String(pass.getPassword())) ;
    }
  }) ;

        confirm.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       area.setText(area.getText() + '\n' + confirm.getText()) ;
    }
  }) ;


When you run the code, the application's GUI displays the four text areas.

1 comments:

Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
kajal agarwal hot

Post a Comment