Layout of a complicated GUI interface with GridLayout #Programming Code Examples #Java/J2EE/J2ME #AWT Components

##################################
GridBagTest.java Layout of a complicated GUI interface with GridLayout. Uses WindowUtilities.java and ExitListener.java. 
##################################
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

/** An example demonstrating a GridBagLayout GUI with
 *  input text area and multiple buttons.
 *
 *********

public class GridBagTest extends JPanel {
  private JTextArea textArea;
  private JButton bSaveAs, bOk, bExit;
  private JTextField fileField;
  private GridBagConstraints c;

  public GridBagTest() {
    setLayout(new GridBagLayout());
    setBorder(BorderFactory.createEtchedBorder());

    textArea = new JTextArea(12,40);  // 12 rows, 40 cols
    bSaveAs = new JButton("Save As");
    fileField = new JTextField("C:Document.txt");
    bOk = new JButton("OK");
    bExit = new JButton("Exit");

    c = new GridBagConstraints();

    // Text Area.
    c.gridx      = 0;
    c.gridy      = 0;
    c.gridwidth  = GridBagConstraints.REMAINDER;
    c.gridheight = 1;
    c.weightx    = 1.0;
    c.weighty    = 1.0;
    c.fill       = GridBagConstraints.BOTH;
    c.insets     = new Insets(2,2,2,2); //t,l,b,r
    add(textArea,c);

    // Save As Button.
    c.gridx      = 0;
    c.gridy      = 1;
    c.gridwidth  = 1;
    c.gridheight = 1;
    c.weightx    = 0.0;
    c.weighty    = 0.0;
    c.fill       = GridBagConstraints.VERTICAL;
    add(bSaveAs,c);

    // Filename Input (Textfield).
    c.gridx      = 1;
    c.gridwidth  = GridBagConstraints.REMAINDER;
    c.gridheight = 1;
    c.weightx    = 1.0;
    c.weighty    = 0.0;
    c.fill       = GridBagConstraints.BOTH;
    add(fileField,c);

    // OK Button.
    c.gridx      = 2;
    c.gridy++;
    c.gridwidth  = 1;
    c.gridheight = 1;
    c.weightx    = 0.0;
    c.weighty    = 0.0;
    c.fill       = GridBagConstraints.NONE;
    add(bOk,c);

    // Exit Button.
    c.gridx      = 3;
    c.gridwidth  = 1;
    c.gridheight = 1;
    c.weightx    = 0.0;
    c.weighty    = 0.0;
    c.fill       = GridBagConstraints.NONE;
    add(bExit,c);

    // Filler so Column 1 has nonzero width.
    Component filler = Box.createRigidArea(new Dimension(1,1));
    c.gridx      = 1;
    c.weightx    = 1.0;
    add(filler,c);
  }

  public static void main(String[] args) {
    WindowUtilities.setNativeLookAndFeel();
    JFrame frame = new JFrame("GrigBagLayout Test");
    frame.setContentPane(new GridBagTest());
    frame.addWindowListener(new ExitListener());
    frame.pack();
    frame.setVisible(true);
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10349
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

ReverseLabels.java Inherits from CloseableFrame.java and uses ReversibleLabel.java. #Programming Code Examples #Java/J2EE/J2ME #AWT Components

ReverseLabels.java Inherits from CloseableFrame.java and uses ReversibleLabel.java. 
**********************
ReverseLabels.java 
**********************
import java.awt.*;

******************

public class ReverseLabels extends CloseableFrame {
  public static void main(String[] args) {
    new ReverseLabels();
  }

  public ReverseLabels() {
    super("Reversible Labels");
    setLayout(new FlowLayout());
    setBackground(Color.lightGray);
    setFont(new Font("Serif", Font.BOLD, 18));
    ReversibleLabel label1 =
      new ReversibleLabel("Black on White",
                          Color.white, Color.black);
    add(label1);
    ReversibleLabel label2 =
      new ReversibleLabel("White on Black",
                          Color.black, Color.white);
    add(label2);
    pack();
    setVisible(true);
  }
}
/./././././././././.
CloseableFrame.java 
&&&&&&&&&&&&&&&&&&&
import java.awt.*;
import java.awt.event.*;

/** A Frame that you can actually quit. Used as the starting 
 *  point for most Java 1.1 graphical applications.
 *
 *******************
 */

public class CloseableFrame extends Frame {
  public CloseableFrame(String title) {
    super(title);
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  }

  /** Since we are doing something permanent, we need
   *  to call super.processWindowEvent first.
   */
  
  public void processWindowEvent(WindowEvent event) {
    super.processWindowEvent(event); // Handle listeners.
    if (event.getID() == WindowEvent.WINDOW_CLOSING) {
      // If the frame is used in an applet, use dispose().
      System.exit(0);
    }
  }
}
&&&&&&&&&&&&&&&&&&&&
ReversibleLabel.java. 
&&&&&&&&&&&&&&&&&&&&
import java.awt.*;
import java.awt.event.*;

/** A Label that reverses its background and
 *  foreground colors when the mouse is over it.
 *
 **********************
 
public class ReversibleLabel extends Label {
  public ReversibleLabel(String text,
                         Color bgColor, Color fgColor) {
    super(text);
    MouseAdapter reverser = new MouseAdapter() {
      public void mouseEntered(MouseEvent event) {
        reverseColors();
      }

      public void mouseExited(MouseEvent event) {
        reverseColors(); // or mouseEntered(event);
      }
    };
    addMouseListener(reverser);
    setText(text);
    setBackground(bgColor);
    setForeground(fgColor);
  }

  protected void reverseColors() {
    Color fg = getForeground();
    Color bg = getBackground();
    setForeground(bg);
    setBackground(fg);
  }
}
#############################

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10343
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

TextAreas #Programming Code Examples #Java/J2EE/J2ME #AWT Components

TextAreas.java
**************
import java.applet.Applet;
import java.awt.*;

/././././././,/././././
public class TextAreas extends Applet {
  public void init() {
    setBackground(Color.lightGray);
    add(new TextArea(3, 10));
    add(new TextArea("SomenInitialnText", 3, 10));
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10342
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

TextFields #Programming Code Examples #Java/J2EE/J2ME #AWT Components


import java.applet.Applet;
import java.awt.*;

/** A TextField from each of the four constructors.
 *
 *********************

public class TextFields extends Applet {
  public void init() {
    add(new TextField());
    add(new TextField(30));
    add(new TextField("Initial String"));
    add(new TextField("Initial", 30));
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10341
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Lists.java #Programming Code Examples #Java/J2EE/J2ME #AWT Components

Lists.java Inherits from CloseableFrame.java. 
/./././././././././
import java.awt.*;

/*****************/

public class Lists extends CloseableFrame {
  public static void main(String[] args) {
    new Lists();
  }

  public Lists() {
    super("Lists");
    setLayout(new FlowLayout());
    setBackground(Color.lightGray);
    setFont(new Font("SansSerif", Font.BOLD, 18));
    List list1 = new List(3, false);
    list1.add("Vanilla");
    list1.add("Chocolate");
    list1.add("Strawberry");
    add(list1);
    List list2 = new List(3, true);
    list2.add("Colored Sprinkles");
    list2.add("Cashews");
    list2.add("Kiwi");
    add(list2);
    pack();
    setVisible(true);
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10339
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

ChoiceTest2 #Programming Code Examples #Java/J2EE/J2ME #AWT Components

ChoiceTest2.java
/././././././././
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

/***********************/

public class ChoiceTest2 extends Applet 
                         implements ItemListener {
  private Choice choice;

  public void init() {
    setFont(new Font("SansSerif", Font.BOLD, 36));
    choice = new Choice();
    choice.addItem("Choice 1");
    choice.addItem("Choice 2");
    choice.addItem("Choice 3");
    choice.addItemListener(this);
    add(choice);
  }

  public void itemStateChanged(ItemEvent event) {
    Choice choice = (Choice)event.getSource();
    String selection = choice.getSelectedItem();
    if (selection.equals("Choice 1")) {
      doChoice1Action();
    } else if (selection.equals("Choice 2")) {
      doChoice2Action();
    } else if (selection.equals("Choice 3")) {
      doChoice3Action();
    }
  }

  private void doChoice1Action() {
    System.out.println("Choice 1 Action");
  }

  private void doChoice2Action() {
    System.out.println("Choice 2 Action");
  }

  private void doChoice3Action() {
    System.out.println("Choice 3 Action");
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10338
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

ChoiceTest #Programming Code Examples #Java/J2EE/J2ME #AWT Components

import java.applet.Applet;
import java.awt.*;

/*******/

public class ChoiceTest extends Applet {
  private Choice choice;

  public void init() {
    setFont(new Font("SansSerif", Font.BOLD, 36));
    choice = new Choice();
    choice.addItem("Choice 1");
    choice.addItem("Choice 2");
    choice.addItem("Choice 3");
    add(choice);
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10337
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

CheckboxGroups #Programming Code Examples #Java/J2EE/J2ME #AWT Components

CheckboxGroups.java
/./././././././././
/////////////////////
import java.applet.Applet;
import java.awt.*;

////////////////////
 
public class CheckboxGroups extends Applet {
  public void init() {
    setLayout(new GridLayout(4, 2));
    setBackground(Color.lightGray);
    setFont(new Font("Serif", Font.BOLD, 16));
    add(new Label("Flavor", Label.CENTER));
    add(new Label("Toppings", Label.CENTER));
    CheckboxGroup flavorGroup = new CheckboxGroup();
    add(new Checkbox("Vanilla", flavorGroup, true));
    add(new Checkbox("Colored Sprinkles"));
    add(new Checkbox("Chocolate", flavorGroup, false));
    add(new Checkbox("Cashews"));
    add(new Checkbox("Strawberry", flavorGroup, false));
    add(new Checkbox("Kiwi"));
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10336
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

ButtonExample.java Uses the following #Programming Code Examples #Java/J2EE/J2ME #AWT Components

/./././././././././
# ButtonExample.java Uses the following classes:

    * CloseableFrame.java
    * FgReporter.java
    * BgReporter.java
    * SizeReporter.java
******************
ButtonExample.java 
******************
import java.awt.*;
import java.awt.event.*;

/././././././././././

public class ButtonExample extends CloseableFrame {
  public static void main(String[] args) {
    new ButtonExample();
  }

  public ButtonExample() {
    super("Using ActionListeners");
    setLayout(new FlowLayout());
    Button b1 = new Button("Button 1");
    Button b2 = new Button("Button 2");
    Button b3 = new Button("Button 3");
    b1.setBackground(Color.lightGray);
    b2.setBackground(Color.gray);
    b3.setBackground(Color.darkGray);
    FgReporter fgReporter = new FgReporter();
    BgReporter bgReporter = new BgReporter();
    SizeReporter sizeReporter = new SizeReporter();
    b1.addActionListener(fgReporter);
    b2.addActionListener(fgReporter);
    b2.addActionListener(bgReporter);
    b3.addActionListener(fgReporter);
    b3.addActionListener(bgReporter);
    b3.addActionListener(sizeReporter);
    add(b1);
    add(b2);
    add(b3);
    setSize(350, 100);
    setVisible(true);
  }
}
/./././././././././
FgReporter.java
***************
import java.awt.event.*;
import java.awt.*;

/././././././././././

public class FgReporter implements ActionListener {
  public void actionPerformed(ActionEvent event) {
    Component c = (Component)event.getSource();
    System.out.println("Foreground: " + c.getForeground());
  }
}
****************
BgReporter.java
****************
/././././././././
import java.awt.event.*;
import java.awt.*;

/././././././././././././

public class BgReporter implements ActionListener {
  public void actionPerformed(ActionEvent event) {
    Component c = (Component)event.getSource();
    System.out.println("Background: " + c.getBackground());
  }
}
/././././././././././
SizeReporter.java
***********************

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

/././././././././

public class SizeReporter implements ActionListener {
  public void actionPerformed(ActionEvent event) {
    Component c = (Component)event.getSource();
    Dimension d = c.getSize();
    System.out.println("Size: " + d.width + "x" + d.height);
  }
}
********************

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10334
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Batton’s java #Programming Code Examples #Java/J2EE/J2ME #AWT Components

import java.applet.Applet;
import java.awt.*;

/././././././

public class Buttons extends Applet {
  private Button button1, button2, button3;
  
  public void init() {
    button1 = new Button("Button One");
    button2 = new Button("Button Two");
    button3 = new Button("Button Three");
    add(button1);
    add(button2);
    add(button3);
  }
}
/././././././././.

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10333
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada