Java Swing Part-2

in #utopian-io7 years ago (edited)

What Will We Learn from this Tutorial ?

In this tutorial , we are learning the basic knowledge of Java Swing .

  • We Will Learn from this tutorial about the basic of Swing
  • Also Learn Java Swing API like as JComboBox, JTable,JList,JOptionPane,JScrollBar,JPopUpMenu,Jprogessbar e.t.c

Requirements of Java Swings

There are some requirements in java Swing. That's are given below :

  • Basic Concept of Object Oriented Programming ( OOP)
  • Knowledge about AWT ( Abstract Window Toolkit) packages.

Difficulty

There is no difficulty in this tutorial . It is primary level contents and easy.

  • Basic

Tutorial Contents

In Previous tutorial, we already know that about the basic of Java Swing and some of Java Swing API like JButton,Jlabel,JTextField,JPasswordField,JCheckBox, JRadioButton. In this Tutorial, we are learning about the rest of the Swing API like JComboBox,JTable,JList,JOptionPane,JScrollBar,JPopUpMenu,Jprogessbar .
Let's discuss about the Swing API :

Java JComboBox : In Java JComboBox class is used to show Popup menu of choices.That Choices are selected by the user. It Inherits from Java JComponent class.

Here, we see an example of Java JComboBox-

import javax.swing.*;    
public class combobox{    
JFrame f;    
combobox(){    
    f=new JFrame("ComboBox");    
    String BloodGroup[]={"A+","AB+","B+","O+","A-","AB-","B-","O-"};        
    JComboBox cb=new JComboBox(BloodGroup);    
    cb.setBounds(50, 50,90,20);    
    f.add(cb);        
    f.setLayout(null);    
    f.setSize(400,500);    
    f.setVisible(true);         
}    
public static void main(String[] args) {    
    new combobox();         
}    
}   

OutPut : After running the program, we get the following result -
1.png

Java JTable : In Java JTable class is used for displaying data in a table form.

Let's see an example of Java JTable-

import javax.swing.*;    
public class table {    
    JFrame f;    
    table(){    
    f=new JFrame();    
    String data[][]={ {"101","sakib","15000"},    
                          {"102","rakib","22000"},    
                          {"103","wahid","25000"}};    
    String column[]={"ID","NAME","SALARY"};         
    JTable jt=new JTable(data,column);    
    jt.setBounds(30,40,200,300);          
    JScrollPane sp=new JScrollPane(jt);    
    f.add(sp);          
    f.setSize(300,400);    
    f.setVisible(true);    
}     
public static void main(String[] args) {    
    new table();    
}
}

OutPut : After running the program, we get the following results -
2.PNG
2a.PNG

Java JList: In Java Jlist class represents a list of text Items. That's text item should be set up so that the user can choose either one item or multiple items.

Let's see an example of Java JList -

import javax.swing.*;  
public class list {  
     list(){  
        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 list();  
    }
}  

Output: After running the program, we get the following results-
3.PNG
3a.PNG

Java JOptionPane: In Java JOptionPane is used to provide standard dialog boxes such as message dialog box, confirm dialog box . It Inherits from JComponent class.

Let's see an example of Java JOptionPane -

import javax.swing.*;  
public class optionpane {  
JFrame f;  
optionpane(){  
    f=new JFrame();  
    JOptionPane.showMessageDialog(f,"Hello, Welcome to Java Swing part-2  Tutorial !!!");  
}  
public static void main(String[] args) {  
    new optionpane();  
}  
}  

OutPut : After running the program, we get the following results -
4.PNG
4a.PNG

Java JScrollBar: In Java JScrollBar class is used to add horizontal and vertical scrollbar. It Inherits from JComponent Class.

Here, we see an example of Java JScrollBar-

import javax.swing.*;  
class scrollbar{  
scrollbar(){  
    JFrame f= new JFrame("Scrollbar Example");  
 JScrollBar s=new JScrollBar();  
s.setBounds(100,100, 50,100);  
f.add(s);  
f.setSize(400,400);  
f.setLayout(null);  
f.setVisible(true);  
}  
public static void main(String args[])  
    {  
        new scrollbar();  
    }
}  

OutPut : After running the program, we get following results-
5.PNG
5a.PNG

Java JPopUpMenu : In Java JPopUpMenu class can be dynamically popped up at specific position within a component.

Here, we can see an example of Java JPopUpMenu-

import javax.swing.*;  
import java.awt.event.*;  
class popupmenu  {  
     popupmenu(){  
         final JFrame f= new JFrame("PopupMenu Example");  
         final JPopupMenu popupmenu = new JPopupMenu("Edit");   
         JMenuItem kabir = new JMenuItem("kabir");  
         JMenuItem wahid = new JMenuItem("wahid");  
         JMenuItem safat = new JMenuItem("safat");  
         popupmenu.add(kabir); popupmenu.add(wahid); popupmenu.add(safat);        
         f.addMouseListener(new MouseAdapter() {  
            public void mouseClicked(MouseEvent e) {              
                popupmenu.show(f , e.getX(), e.getY());  
            }                 
         });  
         f.add(popupmenu);   
         f.setSize(300,300);  
         f.setLayout(null);  
         f.setVisible(true);  
     }  
public static void main(String args[])  
{  
        new popupmenu();  
}
}  

OutPut : After running the program, we get following the results-
6.PNG
2018-01-23 (11).png

Java JProgressBar: In Java JProgressBar is used to display the progress of the task .It Inherits from JComponent class.

Here, Let's see an example of Java JProgressBar-

import javax.swing.*;    
public class progressbar extends JFrame{    
JProgressBar jb;    
int i=0,num=0;     
progressbar(){    
jb=new JProgressBar(0,1000);    
jb.setBounds(40,40,160,30);         
jb.setValue(0);    
jb.setStringPainted(true);    
add(jb);    
setSize(250,150);    
setLayout(null);    
}    
public void iterate(){    
while(i<=1000){    
  jb.setValue(i);    
  i=i+10;    
  try{Thread.sleep(150);}catch(Exception e){}    
}    
}    
public static void main(String[] args) {    
    progressbar m=new progressbar();    
    m.setVisible(true);    
    m.iterate();    
}    
} 

OutPut : After running the program, we get the following results-
7.PNG
7a.PNG

Curriculum

Here , Add an previous tutorial Link is given below :



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Like your contribution, upvote.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

The post is lacking proper formatting. The basic code structure is very similar to examples in https://javatechnocampus.wordpress.com/2016/01/16/java-swing/ with the values changed.

You can contact us on Discord.
[utopian-moderator]

Hey @wahidurrahman I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.20
TRX 0.19
JST 0.034
BTC 91295.19
ETH 3130.58
USDT 1.00
SBD 2.89