Adv Java - [Swing: JButton Class]

♠ Posted by Unknown in

Swing Button Classes


Swing buttons provide features that are not found in the Button class defined by the AWT. For example, you can associate an icon with a Swing button. Swing buttons are subclasses of the AbstractButton class, which extends JComponent. AbstractButton contains many methods that allow you to control the behavior of buttons, check boxes, and radio buttons.

Adv Java - [Swing JTextField Class]

♠ Posted by Unknown in

JTextField Class


The swing text field is encapsulated by the JTextComponent class, which extends JComponent. It provides functionality that is common to Swing text components. One of its subclass is JTextField, which allows you to edit one line of text.

Adv Java - [Swing: ImageIcon/JLabel Classes]

♠ Posted by Unknown in

ImageIcon Class


In Swing, icons are encapsulated by the ImageIcon class, which paints an icon from an image. Two of lists constructors are show here:

     ImageIcon(String filename)
     ImageIcon(URL url)

The first form used the image in the file named “filename”. The second form used the image in the resource identified by “url”.

Adv Java - [Swing Layout Manager]

♠ Posted by Unknown in

Layout Managers


The way that you place components on a form in Java is probably different from any other GUI system you’ve used. The way components are placed on a form is controlled not by absolute positioning but by a “Layout Manager” that decides how the components lie based on the order that you add() them. The size, shape, and placement of components will be remarkably different from one layout manager to another. In addition, the layout managers adapt to the dimensions of your applet or application window, so if the window dimension is changed, the size, shape, and placement of the components can change in response.

Adv Java - [Swing Fundamental]

♠ Posted by Unknown in

swing


Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar components, such as buttons, check boxes, and labels, Swings supplies several exciting addition, including tabbed panes, scroll panes, trees, and tables. Even familiar components such as buttons have more capabilities in swing. For example, a button may have both an image and a text string associated with it. Also, the image can be changed as the state of the button changes.

Core Java - [Java File Handling]

♠ Posted by Unknown in

Java Stream Classes


The java.io package a large number of stream classes that provide capabilities for processing all types of data. These classes may be categorized into two groups on the data type on which they operate.

Core Java - [AWT GridLayout [Frame] Example]

♠ Posted by Unknown in
GridLayout  Manager on Frame

Example:


import java.awt.*;
import java.awt.event.*;

class FindHighestExample extends Frame implements ActionListener
{
    Label lblFirst, lblSecond, lblThird, lblAnswer;
    TextField tfFirst, tfSecond, tfThird;
    Button btnCheck, btnClear, btnExit;
    Panel p1, p2;

    public FindHighestExample()
    {
        lblFirst = new Label("Enter First Value : ");
        lblSecond = new Label("Enter Second Value : ");
        lblThird = new Label("Enter Third Value : ");
        lblAnswer = new Label("The Highest : ");

        tfFirst = new TextField("0");
        tfSecond = new TextField("0");
        tfThird = new TextField("0");

        btnCheck = new Button("Check");
        btnClear = new Button("Clear");
        btnExit = new Button("Exit");

        setLayout(new FlowLayout());
        p1 = new Panel();
        p1.setLayout(new GridLayout(4,2,5,10));

        p2 = new Panel();
        p2.setLayout(new FlowLayout(FlowLayout.CENTER,5,10));

        add(p1);
        add(p2);

        p1.add(lblFirst);
        p1.add(tfFirst);
        p1.add(lblSecond);
        p1.add(tfSecond);
        p1.add(lblThird);
        p1.add(tfThird);
        p1.add(lblAnswer);

        p2.add(btnCheck);
        btnCheck.addActionListener(this);
        p2.add(btnClear);
        btnClear.addActionListener(this);
        p2.add(btnExit);
        btnExit.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == btnCheck)
        {
                int a, b, c;
                a = Integer.parseInt(tfFirst.getText());
                b = Integer.parseInt(tfSecond.getText());
                c = Integer.parseInt(tfThird.getText());

                if((a>b) && (a>c))
                {
                        lblAnswer.setText("The Highest : " + a);
                }
                else if(b>c)
                {
                        lblAnswer.setText("The Highest : " + b);
                }
                else
                {
                        lblAnswer.setText("The Highest : " + c);
                }
        }
        else if(ae.getSource() == btnClear)
        {
                tfFirst.setText("0");
                tfSecond.setText("0");
                tfThird.setText("0");

        }
        else
        {
                dispose();
                System.exit(0);
        }
    }

    public static void main(String[] args) 
    {
        FindHighestExample f = new FindHighestExample();
        f.setVisible(true);
        f.setSize(400,200);
    }

}


Core Java - [AWT CardLayout Manager]

♠ Posted by Unknown in

CardLayout

The CardLayout manager arranges components in a queue of cards. You can only see one card at a time. To construct a CardLayout manager, simply use the constructor CardLayout().
Cards are usually placed in a container such as a panel. Components are placed into the card queue in the order in which they are added. To add a component in the CardLayout container, use the following method.
                 
                  void add(Component com, String name)

This add the specified component to this container at the specified index denoted by the specified string. The String argument of the method, name, gives an explicit identity to the component in the queue.

To make a component visible in the container with CardLayout, you can use the following instance methods in the CardLayout object:

public void first(container)
This method views the first card in the container.
public void last(container)
This method views the last card in the container.
public void next(container)
This method views the next card in the container.
public void previous(container)
This method views the previous card in the container.
public void show(container, String name)
This method views the component with the specified name in the container. You can  use this method to directly display the component.

Example:


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CardLayoutExample extends Applet implements ActionListener 
{
    TextField tfFirstValue, tfSecondValue, tfAnswer;
    Label lblFirstValue, lblSecondValue, lblAnswer;
    Button btnSum,btnPrevious,btnNext,btnSub;
    Panel p1;
    Panel p2;
    Panel CardPanel = new Panel();
    CardLayout queue = new CardLayout();
    int a, b, c, d;
    
    public void init()
    {
        lblFirstValue = new Label("Enter First value : ");
        lblSecondValue = new Label("Enter Second Value : ");
        lblAnswer = new Label("The Answer : ");

        tfFirstValue = new TextField();
        tfSecondValue = new TextField();
        tfAnswer = new TextField();

        btnSum = new Button("Sum");
        btnSub = new Button("Subtract");
        btnPrevious = new Button("Previous");
        btnNext = new Button("Next");

        CardPanel.setLayout(queue);
Image: Java CardLayout Example        p1 = new Panel(new GridLayout(3,2));
        p2 = new Panel(new FlowLayout());
        CardPanel.add(p1,"Previous");
        CardPanel.add(p2,"Next");

        p1.add(lblFirstValue);
        p1.add(tfFirstValue);
        p1.add(lblSecondValue);
        p1.add(tfSecondValue);
        p1.add(btnNext);

Image: Java CardLayout Example

        p2.add(lblAnswer);
        p2.add(tfAnswer);
        p2.add(btnPrevious);
        p2.add(btnSum);
        p2.add(btnSub);
        setLayout(new FlowLayout());
        add(CardPanel);
        btnSum.addActionListener(this);
        btnSub.addActionListener(this);
        btnPrevious.addActionListener(this);
        btnNext.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
        String btnName = ae.getActionCommand(); 
        if(btnName =="Next")
        {
                queue.next(CardPanel);
                a = Integer.parseInt(tfFirstValue.getText());
                b = Integer.parseInt(tfSecondValue.getText());
                c = a + b;
                d = a - b;
        }
        else if(btnName == "Sum")
        {

                tfAnswer.setText(Integer.toString(c));
        }
        else if(btnName == "Subtract")
        {
                tfAnswer.setText(Integer.toString(d));
        }
        else
        {
                queue.previous(CardPanel);
                tfFirstValue.setText("");
                tfSecondValue.setText("");
        }
    }
}

Core Java - [AWT GridBag Layout Manager]

♠ Posted by Unknown in

GridBagLayout


The GridBagLayout manager is the most flexible and most complex. The components can vary in size, however, and can be added in any order in GridBagLayout.

The Constructor GridBagLayout() is used to create a new GridBagLayout. 

In the GridLayout, the grid size (the number of rows and columns) is specified in the constructor. The size is unspecified, however, in the GridBagLayout. How a GridBagLayout places a set of components depends on each component's GridBagConstraints and minimum size, as well as the preferred size of the component's container.

gridx and gridy :
Specifies the cell at the upper left of the component's display area, where the upper left most cell has address gridx=0 and gridy=0.

numrows and numcols:
Specifies the number of cells ina row or column in the component's display area. the default value is 1.

weightx and weighty:
Specifies the extra space to allocate horizontally and vertically for the component in a row and a column, all the component clump together in the center of their container.

fill:
Specifies how the component should be resized if the component's viewing area is larger than its current size.

Valid Values are,
1. GridBagConstraints.NONE
2. GridBagConstraints.HORIZONTAL
3.GridBagConstraints.VERTICAL
4. GridBagConstraints.BOTH

anchor:
GridBagConstraint.CENTER (the default)
GridBagConstraint.NORTH
GridBagConstraint.NORTHEAST
GridBagConstraint.EAST
GridBagConstraint.SOUTHEAST
GridBagConstraint.SOUTH
GridBagConstraint.SOUTHWEST
GridBagConstraint.WEST
GridBagConstraint.NORTHWEST

The fill and anchor parameters deal with how to fill and place the component when the viewing area is larger than the requested area.


Core Java - [AWT BorderLayout Manager]

♠ Posted by Unknown in

BorderLayout


The BorderLayout manager divides the window into five areas: East, South, West, North, and Center. Components are added to a BorderLayout by using add(String, Component). You can use one of the following two constructors to create a new BorderLayout.
                 
                  public BorderLayout(int hGap, int vGap)

This constructs a new BorderLayout with the specified horizontal and vertical gaps between the components.

                  public BorderLayout()

This constructs a new BorderLayout without horizontal or vertical gaps.

Example:

import java.awt.*;
import java.awt.event.*;

class ScrollMarquee extends Canvas
{
    private String message;
    private int x = 10; 
    private int y = 10;

    ScrollMarquee(String s)
    {
            message = s;
            repaint();
    }
Image: Java BorderLayout Example
    public void left()
    {
            if(x>10) x = x - 10;
            repaint();
    }

    public void right()
    {
            if(x<250) x = x + 10;
            repaint();
    }

    public void paint(Graphics g)
    {
            g.drawString(message, x, y);
    }
}

public class BorderLayoutExample extends Frame implements ActionListener 
{
    private Button btnLeft, btnRight, btnExit;
    private ScrollMarquee cObj;

    BorderLayoutExample()
    {
            btnLeft = new Button("Left");
            btnRight = new Button("Right");
            btnExit = new Button("Exit");

            cObj = new ScrollMarquee("My Canvas");
            cObj.setBackground(Color.yellow);
            cObj.setForeground(Color.black);

            Panel p = new Panel();

            p.setLayout(new FlowLayout());
            p.add(btnLeft);
            p.add(btnRight);
            p.add(btnExit);

            setLayout(new BorderLayout());
            add("Center",cObj);
            add("South", p);

            btnLeft.addActionListener(this);
            btnRight.addActionListener(this);
            btnExit.addActionListener(this);

    }

    public void actionPerformed(ActionEvent ae)
    {
            if(ae.getSource() == btnLeft)
            {
                    cObj.left();
            }
            else if(ae.getSource() == btnRight)
            {
                    cObj.right();
            }
            else if(ae.getSource() == btnExit)
            {
                    dispose();
                    System.exit(0);
            }
    }
    public static void main(String[] args) 
    {
            BorderLayoutExample f = new BorderLayoutExample();
            f.setVisible(true);
            f.setSize(300,200);
    }
}


Core Java - [AWT GridLayout Manager]

♠ Posted by Unknown in

GridLayout


The GridLayout manager arranges components in a grid(matrix) formation with the number of rows and columns defined by the constructor. The components are placed in the grid from left to right starting with the first row, then the second, and so on, in the order in which they are added.

The GridLayout manager has two constructors:
                 
                  public GridLayout(int rows, int columns)

This constructs a new GridLayout with the specified number of rows and columns.
                 
                  public GridLayout(int rows, int columns, int hGap, intvGap)

This constructs a new GridLayout with the specified number of rows and columns, along with
specified horizontal and vertical gaps between components.

Example:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class GridLayoutExample extends Applet implements ActionListener 
{
    TextField tfFirstValue, tfSecondValue, tfAnswer;
    Label lblFirstValue, lblSecondValue, lblAnswer;
    Button btnSum;
    Panel p1;
    Panel p2;
    public void init()
    {
            lblFirstValue = new Label("Enter First value : ");
            lblSecondValue = new Label("Enter Second Value : ");
            lblAnswer = new Label("The Answer : ");
            tfFirstValue = new TextField();
            tfSecondValue = new TextField();
            tfAnswer = new TextField();
            btnSum = new Button("Sum");
            p1 = new Panel(new GridLayout(3,2));
            p2 = new Panel(new GridLayout(3,1));
Image: Java GridLayout Example            add(p1);
            add(p2);
            p1.add(lblFirstValue);
            p1.add(tfFirstValue);
            p1.add(lblSecondValue);
            p1.add(tfSecondValue);
            p1.add(lblAnswer);
            p1.add(tfAnswer);
            p2.add(btnSum);
            btnSum.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
            int a = Integer.parseInt(tfFirstValue.getText());
            int b = Integer.parseInt(tfSecondValue.getText());
            int c = a + b;
            tfAnswer.setText(Integer.toString(c));
    }
}


Core Java - [AWT FlowLayout Manager]

♠ Posted by Unknown in

FlowLayout


FlowLayout is the simplest layout manager. The components are arranged in the
container from left to right in the order in which they were added. When one row
becomes filled, a new row is started. You can specify the way the components are
aligned by using one of three constants: 

                  FlowLayout.RIGHT
                  FlowLayout.CENTER
                  FlowLayout.LEFT

you can also specify the gap between components in pixels. FlowLayout has the
following three constructors.
                 
                  public FlowLayout(int align, int hGap, int vGap)

This is constructs a new FlowLayout with a specified alignment, horizontal gap, and vertical gap. The gaps are the distances in pixels between components.

                  public FlowLayout(int alignment)

This constructs a new FlowLayout with a specified alignment and default gap of 5 pixels for both horizontal and vertical.

                  public FlowLayout()

This constructs a new FlowLayout with a default center alignment and default gap of five pixels for both horizontal and vertical.


Example:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class FlowLayoutExample extends Applet implements ActionListener 
{
    TextField tfFirstValue, tfSecondValue, tfAnswer;
    Label lblFirstValue, lblSecondValue, lblAnswer;
    Button btnSum;
    Panel p1;
    Panel p2;
    
    public void init()
    {
            lblFirstValue = new Label("Enter First value : ");
            lblSecondValue = new Label("Enter Second Value : ");
            lblAnswer = new Label("The Answer : ");
            tfFirstValue = new TextField();
            tfSecondValue = new TextField();
            tfAnswer = new TextField();
            btnSum = new Button("Sum");
            p1 = new Panel(new FlowLayout(FlowLayout.CENTER,10,20));
            p2 = new Panel(new FlowLayout(FlowLayout.CENTER,10,20));
            add(p1);
            add(p2);
            p1.add(lblFirstValue);
            p1.add(tfFirstValue);
            p1.add(lblSecondValue);
            p1.add(tfSecondValue);
            p1.add(lblAnswer);
            p1.add(tfAnswer);
            p2.add(btnSum);
            btnSum.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
            int a = Integer.parseInt(tfFirstValue.getText());
            int b = Integer.parseInt(tfSecondValue.getText());
            int c = a + b;
            tfAnswer.setText(Integer.toString(c));
    }
}

Core Java - [Java LayoutManagers]

♠ Posted by Unknown in

Java Layout Managers


The AWT components are placed in containers. Each container has a layout manager to arrange the AWT components within the container. Java knows where to place button because the layout manager works behind the scenes to place the components in the correct locations. The AWT provides five layout managers: 
  1. FlowLayout 
  2. GridLayout
  3. GridBagLayout
  4. BorderLayout
  5. CardLayout 

These classes implement the LayoutManager interface.

The layout managers are defined by implementing the LayoutManager interface. The LayoutManager interface defines the common methods that each layout manager uses to arrange components. The common methods are add() and remove(). Use the add() method to add a component to the container and the remove() method to remove a component from the container.

Core Java - [Java Menu]

♠ Posted by Unknown in

Create Menu in Java

Menu makes selection easier, and are widely used in window applications. In Java, menus can only appear on a frame. Java provides three classes - MenBar, Menu, and MenuItem - to implements menus in frame.

A frame can hold a menu bar to which the pull-down menus are attached. Menu Consist of menu items that the user can select.

1. To create a Menu bar and associate it with frame.

Frame f = new Frame();
f.setsize(300,300);
f.setVisible(true);

MenuBar mb = new MenuBar();
f.setMenuBar(mb);


2. To Create Menus.
           
             public Menu(String label, boolean tearOff);

In above constructor the "true" tearOff enables the programmer to create a menu that desplays even when the mouse button is released.

            public Menu(String label);

This constructs a new menu instance with the specified label.

            Menu FileMenu = new Menu("File", true);
            mb.add(FileMenu);

3. To Create Menu items:

            FileMenu.add(new MenuItem("New");
            FileMenu.add(new MenuItem("Open");
            FileMenu.add(new MenuItem("-"); To add separator.


Example:

import java.awt.*;
import java.awt.event.*;

public class MyMenuExample extends Frame implements ActionListener
{
            MenuBar mb;
            Menu FileMenu, EditMenu;
           
            public MyMenuExample()
            {
                        mb = new MenuBar();
                        setMenuBar(mb);
                       
                        FileMenu = new Menu("File");
                        EditMenu = new Menu("Edit");
                       
                        mb.add(FileMenu);
                        mb.add(EditMenu);
                       
                        FileMenu.add(new MenuItem("New"));
                        FileMenu.add(new MenuItem("Save"));
                        FileMenu.add(new MenuItem("-"));
                       
                        MenuItem CloseMenuItem = new MenuItem("Close");
                        FileMenu.add(CloseMenuItem);
                        CloseMenuItem.addActionListener(this);
                       
                        EditMenu.add(new MenuItem("Copy"));
                        EditMenu.add(new MenuItem("Paste"));
                       
            }
            public void actionPerformed(ActionEvent ae)
            {
                        dispose();
                        System.exit(0);
            }
           
            public static void main(String[] args)
            {
                        MyMenuExample f = new MyMenuExample();
                        f.setVisible(true);
                        f.setSize(300, 300);
            }
}