IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Gui stuff in Java: Introduction

Go down

Gui stuff in Java: Introduction  Empty Gui stuff in Java: Introduction

Post by BIT0112-Rokon Tue Jul 27, 2010 8:15 am

Some of us very much interested about GUI stuff of Java. Java is very nice programming language for making GUI. But you can find more easy solution in .NET (dot)Net framework. Anyways, we love Java as it is robust and platform independent. In this text I try to give you a basic introduction about Gui stuff in Java.
Oh yes, before I start, Im gonna quote from Albert Einstein that every Java programmer should keep in mind.

"Why should I memorize something when I know where to find it?"
"Know where to find the information and how to use it - that's the secret of success"

To make a gui in java, you have to know about some basic element of Java. They are -

1. Top- Level Container
2. Intermediate Container
3. Components
4. Listener
5. Layouts

What is a container actually?
A container is basically a component which can contain other components inside itself. More generally Container is something that holds something inside itself.
Now let us see a summary of containers .

Top-level Container: javax.swing.JFrame
JFrame - window, typically subclassed
w= new JFrame(); //Constructor
w= new JFrame(t); //Constructor. Sets titlebar to t.

w.setTitle(t); //Sets titlebar text to t

w.setDefaultCloseOperation(opt);
JFrame.EXIT_ON_CLOSE  //terminates program when close box clicked.

w.setVisible(true/false); //Make visible (and start GUI thread) or hide.

w.pack(); Calculates layout on all inner containers and sets size of JFrame.

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 a JMenuBar.

w.setResizable(false); //Prevent user from resizing window.

w.setLocation(x,y); //Positions window's top left corner at screen coordinates (x, y).

w.setSize(w, h); //Sets window size, but use layouts and pack() instead.

w.show(); //Deprecated. Use w.setVisible(true).

w.hide(); //Deprecated. Use w.setVisible(false).

Top-level Containers: Dialogs: JOptionPane, JFileChooser, JColorChooser, JDialog

JOptionPane - Commonly used to create dialogs.
Low-level Container supporting layouts - 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); //Add widget to next position - FlowLayout, GridLayout, BoxLayout.

p.add(widget, constraint); //Add widget at position constraint = BorderLayout, GridbagLayout).

Low-level Containers - special purpose - JSrollPane, JTabbedPane
JScrollPane - Holds textarea, list, (or large images in label) and adds scroll bars as necessary.
scr= new JScrollPane(textarea); //Surrounds textarea with scrollbars as needed.
JTabbedPane - Display tabs to allow user to select one of many panels.
tp =  new JTabbledPane(); //Constructor. Creates new tabbed pane.
tp =  new JTabbledPane(place); //Constructor. placement is JTabbedPane.TOP (default), BOTTOM, LEFT, or RIGHT.
tp =  new JTabbledPane(place, ovrfl); //Constructor. ovrfl tells what to do if too many tabs: JTabbedPane.WRAP_TAB_LAYOUT or .SCROLL_TAB_LAYOUT.

tp.addTab(title, comp); //Adds tab labelled with string title which displays comp (usually JPanel).

tp.addTab(title, icon, comp);
Used string and icon for tab label.
Recommended reading : http://leepoint.net/notes-java/index.html
it is a nice place to learn about Java.

Now let us know about different GUI components.
There are lots of gui components are available in java like JButton, JTextField etc better lets see a summary of it.


Components share many common methods, for example:

cmp.requestFocus();
Puts focus (eg, blinking cursor) in field, select button, etc.

cmp.setFont(f);
Sets font.
JLabel - For fixed text.

lbl =  new JLabel(t)
Creates JLabel with text t on it.

lbl =  new JLabel(t, align)
Align text JLabel.LEFT, JLabel.CENTER, or JLabel.RIGHT.

lbl =  new JLabel(icon)
Displays icon (eg, ImageIcon).

lbl =  new JLabel(icon, align)
Align icon to JLabel.LEFT, JLabel.CENTER, or JLabel.RIGHT.
JTextField - Box containing one line of text.

tf =  new JTextField(n);
Creates textfield n characters 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, orJTextField.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(actlstnr);
Action listener actlstnr called when button clicked.


btn.setEnabled(bool);
Used to enable/disable button.
JTextArea - Box containing multiple lines of text separated by '\n'.

ta =  new JTextArea(rows, cols);
Creates textarea with specifed number of rows and columns.

s =  ta.getText();
Returns string in text area.

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 4 pixels use brdrBorderFactory.createEmptyBorder(4,4,4,4)
JCheckBox - Check box followed by text. Use either listener or check with isSelected().

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.

cb.addActionListener(actlstnr);
Adds ActionListener, which is called whenever user clicks.


cb.setEnabled(bool);
Used to enable/disable check box.

Now lets know about listener.
A listener in Java is an object that is used to handle events. In effect, it is implemented to listen for events and then tell the program the required information about that event, which you may then handle. For example, if the player moves the mouse, a listener will alert the program that the mouse has been moved and give details of its position, relative to the component currently occupying that area of the screen.
There is various listener available in java . There is thousands of tutorial available over INTERNET. Better I recommend you some of them.

Recommended reading :
[http://www.javaworld.com/javaqa/2000-08/01-qa-0804-events.html]
[http://www.javapractices.com/topic/TopicAction.do?Id=156]
[http://leepoint.net/notes-java/GUI/events/15listeners.html]

Layouts:
To manage gui stuff, layout is very much important issue. Layout is something that ensures the orientation of different gui stuff, in which place they took to be placed.

Recommended Reading:
[http://mindprod.com/jgloss/layout.html]
[http://leepoint.net/notes-java/GUI/layouts/05layouts.html]

Now go to the links given below to see some example-

[https://iitdu.forumotion.com/java-tutorials-f37/jframe-utilites-t704.htm]
[https://iitdu.forumotion.com/java-tutorials-f37/simple-calculator-in-java-t686.htm]
[https://iitdu.forumotion.com/java-tutorials-f37/how-to-create-a-frame-in-java-t684.htm]
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum