It's all about Java: swing
Showing posts with label swing. Show all posts
Showing posts with label swing. Show all posts

Monday, 29 January 2018

Swing JLabel demo with example

This article is continuation of previous post JFrame usage demo. Please read it before continuing.

This post uses the same example code shown in JFrame usage demo with JLabel changes and introduces layout manager BorderLayout.

Layout Manager:

Layout manager is responsible to arrange components on containers. Usually every swing container is created with a default layout manager but can be changed with setLayout() api. For example JFrame uses BorderLayout as default layout amanger.

Java program for JLabel


2   import java.awt.BorderLayout;
3   import java.awt.Color;
4   import java.awt.Font;

6   import javax.swing.JFrame;
7   import javax.swing.JLabel;
8   import javax.swing.SwingUtilities;

10  /**
11   *
12   * Simple class to demonstrate JLabel of swing toolkit
13   *
14   * @author Nagasharath
15   *
16   */
17  public class LabelDemo extends JFrame {
18
19  private final String title = "It's all abt java!...  ";
20
21  private JLabel label;
22
23  /**
24  * set properties of the main window. Title, size of frame and position/location
25  */
26  public LabelDemo() {
27  this.setTitle(title);
28  this.setSize(300, 200);
29  this.setLocationRelativeTo(null);
30  initComponents();
31  }
32
33  /**
34  * instantiates controls controls.
35  *
36  */
37  private void initComponents() {
38  BorderLayout layout = new BorderLayout(20, 10);
39  this.setLayout(layout);
40
41  label = new JLabel("java-gui.blogspot.com");
42  label.setForeground(Color.RED);
43  label.setFont(new Font(Font.MONOSPACED, Font.BOLD, 25));
44  this.getContentPane().add(label, BorderLayout.CENTER);
45  }
46
47  public static void main(String[] args) {
48
49  Runnable r = () -> {
50  LabelDemo demo = new LabelDemo();
51  demo.setVisible(true);
52  };
53  SwingUtilities.invokeLater(r);
54  }
55  }
56


Line 21: Introduced new variable of type JLabel

Line 38 and Line 39: Used BorderLayout as layout manager to arrange label component on JFrame container

Line 41: Instantiated label with a text of String type.

Line 42 and Line 43:  label properties like foreground color, font style, size, type and background etc are specified

Line 44:  Finally added this label to the JFrame using add().

Output:


JFrme with JLabel

Swing: JFrame usage demo

This post helps in understanding usage of JFrame class with an example


Swing toolkit categorizes components into 2 types. They are:

  • Containers
  • Controls

Containers allows Controls to be arranged on them. JPanel, JFrame and JDialog are few which are frequently used.

Controls are components like Buttons, labels, tables etc. These controls are arranged on containers using different layout managers.

All Swing class names starts with J stands for Java which symbolizes swing is pure java! 

Layout Managers:

Layout manager is responsible for laying out controls on containers according to the business requirement. Example layout managers are BorderLayout, GridLayout, GridBagLayout and FlowLayout.

Few points about JFrame:
  • JFrame is considered as a main window of the application.for almost all swing based applications
  • All other controls and containers are created as child components of JFrame
  • JFrama holds special components like  Menu bar, Tool bar etc  

Example program

1 import javax.swing.JFrame;
2 import javax.swing.SwingUtilities;
3
4 /**
5  *
6  * Simple class to demonstrate JFrame container of swing toolkit
7  *
8  * @author Nagasharath
9  *
10 */
11public class FrameDemo extends JFrame {
12
13 private final String title = "It's all abt java!...  ";
14
15 /**
16 * set properties of the main window. Title, size of frame and position/location
17 */
18 public FrameDemo() {
19 this.setTitle(title);
20 this.setSize(300, 200);
21 this.setLocationRelativeTo(null);
22 initComponents();
23 }
24
25 /**
26 * It is used in future articles for instantiating controls like buttons etc.
27 * left empty for now.
28 */
29 private void initComponents() {
30 }
31
32 public static void main(String[] args) {
33
34 Runnable r = () -> {
35 FrameDemo demo = new FrameDemo();
36 demo.setVisible(true);
37 };
38 SwingUtilities.invokeLater(r);
39 }
40}
41


Line 1: Our class FrameDemo extends JFrame hence gets all benefits that JFrame possess    

Line 20: Sets the width 300 and height 200. How ever frame is re sizable at run time

Line 21:  Puts the frame at the center of the desktop monitor

Line 36: Unless call to setVisible(true), frame can not be seen at all.

Line 38: It is always good practice to call setVisible(true) in invokeLater().

Output on Win 10:

The look and feel we see below  is default which is same on all platforms 

This Look and feel can be changed using UIManager class. We see it in coming posts.  

      
JFrame with title


Sunday, 28 January 2018

Java desktop GUI.

Graphical user interface in short GUI makes computer users easy to understand and use the application software comfortably. Almost all modern programming languages allows application developers to develop GUIs.

Java has AWT/Swing as default API as part of Java Foundation classes in short JFC which are shipped with JDK. Hence these toolkits can be used directly in our applications with out adding external libraries like jars to our application's class path. But there are some other toolkits which found useful and industry endorsed in developing GUI.

    •      SWT 
    •      JFaces [Framework for SWT]
    •      Java FX
    •      Swing
    •      AWT

AWT:

Abstract window toolkit is available since java first version. AWT uses native operating system resources like Fonts, colors and components like table, tree, dialogues, windows etc.

Few notable points about AWT:
  • AWT is heavy weight. It uses OS resources
  • There are limited components in AWT precisely only components which are available in native OS. 
  • We can not have a new component according to our business logic need. ex: Tri state checkbox or treetable  
  • AWT behaves differently on different platforms
  • Hence it violates the java Platform independence rule
  • The program GUI developed with AWT looks differently on different OSs. Means on linux it renders linux graphics look and feel and on windows platform it renders windows graphics look and feel.   
  • Provides event model API which also used by Swing toolkit

Swing:

  • Swing was designed and developed keeping all cons in mind that are caused due to using AWT toolkit.
  • First of all "Swing is pure java hence it is platform independent." This statement made many good things possible:
Few notable points about Swing:
  •  Swing is light weight. It does not depend on native OS resources 
  •  Unlike AWT, it is extensible. New custom components can be developed according to program  requirement. 
   NOTE: See swingx library in it's official site.
  •  Swing has Pluggable look-and-feel support in short PLAF.
  •  Drag and Drop api, 2D api, internationalization are few among other included in Swing toolkit.
  •  Netbeans IDE itself is a good example for swing based application used allot now a days.
  •  Few GUI experts noticed that Swing is slow and not quite professional for commercial applications

SWT:

Standard Widget toolkit in short SWT is another toolkit for developing Java Desktop GUI based applications.
Compared to Swing it is faster and consistent in performance and Eclipse IDE itself is an example for SWT based applications.
Eclipse is industry endorsed IDE specifically for java based software development.
This is not shipped with JDK. Please see eclipse.org [official site] for more details.
And there are Integrated development environments in short IDE which provides Drag and Drop [DAD] palettes also known as GUI Builders for above toolkits for programmer's convenience.
Mostly used IDEs are:
It is advised to code manually to develop the GUI rather using GUI builders provided by different IDEs. And for beginners.  


Saturday, 16 July 2011

Romain guy's blog

If you are interested in designing GUIs and building custom components with Swing toolkit, definitely Romain guy's blog can be good reference for you, he writes about his work on swing components, custom components, tips and tricks on designing and using components. and you can download the demos of his work.


You find more swing articles in 2005, 2006 archives.
His URL: http://www.curious-creature.org/

Popular posts

Atomicity with Java Programming Language

 Atomicity with Java What is Atomicity Atomicity, in computer science, is considered to be a property [ALL-OR-NOTHING], that the state of a ...