JAVA Swing GUI EXAMPLES

JTextArea


import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class WhiteBoard extends JFrame implements ActionListener
{
private static int WIDTH = 550;
private static int HEIGHT = 350;

private int row = 10;
private int col = 20;

//GUI components
private JLabel headingL;
private JTextField lineTF;
private JTextArea whiteBoardTA;
private JButton exitB, appendB;

public WhiteBoard()
{
setTitle("White Board");
Container pane = getContentPane();
setSize(WIDTH,HEIGHT);

headingL = new JLabel("Welcome to White Board");
lineTF = new JTextField(20);

whiteBoardTA = new JTextArea(row,col);
exitB = new JButton("Exit");
exitB.addActionListener(this);

appendB = new JButton("Append");
appendB.addActionListener(this);

pane.setLayout(null);

headingL.setLocation(50,20);
lineTF.setLocation(20,100);
whiteBoardTA.setLocation(320,50);
appendB.setLocation(230,100);
exitB.setLocation(230,250);

headingL.setSize(200,30);
lineTF.setSize(200,30);
whiteBoardTA.setSize(200,200);
appendB.setSize(80,30);
exitB.setSize(80,30);

pane.add(headingL);
pane.add(lineTF);
pane.add(whiteBoardTA);
pane.add(appendB);
pane.add(exitB);

setVisible(true);

} //end of the constructor

public static void main(String[] args) throws IOException
{
WhiteBoard board = new WhiteBoard();
}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Append"))
whiteBoardTA.append(lineTF.getText());
else if(e.getActionCommand().equals("Exit"))
System.exit(0);
}
}

JCheckBox

 

//Welcome Applet with check boxes

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GrandWelcomeCheckBox extends JFrame implements
ItemListener
{
private int intBold = Font.PLAIN;
private int intItalic = Font.PLAIN;
private JCheckBox boldCB, italicCB;

GrandWelcomeCheckBox()
{
Container c = getContentPane();
c.setLayout(null);
boldCB = new JCheckBox( "Bold");
italicCB = new JCheckBox( "Italic");

boldCB.setSize(100,30);
italicCB.setSize(100,30);

boldCB.setLocation(100,100);
italicCB.setLocation(300,100);

boldCB.addItemListener(this);
italicCB.addItemListener(this);

c.add(boldCB);
c.add(italicCB);
}
public static void main(String [] args)
{GrandWelcomeCheckBox f = new GrandWelcomeCheckBox();
f.setSize(440,200);
f.setVisible(true);
}
public void paint( Graphics g)
{
super.paint(g);
g.setColor(Color.red);
g.setFont(new Font("Courier", intBold + intItalic, 24));
g.drawString("Welcome to Java Programming",30,80);
}

public void itemStateChanged(ItemEvent e)
{
if(e.getSource() == boldCB)

if(e.getStateChange() == ItemEvent.SELECTED)
intBold = Font.BOLD;
if(e.getStateChange() == ItemEvent.DESELECTED)
intBold = Font.PLAIN;
}

if(e.getSource() == italicCB)
{
if(e.getStateChange() == ItemEvent.SELECTED)
intItalic = Font.ITALIC;
if(e.getStateChange() == ItemEvent.DESELECTED)
intItalic = Font.PLAIN;
}

repaint();
}
}

JRadioButton

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GrandWelcomeRButton extends JFrame implements
ItemListener
{
private int intBold = Font.PLAIN;
private int intItalic = Font.PLAIN;
private Color currentColor = Color.black;
private JCheckBox boldCB, italicCB;
private JRadioButton redRB, greenRB, blueRB;
private ButtonGroup ColorSelectBGroup;

GrandWelcomeRButton()
{
Container c = getContentPane();
c.setLayout(null);
boldCB = new JCheckBox("Bold");
italicCB = new JCheckBox("Italic");
redRB = new JRadioButton("Red");
greenRB = new JRadioButton("Green");
blueRB = new JRadioButton("Blue");

boldCB.setSize(100,30);
italicCB.setSize(100,30);
redRB.setSize(100,30);
greenRB.setSize(100,30);
blueRB.setSize(100,30);

boldCB.setLocation(100,70);
italicCB.setLocation(100,150);
redRB.setLocation(300,70);
greenRB.setLocation(300,110);
blueRB.setLocation(300,150);

boldCB.addItemListener(this);
italicCB.addItemListener(this);
redRB.addItemListener(this);
greenRB.addItemListener(this);
blueRB.addItemListener(this);

c.add(boldCB);
c.add(italicCB);
c.add(redRB);
c.add(greenRB);
c.add(blueRB);

ColorSelectBGroup = new ButtonGroup();
ColorSelectBGroup.add(redRB);
ColorSelectBGroup.add(greenRB);
ColorSelectBGroup.add(blueRB);
}
public static void main(String [] args)
{GrandWelcomeRButton f = new GrandWelcomeRButton();
f.setSize(440,230);
f.setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.orange);
g.drawRoundRect(75,50,125,160,10,10);
g.drawRoundRect(275,50,125,160,10,10);
g.setColor(currentColor);
g.setFont(new Font("Courier", intBold + intItalic, 24));
g.drawString("Welcome to Java Programming",30,40);
}

public void itemStateChanged(ItemEvent e)
{
if(e.getSource() == boldCB)

if(e.getStateChange() == ItemEvent.SELECTED)
intBold = Font.BOLD;
if(e.getStateChange() == ItemEvent.DESELECTED)
intBold = Font.PLAIN;
}

if(e.getSource() == italicCB)
{
if(e.getStateChange() == ItemEvent.SELECTED)
intItalic = Font.ITALIC;
if(e.getStateChange() == ItemEvent.DESELECTED)
intItalic = Font.PLAIN;


if(e.getSource() == redRB)
currentColor = Color.red;
else if(e.getSource() == greenRB)
currentColor = Color.green;
else if(e.getSource() == blueRB)
currentColor = Color.blue;

repaint();
}
}

JComboBox

//Welcome program with check boxes, radio buttons, and combo box

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GrandWelcomeFinal extends JFrame implements
ItemListener
{
private int intBold = Font.PLAIN;
private int intItalic = Font.PLAIN;
private Color currentColor = Color.black;
private String currentFontName ="Courier";
private JCheckBox boldCB, italicCB;
private JRadioButton redRB, greenRB, blueRB;
private ButtonGroup ColorSelectBGroup;
private JComboBox fontFaceDD;
private String[] fontNames 
= {"Dialog","Century Gothic","Courier","Serif"};

GrandWelcomeFinal()
{
Container c = getContentPane();
c.setLayout(null);
boldCB = new JCheckBox("Bold");
italicCB = new JCheckBox("Italic");
redRB = new JRadioButton("Red");
greenRB = new JRadioButton("Green");
blueRB = new JRadioButton("Blue");
fontFaceDD = new JComboBox(fontNames);
fontFaceDD.setMaximumRowCount(3);

boldCB.setSize(80,30);
italicCB.setSize(80,30);
redRB.setSize(80,30);
greenRB.setSize(80,30);
blueRB.setSize(80,30);
fontFaceDD.setSize(80,30);

boldCB.setLocation(100,70);
italicCB.setLocation(100,150);
redRB.setLocation(300,70);
greenRB.setLocation(300,110);
blueRB.setLocation(300,150);
fontFaceDD.setLocation(200,70);

boldCB.addItemListener(this);
italicCB.addItemListener(this);
redRB.addItemListener(this);
greenRB.addItemListener(this);
blueRB.addItemListener(this);
fontFaceDD.addItemListener(this);

c.add(boldCB);
c.add(italicCB);
c.add(redRB);
c.add(greenRB);
c.add(blueRB);
c.add(fontFaceDD);
ColorSelectBGroup = new ButtonGroup();
ColorSelectBGroup.add(redRB);
ColorSelectBGroup.add(greenRB);
ColorSelectBGroup.add(blueRB);
}
public static void main(String [] args)
{GrandWelcomeFinal f = new GrandWelcomeFinal();
f.setSize(440,230);
f.setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.orange);
g.drawRoundRect(75,50,324,160,10,10);
g.drawLine(183,50,183,210);
g.drawLine(291,50,291,210);

g.setColor(currentColor);
g.setFont(new Font(currentFontName, intBold + intItalic, 24));
g.drawString("Welcome to Java Programming",30,40);
}

public void itemStateChanged(ItemEvent e)
{
if(e.getSource() == boldCB)

if(e.getStateChange() == ItemEvent.SELECTED)
intBold = Font.BOLD;
if(e.getStateChange() == ItemEvent.DESELECTED)
intBold = Font.PLAIN;
}

if(e.getSource() == italicCB)
{
if(e.getStateChange() == ItemEvent.SELECTED)
intItalic = Font.ITALIC;
if(e.getStateChange() == ItemEvent.DESELECTED)
intItalic = Font.PLAIN;


if(e.getSource() == redRB)
currentColor = Color.red;
else if(e.getSource() == greenRB)
currentColor = Color.green;
else if(e.getSource() == blueRB)
currentColor = Color.blue;

if(e.getSource() == fontFaceDD)
currentFontName = fontNames[fontFaceDD.getSelectedIndex()];

repaint();
}
}

JList    Click here to down load the zip file for the example below.

//Program to demonstrate JLIST

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class JListPictureViewer extends JFrame implements
ListSelectionListener
{
private String[] pictureNames = {"Pie Diagram",
"Line Graph",
"Bar Graph",
"Table",
"Normal Curve"};

private ImageIcon[] pictures =
{new ImageIcon("pieDiagram.jpg"),
new ImageIcon("lineGraph.jpg"),
new ImageIcon("barGraph.jpg"),
new ImageIcon("table.jpg"),
new ImageIcon("normalCurve.jpg")};

private BorderLayout layoutBL;
private JList pictureJList;
private JScrollPane selectionJS;
private JLabel promptJL;
private JLabel displayPicJL;
private JLabel infoJL;

public JListPictureViewer()
{
super("Photo Viewer");

Container pane = getContentPane();
pane.setLayout(null);

promptJL = new JLabel("Select an Image",
SwingConstants.CENTER);
promptJL.setSize(350,20);
promptJL.setLocation(10,0);
pane.add(promptJL);

pictureJList = new JList(pictureNames);
pictureJList.setVisibleRowCount(3);
pictureJList.setSelectionMode
(ListSelectionModel.SINGLE_SELECTION);
pictureJList.addListSelectionListener(this);

selectionJS = new JScrollPane(pictureJList);
selectionJS.setSize(350,60);
selectionJS.setLocation(10,20);
pane.add(selectionJS);

displayPicJL = new JLabel(pictures[4]);
displayPicJL.setSize(350,350);
displayPicJL.setLocation(10,50);

pane.add(displayPicJL);

infoJL = new JLabel(pictureNames[4],
SwingConstants.CENTER);
infoJL.setSize(350,20);
infoJL.setLocation(10,380);
pane.add(infoJL);

setSize (380, 440);
setVisible(true);
// setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String args[])
{
JListPictureViewer picViewer = new JListPictureViewer();
}

public void valueChanged(ListSelectionEvent e)
{
displayPicJL.setIcon(
pictures[pictureJList.getSelectedIndex()]);
infoJL.setText(
pictureNames[pictureJList.getSelectedIndex()]);
repaint();
}
}