Beruflich Dokumente
Kultur Dokumente
Major Components
1. JFrame
2. JButton
Object
3. JOptionPane
Component
4. JDialog
5. JPanel Container
6. JComboBox
Window
7. JScrollPane
8. JTabbedPane Frame
9. Jtable JFrame
10.JTree
Fig: JFrame Hierarchy 7
1. JFrame
JFrame is a predefined class
JFrame is a Container under swing package
JFrame is used to set the title and border of a
frame.
Syntax:
JFrame = new JFrame();
8
//Demo for Frames
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameTester
{ public static void main (String args[ ])
{ JFrame f = new JFrame ("JFrame Example");
Container c = f.getContentPane();
c.setLayout (new FlowLayout());
for (int i = 0; i < 5; i++)
{
c.add (new JButton ("No"));
c.add (new Button ("Batter"));
}
c.add (new JLabel ("Swing"));
f.setSize (300, 200);
f.show();
}
}
9
2. JButton
JButton is a predefined class
JButton is a Container under swing package
JButton is used to simulate the appearance of a push
button in a frame.
Syntax:
JButton = new JButton();
10
//Demo for JButton is a predefined class under swing package
import java.awt.*;
import javax.swing.*;
class BorderTest
{ public static void main(String[ ] args)
{ JFrame window = new BorderTestGUI();
window.setTitle("BorderTest");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
class BorderTestGUI extends JFrame
{ BorderTestGUI() { JButton north = new JButton("North");
JButton east = new JButton("East");
JButton south = new JButton("South");
JButton west = new JButton("West");
JButton center = new JButton("Center");
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(north , BorderLayout.NORTH);
content.add(east , BorderLayout.EAST);
content.add(south , BorderLayout.SOUTH);
content.add(west , BorderLayout.WEST);
content.add(center, BorderLayout.CENTER);
this.pack();
}
}
11
3. JOptionPane
JOptionPane is a predefined class
JOptionPane is a Container under swing package
JOptionPane is used to convey the message
through dialog box (take inputs/show the output)
of a frame.
Syntax:
JOptionPane = new JOptionPane();
12
//Demo for Fibonacci Series using JOptionPane Class
import javax.swing.*;
class fibserjp
{ public static void main(String args[ ]) throws Exception
{ int n=Integer.parseInt(JOptionPane.showInputDialog("Enter n:"));
int a=-1;
int b=1;
JOptionPane.showMessageDialog(null,"Fibonacci Series");
for(int i=0;i<n;i++)
{
int c=a+b;
JOptionPane.showMessageDialog(null,c);
a=b;
b=c;
}
}
}
13
4. JDialog
JDialog is a predefined class
JDialog is a Container under swing package
JDialog is used to convey the message through
dialog box (take inputs/show the output) of a
frame.
Syntax:
JDialog = new JDialog();
14
//Demo for Dialog box Frame using JOptionPane class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DLG extends JFrame
{ DLG()
{ super("Example: Swing Dialog");
final JFrame jf = this;
final JButton jb = new JButton("Show a message dialog!");
jb.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(jf,"This is a simple message dialog");
}
});
add(jb);
setSize(250,100);
}
public static void main(String[ ] args)
{ new DLG().setVisible(true);
}
}
15
5. JPanel
JPanel is a predefined and simplest container
class.
JPanel is a Container under swing package
JPanel is used to provide space to include other
panels (like buttons, textboxes etc..)
Panel is the Syntax:
JPanel = new JPanel();
16
//Demo for Panel in SWINGS
import java.awt.*;
import java.awt.event.*;
class PNL extends Frame
{ PNL()
{ super("Example: Panel");
final Panel p = new Panel();
p.add(new Button("1"));
p.add(new Button("2"));
p.add(new Button("3"));
add(p);
setSize(250,100);
}
public static void main(String[ ] args)
{ new PNL().setVisible(true);
}
}
17
6. JComboBox
JComboBox is a predefined class
JComboBox is a Container under swing package
JComboBox is used to display the list of items to
be selected in the frame.
There are two types of JComboBox:
Uneditable (default)
Editable
Syntax:
JComboBox = new JComboBox();
18
//Demo for ComboBox in swings
import javax.swing.*; import java.awt.*;
import java.awt.event.*;
public class ComboBox
{ JComboBox combo; JTextField txt;
public static void main(String[ ] args)
{ ComboBox b = new ComboBox();
}
public ComboBox()
{ String course[ ] = {"BCA","MCA",“M.TECH",“PH.D"};
JFrame frame = new JFrame("Creating a JComboBox Component");
JPanel panel = new JPanel();
combo = new JComboBox(course);
combo.setBackground(Color.gray);
combo.setForeground(Color.red);
txt = new JTextField(10);
panel.add(combo); panel.add(txt);
frame.add(panel);
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie)
{ String str = (String)combo.getSelectedItem();
txt.setText(str);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400); frame.setVisible(true);
}
}
19
7. JScrollPane
JScrollPane is a predefined class
JScrollPane is a Container under swing package
JScrollPane is used to increase the text area i.e.
top to bottom or left to right of a frame.
Syntax:
JScrollPane = new JScrollPane();
20
//Demo for JScrollPane in SWINGS
import javax.swing.*;
public class JScrollContainer
{ public static void main(String[ ] args)
{
String st = "Math_calculs_coordinate\n"
+ "Computer_Operator_Programer\n" + "Physics_Laboratry\n"
+ "Chemestru\n" + "Biology" + "Civics\n" +"History" + "Hindi\n"
+ "English\n" + "Tally" + "Coordinate\n"+ "Trigonometry"
+ "ram is a good boy\n" + "He has four cars and two computers.";
JFrame frame = new JFrame("Creating a JScrollPane Container");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JTextArea area = new JTextArea(st,5,6);
JScrollPane spane = new JScrollPane(area);
panel.add(spane);
frame.add(panel);
frame.setSize(400,400);
frame.setVisible(true);
}
}
21
8. JTabbedPane
JTabbedPane is a predefined class
JTabbedPane is a Container under swing package
JTabbedPane is used to display the panels in
tabbed manner in the frame.
Syntax:
JTabbedPane = new JTabbedPane();
22
//Demo for JTabbedPane
import javax.swing.*;
import java.awt.*;
public class CreateTabbedPane
{ public static void main(String[ ] args)
{ JFrame frame = new JFrame("Tabbed Pane Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JButton button = new JButton("1");
tab.add("Tab 1", button);
button = new JButton("2");
tab.add("Tab 2", button);
frame.setSize(400,400);
frame.setVisible(true);
}
}
23
9. JTable
JTable is a predefined class
JTable is a Container under swing package
JTable is used to display the table with rows and
columns along with header in the frame.
Syntax:
JTable = new JTable();
24
//Demo for JTable
import javax.swing.*; import javax.swing.table.*;
import java.awt.*;
public class GridLine
{ JTable table;
public static void main(String[ ] args)
{ new GridLine();
}
public GridLine()
{ JFrame frame = new JFrame("Grid Line in JTable Component!");
JPanel panel = new JPanel();
String data[ ][ ] = { {"Vinod","Computer","3"},
{"Rahul","History","2"},
{"Manoj","Biology","4"},
{"Sanjay","PSD","5"}};
String col [ ] = {"Name","Course","Year"};
DefaultTableModel model = new DefaultTableModel(data,col);
table = new JTable(model); table.setShowGrid(true);
table.setShowVerticalLines(true); table.setGridColor(Color.red);
JTableHeader header = table.getTableHeader(); header.setBackground(Color.yellow);
JScrollPane pane = new JScrollPane(table); panel.add(pane); frame.add(panel);
frame.setSize(500,150); frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
25
10. JTree
JTree is a predefined class
JTree is a Container under swing package
JTree is used to display the hierarchical data like
files in the disk.
Syntax:
JTree = new JTree();
26
//Displaying System Files in JTree
import javax.swing.*;
import javax.swing.event.*;
import java.util.Properties;
import javax.swing.JTree.*;
public class FileDirectoryTree
{ public static void main(String[ ] args)
{ try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
JFrame fm = new JFrame("System file properties of tree ");
Properties p = System.getProperties();
JTree tree = new JTree(p);
tree.setRootVisible(true);
JScrollPane scrollpane = new JScrollPane(tree);
fm.getContentPane().add(scrollpane, "Center");
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setSize(450,400);
fm.setVisible(true);
} catch (Exception e) {}
}
}
27
AWT SWINGS
31
/* Simple Java Program for the implementation of ProgressBar, you can also refer to
the progressbar code given beow and implement it in your programs */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class ProgressBarr extends JFrame implements ActionListener
{ JButton jb;
JProgressBar jpb1;
int value =0;
ProgressBarr()
{ Container contentPane=getContentPane();
contentPane.setLayout(new FlowLayout());
jb = new JButton("Click");
contentPane.add(jb);
jb.addActionListener(this);
jpb1 = new JProgressBar(0,1000);
contentPane.add(jpb1);
jpb1.setStringPainted(true);
}
public static void main(String args[ ])
{ ProgressBarr pb1 = new ProgressBarr();
pb1.setSize(500,500);
pb1.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{ while(value<=1000)
{ value = value+10;
jpb1.setValue(value);
}
}
}
32