Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


Java Tutorials



Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java: Summary - Basic GUI Components


Swing API provides lots components for crating the GUI for Java Swing applications.


In this section we are studying the Basic GUI components of Swing framework.


We will study the following GUI components:


a) JLabel

b) JTextField

c) JButton

d) JTextArea

e) JCheckBox

f) JList


Let's see all these components in detail.


Most components have a few common methods.
cmp.requestFocus(); Puts focus (eg, blinking cursor) in field, select button, etc.
tf.setFont(f); Sets font.
JLabel - For fixed text.
lbl = new JLabel(t) Creates JLabel with text on it. Typically created in call to add() and not assigned.
JTextField - Box containing one line of text.
tf = new JTextField(n); Creates textfield about n columns wide.
s = tf.getText(); Returns string in textfield.
tf.setText(s); Sets text to s.
tf.addActionListener(lst); Action listener lst will be called if enter typed.
tf.setEditable(bool); Don't allow user to edit text field used for output.
tf.setHorizontalAlignment(align); JTextField.LEFT (default), JTextField.CENTER, or JTextField.RIGHT
JButton - Standard clickable button.
btn = new JButton(t); Creates button with text t.
btn = new JButton(img); Creates button with icon img.
btn = new JButton(t, img); Creates button with both text and icon.
btn.addActionListener(lst); Action listener lst called when button clicked.
btn.setEnabled(bool); Used to enable/disable button.
JTextArea - Box containing multiple lines of text separated by '\n'.
ta = new JTextField(rows, cols); Creates textarea with specifed number of rows and columns.
s = ta.getText(); Returns string in textfield.
ta.setText(s); Sets text to s.
ta.append(s); Adds s to end of existing text.
ta.insert(s, pos); Inserts s at position pos.
ta.setEditable(bool); Don't allow user to edit textarea if used for output.
ta.setLineWrap(bool); Allow/disallow long lines to wrap.
ta.setWrapStyleWord(bool); Call setLineWrap(true) first. true wraps at word boundaries, false (default) at characters.
ta.setBorder(brdr); Add space between text and edge. Eg, to add 2 pixels use brdr BorderFactory.createEmptyBorder(2,2,2,2)
JCheckBox - Check box followed by text. Use with listener or to get/set state.
cb = new JCheckBox(text); Creates check box initially unchecked.
cb = new JCheckBox(text, bool); Creates check box with checked state tf.
b = cb.isSelected(); Returns bool if box is checked/unchecked.
cb.setSelected(bool); Sets checked state to tf.
ta.addItemListener(itemListen); Adds an item listener.
JList - List of string values. Put in JScrollPane if scrolling needed.
lst = new JList(); Creates empty JList.
lst = new JList(sarray); Creates JList intialized to String array sarray. Can also use array of Objects.
lst.setListData(sarray); Sets list "model" to String array sarray.
s = (String)lst.getSelectedValue(); Returns value as Object value. Typically cast to String. null if no selection.
i = lst.getSelectedIndex(); Returns index of selection or -1 if no selection.

Copyleft 2005 Fred Swartz
Ask programming questions?



Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]