Swing 2nd Class

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Java Swing

Java JComboBox:
The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits JComponent
 class.
declaration class:
public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible  

Syntax:

JComboBox cb=new JComboBox(country);    

Constructors=>

Constructor Descriptions
JComboBox() Creates a JComboBox with a default data model.
JComboBox(Object[] items) Creates a JComboBox that contains the elements in the
specified array.
JComboBox(Vector<?> items) Creates a JComboBox that contains the elements in the
specified Vector.

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Java Swing
Java JComboBox: Example=> b.addActionListener(new ActionListener() {
public class ComboBox { public void actionPerformed(ActionEvent e) {
JFrame f; String data = "Programming language Selected: "
+ cb.getItemAt(cb.getSelectedIndex());
ComboBox(){
label.setText(data);
f=new JFrame("ComboBox Example"); }
final JLabel label = new JLabel(); });
label.setHorizontalAlignment(JLabel.CENTER); }
public static void main(String[] args) {
label.setSize(400,100);
new ComboBox();
JButton b=new JButton("Show"); }
b.setBounds(200,100,75,20); }
String languages[]={"C","C++","C#","Java","PHP"};
final JComboBox cb=new JComboBox(languages);
cb.setBounds(50, 100,90,20);
f.add(cb); f.add(label); f.add(b);
f.setLayout(null);
f.setSize(350,350);
f.setVisible(true);

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Java Swing
Java JTable:
The JTable class is used to display data in tabular form. It is composed of rows and columns.

Syntax:

JTable jt=new JTable(data,column);   

  
Constructors:

Constructor Description
JTable() Creates a table with empty cells.
JTable(Object[][] rows, Object[] columns) Creates a table with the specified data.

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Java Swing
Java JTable: Example=> int[] row = jt.getSelectedRows();
public class Table {
int[] columns = jt.getSelectedColumns();
for (int i = 0; i < row.length; i++) {
public static void main(String[] a) { for (int j = 0; j < columns.length; j++) {
JFrame f = new JFrame("Table Example"); Data = (String) jt.getValueAt(row[i],
columns[j]);
String data[][]={ {"101","Amit","670000"}, }}
System.out.println("Table element selected is: "
{"102","Jai","780000"},
+ Data);
{"101","Sachin","700000"}}; }
String column[]={"ID","NAME","SALARY"}; });
final JTable jt=new JTable(data,column); JScrollPane sp=new JScrollPane(jt);
f.add(sp);
jt.setCellSelectionEnabled(true);
f.setSize(300, 200);
ListSelectionModel select= jt.getSelectionModel(); f.setVisible(true);
select.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); }
select.addListSelectionListener(new ListSelectionListener() { }
public void valueChanged(ListSelectionEvent e) {
String Data = null;

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Java Swing
Java Jlist:
The object of JList class represents a list of text items. The list of text items can be set up so that the user can choose either one item or multiple items.
It inherits JComponent class.
Syntax:

 JList<String> list = new JList<>(ListObject);  

Constructor:

Constructor Description
Jlist() Creates a JList with an empty, read-only, model.
JList(ary[] listData) Creates a JList that displays the elements in the specified array.
JList(ListModel<ary> dataModel) Creates a JList that displays elements from the specified, non-null,
model.

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Java Swing
Java Jlist: Example=>

public class ListBox {


ListBox(){
JFrame f= new JFrame();
DefaultListModel<String> l1 = new DefaultListModel<>();
l1.addElement("Item1");
l1.addElement("Item2");
l1.addElement("Item3");
l1.addElement("Item4");
JList<String> list = new JList<>(l1);
list.setBounds(100,100, 75,75);
f.add(list);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ListBox();
}}

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Java Swing
Java JOptionalPane:
The JOptionPane class is used to provide standard dialog boxes such as message dialog box, confirm dialog box and input dialog box. These dialog
boxes are used to display information or get input from the user. 
Syntax:
JOptiona;lPane.showMessageDialog(jframe_object),”Message”;
Methods of JOptionPane class:
Method Description
Static void showMessageDialog(Component It is used to create an information-message dialog titled
parentComponent, Object message) "Message".
Static void showMessageDialog(Component It is used to create a message dialog with given title and
parentComponent, Object message, String title, messageType.
int messageType)
Static void showConfirmDialog(Component It is used to create a dialog with the options Yes, No and Cancel;
parentComponent, Object message) with the title, Select an Option.
Static void showInputDialog(Component It is used to show a question-message dialog requesting input
parentComponent, Object message) from the user parented to parentComponent.
void setInputValue(Object newValue) It is used to set the input value that was selected or input by the
user.

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Java Swing
Java JOptionalPane:
Example:

package demoSwing;
import javax.swing.*;
public class JOption {
JFrame f;
JOption(){
f=new JFrame();
JOptionPane.showMessageDialog(f,"Successfully Updated.","Alert",JOptionPane.WARNING_MESSAGE);
}
public static void main(String[] args) {
new JOption();
}
}

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Java Swing
Java JScrollBar:
The object of JScrollbar class is used to add horizontal and vertical scrollbar. It is an implementation of a scrollbar. 
Syntax:
The object of JScrollbar class is used to add horizontal and vertical scrollbar.
Syntax:

 JScrollBar s=new JScrollBar();  

Constructors:

Constructor Description
JScrollBar() Creates a vertical scrollbar with the initial values.
JScrollBar(int orientation) Creates a scrollbar with the specified orientation and the initial
values.
ScrollBar(int orientation, int value, int extent, Creates a scrollbar with the specified orientation, value, extent,
int min, int max) minimum, and maximum.

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Java Swing
Java JScrollBar:
Example:
package demoSwing;
import javax.swing.*;
public class Scroll {
Scroll(){
JFrame f= new JFrame("Scrollbar Example");
JScrollBar s=new JScrollBar();
s.setBounds(100,100, 20,100);
JScrollBar s1=new JScrollBar(JScrollBar.HORIZONTAL );
s1.setBounds(100,200, 100,20);
f.add(s);
f.add(s1);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);}
public static void main(String args[])
{
new Scroll();}}
BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING
Java Swing
JMenuBar: Example=>
cut.addActionListener(this);
public class Menu implements ActionListener {
copy.addActionListener(this);
JFrame f; JMenuBar mb;
paste.addActionListener(this);
JMenu home,file,edit,help;
selectAll.addActionListener(this);
JMenuItem cut,copy,paste,selectAll,profile,porfolio,gallery;
edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);
JTextArea ta;
Menu(){
profile=new JMenuItem("profile"); // this is for home menu
mb=new JMenuBar();
porfolio=new JMenuItem("porfolio");
home=new JMenu("Home");
gallery=new JMenuItem("gallery");
file=new JMenu("File");
profile.addActionListener(this);
edit=new JMenu("Edit");
porfolio.addActionListener(this);
help=new JMenu("Help");
gallery.addActionListener(this);
mb.add(home); mb.add(file);mb.add(edit);mb.add(help);
home.add(profile);home.add(porfolio);home.add(gallery);
f=new JFrame(); // this is for edit menu ta=new JTextArea();
cut=new JMenuItem("cut"); // this is for edit menu ta.setBounds(5,5,360,320);
f.add(mb);f.add(ta);
copy=new JMenuItem("copy"); f.setJMenuBar(mb);
paste=new JMenuItem("paste"); f.setLayout(null);
f.setSize(400,400);
selectAll=new JMenuItem("selectAll"); f.setVisible(true);
}
BCA 6 SEMESTAR | ADVANCE JAVA PROGRAMMING
th
Java Swing
JMenuBar: Example=>

public void actionPerformed(ActionEvent e) {


if(e.getSource()==cut)
ta.cut();
if(e.getSource()==paste)
ta.paste();
if(e.getSource()==copy)
ta.copy();
if(e.getSource()==selectAll)
ta.selectAll();
}
public static void main(String[] args) {
new Menu();
}
}

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING

You might also like