Gui stuff in Java: Introduction
Page 1 of 1
Gui stuff in Java: Introduction
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]
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]
Similar topics
» Java, Introduction to SUPER and THIS
» Java Tutorial: Collection - Part 1 (Introduction)
» Java Tutorial: Using Two Java Reference Keywords, this and super
» Introduction
» Introduction
» Java Tutorial: Collection - Part 1 (Introduction)
» Java Tutorial: Using Two Java Reference Keywords, this and super
» Introduction
» Introduction
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford