UNIT-II Notes Advance Java
UNIT-II Notes Advance Java
********************Swing*******************
####JFrame Class####
- JFrame is a standard window which has title bar,menu-bar,borders,
minimize,maximum button and resizing corner.
*Constructor:
1) JFrame() - create window without title.
*Methods:
1) void setVisible(true/false)
2) void setSize(width,height)
Where:
operation:- JFrame.EXIT_ON_CLOSE
JFrame.HIDE_ON_CLOSE
JFrame.DO_NOTHING_ON_CLOSE
Code:-
1)
import javax.swing.*;
JFrameDemo()
jd.setVisible(true);
jd.setTitle("JFrame Window");
jd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jd.setSize(500,500);
Output:-
2)
import javax.swing.*;
JFrameDemo1(String title)
super(title);
jd.setVisible(true);
jd.setSize(500,500);
//jd.setTitle("JFrame Window");
Output:-
3)
import java.applet.*;
import java.awt.*;
import javax.swing.*;
g.drawString("VJTech Academy!!!",40,30);
</applet>*/
Output:-
********************************************************
***Constructor:
1) ImageIcon(String filename);
2) ImageIcon(URL url);
**Methods:
1) int getIconHeight();
2) int getIconWidth();
****************************************************************
***Constructor:
1) JLabel(ImageIcon ii);
2) JLabel(String str);
4) JLabel();
***Methods:
1) void setText(String str)
2) String getText();
4) int getAlignment();
6) ImageIcon getIcon();
Code:-
import java.awt.*;
import javax.swing.*;
JLabelDemo()
Container c=getContentPane();
c.setLayout(f1);
c.add(L1);
jld.setVisible(true);
jld.setTitle("JLabel Demo");
jld.setSize(500,500);
Output:-
********************************************************
***Constructor:
1) JTextField();
2) JTextField(String str);
3) JTextField(int max_chars);
***Methods:
1) void setText(String str)
2) String getText();
4) Boolean isEdiatble();
5) char getEchoChar();
Code:-
import java.awt.*;
import javax.swing.*;
JTextFieldDemo()
Container c1=getContentPane();
c1.setLayout(f1);
c1.add(L1);
c1.add(tf1);
c1.add(L2);
c1.add(tf2);
c1.add(b1);
jfd.setVisible(true);
jfd.setTitle("JTextField Demo");
jfd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfd.setSize(500,500);
Output:-
-**Constructor.
1) JTextArea();
3) JTextArea(String str);
-**Methods:
1) String getText();
3) boolean isEditable();
5) String getSelectedText();
Code:-
import java.awt.*;
import javax.swing.*;
JTextAreaDemo()
Container c=getContentPane();
c.setLayout(f1);
c.setBackground(Color.cyan);
c.add(ta1);
c.add(ta2);
j1.setVisible(true);
j1.setTitle("JTextArea Demo");
j1.setSize(800,800);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output:-
********************************************************
- When user click on combo box control then list of items are displayed and
user can select one of the item among the list.
***Constructor:
1) JComboBox();
2) JComboBox(Vector obj);
***Method:
1) void addItem(Object obj);
3) void removeAllItems();
Code:-
import java.awt.*;
import javax.swing.*;
JComboBoxDemo()
Container c=getContentPane();
c.setLayout(f1);
c.setBackground(Color.orange);
jcb.addItem("C Lang");
jcb.addItem("C++ Lang");
jcb.addItem("Java Lang");
jcb.addItem("Python Lang");
jcb.addItem("PHP Lang");
c.add(jcb);
j1.setVisible(true);
j1.setTitle("JComboBox Demo");
j1.setSize(800,800);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output:-
****************************************************************
***Constructor:
1) JButton(String str)
2) JButton(ImageIcon ii)
4) JButton();
***Methods:
1) void setLabel(String str)
2) String getLabel();
4) ImageIcon getIcon();
Code:-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JButton b1,b2;
JLabel L1;
JButtonDemo()
Container c=getContentPane();
c.setLayout(f1);
c.setBackground(Color.yellow);
b2=new JButton(ii);
L1.setForeground(Color.red);
b1.addActionListener(this);
c.add(b1);
c.add(L1);
c.add(b2);
j1.setVisible(true);
j1.setTitle("JButton Demo");
j1.setSize(500,500);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output:-
********************************************************
***Constructor:
1) JCheckBox(String str);
3) JCheckBox(ImageIcon ii)
***Methods
1) void setText(String str);
2) String getText();
4) ImageIcon getIcon();
5) boolean getState();
6) boolean isSelected();
Code:-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JCheckBox jc1,jc2,jc3,jc4;
JLabel L1;
JCheckBoxDemo()
Container c=getContentPane();
c.setLayout(f1);
c.setBackground(Color.green);
jc1=new JCheckBox("Malegoan",true);
jc2=new JCheckBox("Pune");
jc3=new JCheckBox("Baramati");
jc4=new JCheckBox("Thane");
jc1.addItemListener(this);
jc2.addItemListener(this);
jc3.addItemListener(this);
jc4.addItemListener(this);
c.add(jc1);
c.add(jc2);
c.add(jc3);
c.add(jc4);
c.add(L1);
if(jc1.isSelected())
L1.setText(jc1.getText()+"Checkbox Selected");
else if(jc2.isSelected())
L1.setText(jc2.getText()+"Checkbox Selected");
else if(jc3.isSelected())
L1.setText(jc3.getText()+"Checkbox Selected");
else if(jc4.isSelected())
L1.setText(jc4.getText()+"Checkbox Selected");
j1.setVisible(true);
j1.setTitle("JCheckBox Demo");
j1.setSize(600,600);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output:-
********************************************************
***Constructor:
1) JRadioButton(String str)
3) JRadioButton(ImageIcon ii);
***Methods:
1) void setText(String str);
2) String getText()
4) ImageIcon getIcon();
6) boolean isSelected();
Code:-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JRadioButton b1,b2,b3;
JRadioButtonDemo()
Container c=getContentPane();
c.setLayout(f1);
c.setBackground(Color.pink);
b1=new JRadioButton("Male");
b2=new JRadioButton("Female");
b3=new JRadioButton("Other");
bg.add(b1);
bg.add(b2);
bg.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add(b1);
c.add(b2);
c.add(b3);
if(b1.isSelected())
else if(b2.isSelected())
else
j1.setVisible(true);
j1.setTitle("JRadioButton Demo");
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j1.setSize(500,500);
Output:-
- It will creates multiple tabs and each tab has its own title and related
components.
- We can see only one tab at a time. We can not open multiple tabs at same
time.
*** Constructor:
1) JTabbedPane();
*** Method:
1) void addTab(String title,Component obj);
*** Procedure:
1) Define required no of Panel classes which contains different components.
3) Use addTab() method to add tabs into JTabbedPane( title and component
object)
Code:-
import java.awt.*;
import javax.swing.*;
JPanel1()
add(b1);
add(b2);
add(b3);
JPanel2()
bg.add(r1);
bg.add(r2);
add(r1);
add(r2);
JPanel3()
cb.addItem("India");
cb.addItem("US");
cb.addItem("UK");
add(cb);
JTabbedPaneDemo()
Container c=getContentPane();
c.setBackground(Color.red);
jtp.addTab("Tab1",p1);
jtp.addTab("Tab2",p2);
jtp.addTab("Tab3",p3);
c.add(jtp);
j1.setVisible(true);
j1.setTitle("JTabbedPane Demo");
j1.setSize(600,600);
j1.setDefaultCloseOperation(EXIT_ON_CLOSE);
Output:-
*** Constructor:
1) JScrollPane(Component obj)
Where:
vertical,horizontal=> this is integer constants(defined in ScrollPaneConstants
interface) which defines vertical and horizontal scrollbar.
HORIZONTAL_SCROLLBAR_ALWAYS
VERTICAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_AS_NEEDED
VERTICAL_SCROLLBAR_AS_NEEDED
Code:-
import java.awt.*;
import javax.swing.*;
JScrollPaneDemo()
Container c=getContentPane();
c.setBackground(Color.red);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
c.add(jsp);
j1.setVisible(true);
j1.setTitle("JScrollPane Demo");
j1.setSize(600,600);
j1.setDefaultCloseOperation(EXIT_ON_CLOSE);
Output:-
*** Constructor:
1) JTree(Hashtable ht)
2) JTree(Object obj[])
3) JTree(TreeNode tn)
4) JTree(Vector v);
1) DefaultMutableTreeNode(Object obj)
**Method:
void add(MutableTreeNode child);
***Procedure:
1) Create JTree Object
2) Create JscrollPane
Code:-
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
JTreeNodeDemo()
Container c=getContentPane();
c.setLayout(b1);
DefaultMutableTreeNode root=new
DefaultMutableTreeNode("Language");
DefaultMutableTreeNode r1=new
DefaultMutableTreeNode("POP Lang");
DefaultMutableTreeNode r2=new
DefaultMutableTreeNode("OOP Lang");
root.add(r1);
root.add(r2);
DefaultMutableTreeNode r11=new
DefaultMutableTreeNode("C Lang");
DefaultMutableTreeNode r12=new
DefaultMutableTreeNode("PACAL Lang");
DefaultMutableTreeNode r13=new
DefaultMutableTreeNode("FORTRAN Lang");
r1.add(r11);
r1.add(r12);
r1.add(r13);
DefaultMutableTreeNode r21=new
DefaultMutableTreeNode("C++ Lang");
DefaultMutableTreeNode r22=new
DefaultMutableTreeNode("Java Lang");
DefaultMutableTreeNode r23=new
DefaultMutableTreeNode("Python Lang");
r2.add(r21);
r2.add(r22);
r2.add(r23);
int
v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
c.add(jsp,BorderLayout.WEST);
j1.setVisible(true);
j1.setTitle("JTree Demo");
j1.setSize(600,600);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output:-
- If we want to display the data in tabular format then we can use JTable class.
***Constructor:
1) JTable(Object data[][],Object ColHeads[])
***Procedure:
1) Create JTable object with data and column heading.
Code:-
import javax.swing.*;
import java.awt.*;
JTableDemo()
Container c=getContentPane();
c.setLayout(b1);
String colHeads[]={"RollNo","Name","Marks"};
String data[][]={
{"1010","Dennis","98.99"},
{"1020","Bjarne","67.89"},
{"1030","James","90.90"},
{"1040","Karan","23.45"},
{"1050","Vishal","100"},
};
jt.setBackground(Color.yellow);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
c.add(jsp,BorderLayout.CENTER);
j1.setVisible(true);
j1.setTitle("JTable Demo");
j1.setSize(600,600);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output:-
********************************************************
****Constructor:
1) JProgressBar() - create horizontal progress bar.
4) JProgressBar(BoundedRangeModel model)
***Methods:
1) void setValue(int val);
Code:-
import java.awt.*;
import javax.swing.*;
JProgressBar jpb;
JProgressBarDemo()
Container c=getContentPane();
c.setLayout(null);
c.setBackground(Color.orange);
jpb=new JProgressBar(0,3000);
jpb.setBounds(200,200,200,40);
jpb.setValue(0);
jpb.setStringPainted(true);
c.add(jpb);
int i=0;
while(i<3000)
jpb.setValue(i);
i=i+20;
try
Thread.sleep(150);
catch(Exception e)
j1.setVisible(true);
j1.setTitle("JProgressBar Demo");
j1.setSize(700,700);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j1.ChangeProgressBarValue();
Output:-
- We can add tooltip text to almost all the components of Java Swing.
Code:-
import java.awt.*;
import javax.swing.*;
JProgressBar jpb;
JToolTipClass()
Container c=getContentPane();
c.setLayout(null);
c.setBackground(Color.orange);
jpb=new JProgressBar(0,3000);
jpb.setBounds(200,200,200,40);
b1.setBounds(200,300,150,40);
jpb.setValue(0);
jpb.setStringPainted(true);
c.add(jpb);
c.add(b1);
int i=0;
while(i<3000)
jpb.setValue(i);
i=i+20;
try
Thread.sleep(150);
catch(Exception e)
j1.setVisible(true);
j1.setTitle("JProgressBar Demo");
j1.setSize(700,700);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j1.ChangeProgressBarValue();
Output:-
- JSlider class is a component that allows the user to select a value by moving
the knob.
- The knob is always positioned at the points that match the integer values
within specified interval.
***Constructor:
1) JSlider() - it will create HORIZONTAL JSlider with 0 to 100 values.
4) JSlider(int min,int max) - HORIZONTAL slider with specified min and max
value
Where:
orient = HORIZONTAL,VERTICAL
***Methods:
1) public void setMinorTickSpacingI(int n)
Code:-
import java.awt.*;
import javax.swing.*;
JSliderDemo()
Container c=getContentPane();
c.setLayout(new FlowLayout());
c.setBackground(Color.cyan);
c.add(js);
j1.setVisible(true);
j1.setTitle("JSlider Demo");
j1.setSize(700,700);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output:-
2) The way that the component looks when submitted on the screen.
In MVC Architecture:
1) Model:
- It defined the state information about the component i.e the event and its
graphical change when even occurs on a component.
2) View:
3) Controller:
- It is used to create any type of custom dialog boxes with the use of
JOptionPane.
***Constructor:
1) JDialog() - it will create model less dialog without title and without parent
window object.
2) JDialog(Frame obj) - It will create model less dialog without title and with
parent window Frame object.
3) JDialog(Frame obj, String title, boolean flag) flag=> TRUE then model and
flag=>FALSE then model less.
Code:-
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
JDialog j1;
JDialogDemo()
Container c=getContentPane();
c.setLayout(new FlowLayout());
c.add(b2);
b2.addActionListener(this);
j1.setLayout(new FlowLayout());
j1.add(b1);
j1.setSize(500,500);
j1.setVisible(true);
jdd.setVisible(true);
jdd.setTitle("JDialogDemo");
jdd.setSize(800,800);
jdd.setDefaultCloseOperation(EXIT_ON_CLOSE);
Output:-
VJTech Academy…