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
 
 
Hot Web Programming Job

 

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

[an error occurred while processing this directive]

Java: Summary - Basic GUI Elements

String s; // Any string.
String t; // A string.  Interpreted as HTML if starts with "<html>" and ends with "</html>".
Font f;   // A Font object.
Icon img; // An image.
Container cont; // Container is the superclass of JPanel.

Top-level Containers (JFrame)

JFrame - window, typically subclassed
w = new JFrame(); Constructor
w.setTitle(t); Sets titlebar text to t
w.setDefaultCloseOperation(opt); Use opt JFrame.EXIT_ON_CLOSE to terminate program when close box clicked.
w.setResizable(false); Prevent user from resizing window.
w.show(); Use w.setVisible(true) instead.
w.hide(); Use w.setVisible(false) instead.
w.setVisible(true/false); Same as w.show() or w.hide()
w.setContentPane(cont); Sets the content pane - common to pass a JPanel here.
cont = w.getContentPane(); Returns the window's content pane.
w.setJMenuBar(mb); Adds JMenuBar (contains Jmenus, which contain JMenuItems) to window.

Low-level Containers (JPanel)

JPanel - Set layout and add components to JPanel.
p = new JPanel(); Creates new JPanel
p.setLayout(layout); Sets the panel's layout.
p.add(widget); Adds the widget to next position in Layout (eg, Flow, Grid, or Box layouts).
p.add(widget, constraint); Adds the widget to position defined by constraint (eg, Border or Gridbag layouts).

Components (JLabel, JTextField, JButton) and their common methods

cmp.requestFocus(); Puts focus (eg, blinking cursor) in the field, selection of a button, etc.
tf.setFont(f); Sets font.
JLabel - For fixed text.
lbl = new JLabel(t); Creates JLabel with text (can be HTML). Typically created in add() call.
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(true/false); 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(act); Creates button from Action act for text, icon, listener, etc.
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(true/false); Used to enable/disable button.

Layouts (FlowLayout, BorderLayout, GridLayout, BoxLayout)

FlowLayout - Arranges widgets left-to-right, top-to-bottom.
p.setLayout(new FlowLayout()); Sets layout of the panel to FlowLayout.
BorderLayout - Lays out components in BorderLayout.NORTH, EAST, SOUTH, WEST, and CENTER sections.
p.setLayout(new BorderLayout()); Sets layout of the panel to BorderLayout. Widgets added with constraint.
GridLayout - Lays out components in equal sized rectangular grid, added r-t-l, top-to-bottom.
p.setLayout(new GridLayout(r, c)); Sets layout to GridLayout with specified rows and columns.
p.setLayout(new GridLayout(r, c, h, v)); As above but also specifies horizontal and vertical space between cells.
BoxLayout - Lays out components in a single row or column. Can add "glue" and rigid areas to control spacing.
p.setLayout(new BoxLayout(p, dir)); Layout is horizontal (dir is BoxLayout.X_AXIS) or vertical (BoxLayout.X_AXIS).

Copyleft 2004 Fred Swartz

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

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
Search Tutorials

 

 
 
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.