hereral/Clara-Training-Data huggingface dataset:
Question: Can you provide a Close AWT Window Example 2: extending WindowAdapter?
Answer: // importing the necessary libraries  
import java.awt.*;    
import java.awt.event.*;    
  
// class which inherits the WindowAdapter class  
public class AdapterExample extends WindowAdapter {    
// object of Frame  
    Frame f;    
// class constructor  
    AdapterExample() {    
// creating the frame  
        f = new Frame();    
// adding  WindowListener to the frame  
        f.addWindowListener (this);    
        
// setting the size, layout and visibility of frame  
        f.setSize (400, 400);    
        f.setLayout (null);    
        f.setVisible (true);    
    }    
  
// overriding the windowClosing() method   
public void windowClosing (WindowEvent e) {    
    f.dispose();    
}    
// main method  
public static void main(String[] args) {    
    new AdapterExample();    
}    
}
Question: What is the Difference between AWT and Swing?
Answer:  No.1 Java AWT : AWT components are platform-dependent.
Java Swing: Java swing components are platform-independent.
No.2 Java AWT : AWT components are heavyweight.
Java Swing: Swing components are lightweight.
No.3 Java AWT : AWT doesn't support pluggable look and feel.
Java Swing: Swing supports pluggable look and feel
No.4 Java AWT : AWT provides less components than Swing.
Java Swing: Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc.
No.5 Java AWT : AWT doesn't follows MVC(Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view.
Java Swing: Swing follows MVC.
Question: What is JFC?
Answer: The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications.
Question: What are the Commonly used Methods of Component class?
Answer: Method: public void add(Component c)
Description: add a component on another component.
Method: public void setSize(int width,int height)
Description: sets size of the component.
Method: public void setLayout(LayoutManager m)
Description: sets the layout manager for the component.
Method: public void setVisible(boolean b)
Description: sets the visibility of the component. It is by default false.
Question: What is Java JButton?
Answer: The JButton class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed. It inherits AbstractButton class.
Question: What is JButton class declaration?
Answer: public class JButton extends AbstractButton implements Accessible
Question: What are the Commonly used Constructors:?
Answer: Constructor: JButton()
Description: It creates a button with no text and icon.
Constructor: JButton(String s)
Description: It creates a button with the specified text.
Constructor: JButton(Icon i)
Description: It creates a button with the specified icon object.
Question: What are the Commonly used Methods of AbstractButton class?
Answer: Methods: void setText(String s)
Description: It is used to set specified text on button
Methods: String getText()
Description: It is used to return the text of the button.
Methods: void setEnabled(boolean b)
Description: It is used to enable or disable the button.
Methods: void setIcon(Icon b)
Description: It is used to set the specified Icon on the button.
Methods: Icon getIcon()
Description: It is used to get the Icon of the button.
Methods: void setMnemonic(int a)
Description: It is used to set the mnemonic on the button.
Methods: It is used to set the mnemonic on the button.
Description: It is used to add the action listener to this object.
Question: Can you provide a Java JButton Example?
Answer: import java.awt.event.*;  
import javax.swing.*;    
public class ButtonExample {  
public static void main(String[] args) {  
    JFrame f=new JFrame("Button Example");  
    final JTextField tf=new JTextField();  
    tf.setBounds(50,50, 150,20);  
    JButton b=new JButton("Click Here");  
    b.setBounds(50,100,95,30);  
    b.addActionListener(new ActionListener(){  
public void actionPerformed(ActionEvent e){  
            tf.setText("Welcome to Javatpoint.");  
        }  
    });  
    f.add(b);f.add(tf);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);   
}  
}
Question: Can you provide a Java JButton Example with ActionListener?
Answer: import java.awt.event.*;  
import javax.swing.*;    
public class ButtonExample {  
public static void main(String[] args) {  
    JFrame f=new JFrame("Button Example");  
    final JTextField tf=new JTextField();  
    tf.setBounds(50,50, 150,20);  
    JButton b=new JButton("Click Here");  
    b.setBounds(50,100,95,30);  
    b.addActionListener(new ActionListener(){  
public void actionPerformed(ActionEvent e){  
            tf.setText("Welcome to Javatpoint.");  
        }  
    });  
    f.add(b);f.add(tf);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);   
}  
}
Question: Can you provide an Example of displaying image on the button?
Answer: import javax.swing.*;      
public class ButtonExample{    
ButtonExample(){    
JFrame f=new JFrame("Button Example");            
JButton b=new JButton(new ImageIcon("D:\\icon.png"));    
b.setBounds(100,100,100, 40);    
f.add(b);    
f.setSize(300,400);    
f.setLayout(null);    
f.setVisible(true);    
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    }         
public static void main(String[] args) {    
    new ButtonExample();    
}    
}
Question: What is Java JLabel?
Answer: The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly. It inherits JComponent class.
Question: What is JLabel class declaration?
Answer: public class JLabel extends JComponent implements SwingConstants, Accessible
Question: Can you provide a Java JLabel Example?
Answer: import javax.swing.*;  
class LabelExample  
{  
public static void main(String args[])  
    {  
    JFrame f= new JFrame("Label Example");  
    JLabel l1,l2;  
    l1=new JLabel("First Label.");  
    l1.setBounds(50,50, 100,30);  
    l2=new JLabel("Second Label.");  
    l2.setBounds(50,100, 100,30);  
    f.add(l1); f.add(l2);  
    f.setSize(300,300);  
    f.setLayout(null);  
    f.setVisible(true);  
    }  
    }
Question: Can you provide a Java JLabel Example with ActionListener?
Answer: import javax.swing.*;  
import java.awt.*;  
import java.awt.event.*;  
public class LabelExample extends Frame implements ActionListener{  
    JTextField tf; JLabel l; JButton b;  
    LabelExample(){  
        tf=new JTextField();  
        tf.setBounds(50,50, 150,20);  
        l=new JLabel();  
        l.setBounds(50,100, 250,20);      
        b=new JButton("Find IP");  
        b.setBounds(50,150,95,30);  
        b.addActionListener(this);    
        add(b);add(tf);add(l);    
        setSize(400,400);  
        setLayout(null);  
        setVisible(true);  
    }  
    public void actionPerformed(ActionEvent e) {  
        try{  
        String host=tf.getText();  
        String ip=java.net.InetAddress.getByName(host).getHostAddress();  
        l.setText("IP of "+host+" is: "+ip);  
        }catch(Exception ex){System.out.println(ex);}  
    }  
    public static void main(String[] args) {  
        new LabelExample();  
    } }
Question: What is Java JTextField?
Answer: The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class.
Question: What is JTextField class declaration?
Answer: public class JTextField extends JTextComponent implements SwingConstants
Question: Can you provide a Java JTextField Example?
Answer: import javax.swing.*;  
class TextFieldExample  
{  
public static void main(String args[])  
    {  
    JFrame f= new JFrame("TextField Example");  
    JTextField t1,t2;  
    t1=new JTextField("Welcome to Javatpoint.");  
    t1.setBounds(50,100, 200,30);  
    t2=new JTextField("AWT Tutorial");  
    t2.setBounds(50,150, 200,30);  
    f.add(t1); f.add(t2);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);  
    }  
    }
Question: Can you provide a Java JTextField Example with ActionListener?
Answer: import javax.swing.*;  
import java.awt.event.*;  
public class TextFieldExample implements ActionListener{  
    JTextField tf1,tf2,tf3;  
    JButton b1,b2;  
    TextFieldExample(){  
        JFrame f= new JFrame();  
        tf1=new JTextField();  
        tf1.setBounds(50,50,150,20);  
        tf2=new JTextField();  
        tf2.setBounds(50,100,150,20);  
        tf3=new JTextField();  
        tf3.setBounds(50,150,150,20);  
        tf3.setEditable(false);   
        b1=new JButton("+");  
        b1.setBounds(50,200,50,50);  
        b2=new JButton("-");  
        b2.setBounds(120,200,50,50);  
        b1.addActionListener(this);  
        b2.addActionListener(this);  
        f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);  
        f.setSize(300,300);  
        f.setLayout(null);  
        f.setVisible(true);  
    }         
    public void actionPerformed(ActionEvent e) {  
        String s1=tf1.getText();  
        String s2=tf2.getText();  
        int a=Integer.parseInt(s1);  
        int b=Integer.parseInt(s2);  
        int c=0;  
        if(e.getSource()==b1){  
            c=a+b;  
        }else if(e.getSource()==b2){  
            c=a-b;  
        }  
        String result=String.valueOf(c);  
        tf3.setText(result);  
    }  
public static void main(String[] args) {  
    new TextFieldExample();  
} }
Question: What is Java JTextArea?
Answer: The object of a JTextArea class is a multi line region that displays text. It allows the editing of multiple line text. It inherits JTextComponent class
Question: What is JTextArea class declaration?
Answer: public class JTextArea extends JTextComponent
Question: Can you provide a Java JTextArea Example?
Answer: import javax.swing.*;  
public class TextAreaExample  
{  
     TextAreaExample(){  
        JFrame f= new JFrame();  
        JTextArea area=new JTextArea("Welcome to javatpoint");  
        area.setBounds(10,30, 200,200);  
        f.add(area);  
        f.setSize(300,300);  
        f.setLayout(null);  
        f.setVisible(true);  
     }  
public static void main(String args[])  
    {  
   new TextAreaExample();  
    }}
Question: Can you provide a Java JTextArea Example with ActionListener?
Answer: import javax.swing.*;  
import java.awt.event.*;  
public class TextAreaExample implements ActionListener{  
JLabel l1,l2;  
JTextArea area;  
JButton b;  
TextAreaExample() {  
    JFrame f= new JFrame();  
    l1=new JLabel();  
    l1.setBounds(50,25,100,30);  
    l2=new JLabel();  
    l2.setBounds(160,25,100,30);  
    area=new JTextArea();  
    area.setBounds(20,75,250,200);  
    b=new JButton("Count Words");  
    b.setBounds(100,300,120,30);  
    b.addActionListener(this);  
    f.add(l1);f.add(l2);f.add(area);f.add(b);  
    f.setSize(450,450);  
    f.setLayout(null);  
    f.setVisible(true);  
}  
public void actionPerformed(ActionEvent e){  
    String text=area.getText();  
    String words[]=text.split("\\s");  
    l1.setText("Words: "+words.length);  
    l2.setText("Characters: "+text.length());  
}  
public static void main(String[] args) {  
    new TextAreaExample();  
}  
}
Question: What is JPasswordField class declaration?
Answer: public class JPasswordField extends JTextField
Question: Can you provide a Java JPasswordField Example?
Answer: import javax.swing.*;    
public class PasswordFieldExample {  
    public static void main(String[] args) {    
    JFrame f=new JFrame("Password Field Example");    
     JPasswordField value = new JPasswordField();   
     JLabel l1=new JLabel("Password:");    
        l1.setBounds(20,100, 80,30);    
         value.setBounds(100,100,100,30);    
            f.add(value);  f.add(l1);  
            f.setSize(300,300);    
            f.setLayout(null);    
            f.setVisible(true);     
}  
}
Question: Can you provide a Java JPasswordField Example with ActionListener?
Answer: import javax.swing.*;    
import java.awt.event.*;  
public class PasswordFieldExample {  
    public static void main(String[] args) {    
    JFrame f=new JFrame("Password Field Example");    
     final JLabel label = new JLabel();            
     label.setBounds(20,150, 200,50);  
     final JPasswordField value = new JPasswordField();   
     value.setBounds(100,75,100,30);   
     JLabel l1=new JLabel("Username:");    
        l1.setBounds(20,20, 80,30);    
        JLabel l2=new JLabel("Password:");    
        l2.setBounds(20,75, 80,30);    
        JButton b = new JButton("Login");  
        b.setBounds(100,120, 80,30);    
        final JTextField text = new JTextField();  
        text.setBounds(100,20, 100,30);    
                f.add(value); f.add(l1); f.add(label); f.add(l2); f.add(b); f.add(text);  
                f.setSize(300,300);    
                f.setLayout(null);    
                f.setVisible(true);     
                b.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e) {       
                   String data = "Username " + text.getText();  
                   data += ", Password: "   
                   + new String(value.getPassword());   
                   label.setText(data);          
                }  
             });   
}  
}
Question: What is Java JCheckBox?
Answer: The JCheckBox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a CheckBox changes its state from "on" to "off" or from "off" to "on ".It inherits JToggleButton class.
Question: What is JCheckBox class declaration?
Answer: public class JCheckBox extends JToggleButton implements Accessible
Question: Can you provide a Java JCheckBox Example?
Answer: import javax.swing.*;  
public class CheckBoxExample  
{  
     CheckBoxExample(){  
        JFrame f= new JFrame("CheckBox Example");  
        JCheckBox checkBox1 = new JCheckBox("C++");  
        checkBox1.setBounds(100,100, 50,50);  
        JCheckBox checkBox2 = new JCheckBox("Java", true);  
        checkBox2.setBounds(100,150, 50,50);  
        f.add(checkBox1);  
        f.add(checkBox2);  
        f.setSize(400,400);  
        f.setLayout(null);  
        f.setVisible(true);  
     }  
public static void main(String args[])  
    {  
    new CheckBoxExample();  
    }}
Question: Can you provide a Java JCheckBox Example with ItemListener?
Answer: import javax.swing.*;  
import java.awt.event.*;    
public class CheckBoxExample    
{    
     CheckBoxExample(){    
        JFrame f= new JFrame("CheckBox Example");    
        final JLabel label = new JLabel();            
        label.setHorizontalAlignment(JLabel.CENTER);    
        label.setSize(400,100);    
        JCheckBox checkbox1 = new JCheckBox("C++");    
        checkbox1.setBounds(150,100, 50,50);    
        JCheckBox checkbox2 = new JCheckBox("Java");    
        checkbox2.setBounds(150,150, 50,50);    
        f.add(checkbox1); f.add(checkbox2); f.add(label);    
        checkbox1.addItemListener(new ItemListener() {    
             public void itemStateChanged(ItemEvent e) {                 
                label.setText("C++ Checkbox: "     
                + (e.getStateChange()==1?"checked":"unchecked"));    
             }    
          });    
        checkbox2.addItemListener(new ItemListener() {    
             public void itemStateChanged(ItemEvent e) {                 
                label.setText("Java Checkbox: "     
                + (e.getStateChange()==1?"checked":"unchecked"));    
             }    
          });    
        f.setSize(400,400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
public static void main(String args[])    
{    
    new CheckBoxExample();    
}    
}
Question: What is Java JRadioButton?
Answer: The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. It is widely used in exam systems or quiz.

It should be added in ButtonGroup to select one radio button only.
Question: What is JRadioButton class declaration?
Answer: public class JRadioButton extends JToggleButton implements Accessible
Question: Can you provide a Java JRadioButton Example?
Answer: import javax.swing.*;    
public class RadioButtonExample {    
JFrame f;    
RadioButtonExample(){    
f=new JFrame();     
JRadioButton r1=new JRadioButton("A) Male");    
JRadioButton r2=new JRadioButton("B) Female");    
r1.setBounds(75,50,100,30);    
r2.setBounds(75,100,100,30);    
ButtonGroup bg=new ButtonGroup();    
bg.add(r1);bg.add(r2);    
f.add(r1);f.add(r2);      
f.setSize(300,300);    
f.setLayout(null);    
f.setVisible(true);    
}    
public static void main(String[] args) {    
    new RadioButtonExample();    
}    
}
Question: Can you provide a Java JRadioButton Example with ActionListener?
Answer: import javax.swing.*;    
import java.awt.event.*;    
class RadioButtonExample extends JFrame implements ActionListener{    
JRadioButton rb1,rb2;    
JButton b;    
RadioButtonExample(){      
rb1=new JRadioButton("Male");    
rb1.setBounds(100,50,100,30);      
rb2=new JRadioButton("Female");    
rb2.setBounds(100,100,100,30);    
ButtonGroup bg=new ButtonGroup();    
bg.add(rb1);bg.add(rb2);    
b=new JButton("click");    
b.setBounds(100,150,80,30);    
b.addActionListener(this);    
add(rb1);add(rb2);add(b);    
setSize(300,300);    
setLayout(null);    
setVisible(true);    
}    
public void actionPerformed(ActionEvent e){    
if(rb1.isSelected()){    
JOptionPane.showMessageDialog(this,"You are Male.");    
}    
if(rb2.isSelected()){    
JOptionPane.showMessageDialog(this,"You are Female.");    
}    
}    
public static void main(String args[]){    
new RadioButtonExample();    
}}
Question: What is Java JComboBox?
Answer: 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.
Question: What is JComboBox class declaration?
Answer: public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible
Question: Can you provide an Example of Java JComboBox?
Answer: import javax.swing.*;    
public class ComboBoxExample {    
JFrame f;    
ComboBoxExample(){    
    f=new JFrame("ComboBox Example");    
    String country[]={"India","Aus","U.S.A","England","Newzealand"};        
    JComboBox cb=new JComboBox(country);    
    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 ComboBoxExample();         
}    
}
Question: Can you provide a Java JComboBox Example with ActionListener?
Answer: import javax.swing.*;    
import java.awt.event.*;    
public class ComboBoxExample {    
JFrame f;    
ComboBoxExample(){    
    f=new JFrame("ComboBox Example");   
    final JLabel label = new JLabel();          
    label.setHorizontalAlignment(JLabel.CENTER);  
    label.setSize(400,100);  
    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);       
    b.addActionListener(new ActionListener() {  
        public void actionPerformed(ActionEvent e) {       
String data = "Programming language Selected: "   
   + cb.getItemAt(cb.getSelectedIndex());  
label.setText(data);  
}  
});           
}    
public static void main(String[] args) {    
    new ComboBoxExample();         
}    
}
Question: What is Java JTable?
Answer: The JTable class is used to display data in tabular form. It is composed of rows and columns.
Question: What is JTable class declaration?
Answer: Constructor: JTable()
Description: Creates a table with empty cells.
Constructor: JTable(Object[][] rows, Object[] columns)
Description: Creates a table with the specified data.
Question: Can you provide an example of Java JTable?

Answer: import javax.swing.*;    
public class TableExample {    
    JFrame f;    
    TableExample(){    
    f=new JFrame();    
    String data[][]={ {"101","Amit","670000"},    
                          {"102","Jai","780000"},    
                          {"101","Sachin","700000"}};    
    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 TableExample();    
}    
}
Question: Java JTable Example with ListSelectionListener
Answer: import javax.swing.*;    
import javax.swing.event.*;  
public class TableExample {    
      public static void main(String[] a) {  
            JFrame f = new JFrame("Table Example");  
            String data[][]={ {"101","Amit","670000"},    
                                                                       {"102","Jai","780000"},    
                                                                       {"101","Sachin","700000"}};    
                            String column[]={"ID","NAME","SALARY"};         
                            final JTable jt=new JTable(data,column);    
            jt.setCellSelectionEnabled(true);  
            ListSelectionModel select= jt.getSelectionModel();  
            select.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
            select.addListSelectionListener(new ListSelectionListener() {  
              public void valueChanged(ListSelectionEvent e) {  
                String Data = null;  
                int[] row = jt.getSelectedRows();  
                int[] columns = jt.getSelectedColumns();  
                for (int i = 0; i < row.length; i++) {  
                  for (int j = 0; j < columns.length; j++) {  
                    Data = (String) jt.getValueAt(row[i], columns[j]);  
                  } }  
                System.out.println("Table element selected is: " + Data);    
              }       
            });  
            JScrollPane sp=new JScrollPane(jt);    
            f.add(sp);  
            f.setSize(300, 200);  
            f.setVisible(true);  
          }  
        }
Question: What is Java JList?
Answer: 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.
Question: What is JList class declaration?
Answer: public class JList extends JComponent implements Scrollable, Accessible
Question: Can you provide an example of Java JList?
Answer: import javax.swing.*;  
public class ListExample  
{  
     ListExample(){  
        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 ListExample();  
    }}
Question: Can you provid a Java JList Example with ActionListener?
Answer: import javax.swing.*;  
import java.awt.event.*;  
public class ListExample  
{  
     ListExample(){  
        JFrame f= new JFrame();  
        final JLabel label = new JLabel();          
        label.setSize(500,100);  
        JButton b=new JButton("Show");  
        b.setBounds(200,150,80,30);  
        final DefaultListModel<String> l1 = new DefaultListModel<>();  
          l1.addElement("C");  
          l1.addElement("C++");  
          l1.addElement("Java");  
          l1.addElement("PHP");  
          final JList<String> list1 = new JList<>(l1);  
          list1.setBounds(100,100, 75,75);  
          DefaultListModel<String> l2 = new DefaultListModel<>();  
          l2.addElement("Turbo C++");  
          l2.addElement("Struts");  
          l2.addElement("Spring");  
          l2.addElement("YII");  
          final JList<String> list2 = new JList<>(l2);  
          list2.setBounds(100,200, 75,75);  
          f.add(list1); f.add(list2); f.add(b); f.add(label);  
          f.setSize(450,450);  
          f.setLayout(null);  
          f.setVisible(true);  
          b.addActionListener(new ActionListener() {  
              public void actionPerformed(ActionEvent e) {   
                 String data = "";  
                 if (list1.getSelectedIndex() != -1) {                       
                    data = "Programming language Selected: " + list1.getSelectedValue();   
                    label.setText(data);  
                 }  
                 if(list2.getSelectedIndex() != -1){  
                    data += ", FrameWork Selected: ";  
                    for(Object frame :list2.getSelectedValues()){  
                       data += frame + " ";  
                    }  
                 }  
                 label.setText(data);  
              }  
           });   
     }  
public static void main(String args[])  
    {  
   new ListExample();  
    }}
Question: What is Java JOptionPane?
Answer: 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. The JOptionPane class inherits JComponent class.
Question: Can you provide an example of JOptionPane class declaration?
Answer: public class JOptionPane extends JComponent implements Accessible
Question: Can you provide an example of Java JOptionPane: showMessageDialog()?
Answer: import javax.swing.*;  
public class OptionPaneExample {  
JFrame f;  
OptionPaneExample(){  
    f=new JFrame();  
    JOptionPane.showMessageDialog(f,"Hello, Welcome to Javatpoint.");  
}  
public static void main(String[] args) {  
    new OptionPaneExample();  
}  
}
Question: Can you provide an example of Java JOptionPane: showInputDialog()?
Answer: import javax.swing.*;  
public class OptionPaneExample {  
JFrame f;  
OptionPaneExample(){  
    f=new JFrame();   
    String name=JOptionPane.showInputDialog(f,"Enter Name");      
}  
public static void main(String[] args) {  
    new OptionPaneExample();  
}  
}
Question: Can you provide an example of Java JOptionPane  showConfirmDialog()?
Answer: import javax.swing.*;  
import java.awt.event.*;  
public class OptionPaneExample extends WindowAdapter{  
JFrame f;  
OptionPaneExample(){  
    f=new JFrame();   
    f.addWindowListener(this);  
    f.setSize(300, 300);  
    f.setLayout(null);  
    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  
    f.setVisible(true);  
}  
public void windowClosing(WindowEvent e) {  
    int a=JOptionPane.showConfirmDialog(f,"Are you sure?");  
if(a==JOptionPane.YES_OPTION){  
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
}  
}  
public static void main(String[] args) {  
    new  OptionPaneExample();  
}     
}
Question: What is Java JScrollBar?
Answer: The object of JScrollbar class is used to add horizontal and vertical scrollbar. It is an implementation of a scrollbar. It inherits JComponent class.
Question: What is JScrollBar class declaration?
Answer: public class JScrollBar extends JComponent implements Adjustable, Accessible
Question: Can you provide an example of Java JScrollBar?
Answer: import javax.swing.*;  
class ScrollBarExample  
{  
ScrollBarExample(){  
    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 ScrollBarExample();  
}}
Question: Can you provide an example of Java JScrollBar with AdjustmentListener
Answer: import javax.swing.*;  
import java.awt.event.*;  
class ScrollBarExample  
{  
ScrollBarExample(){  
    JFrame f= new JFrame("Scrollbar Example");  
    final JLabel label = new JLabel();          
    label.setHorizontalAlignment(JLabel.CENTER);    
    label.setSize(400,100);  
    final JScrollBar s=new JScrollBar();  
    s.setBounds(100,100, 50,100);  
    f.add(s); f.add(label);  
    f.setSize(400,400);  
   f.setLayout(null);  
   f.setVisible(true);  
   s.addAdjustmentListener(new AdjustmentListener() {  
    public void adjustmentValueChanged(AdjustmentEvent e) {  
       label.setText("Vertical Scrollbar value is:"+ s.getValue());  
    }  
 });  
}  
public static void main(String args[])  
{  
   new ScrollBarExample();  
}}
Question: What are the Java JMenuBar, JMenu and JMenuItem?
Answer: The JMenuBar class is used to display menubar on the window or frame. It may have several menus.

The object of JMenu class is a pull down menu component which is displayed from the menu bar. It inherits the JMenuItem class.

The object of JMenuItem class adds a simple labeled menu item. The items used in a menu must belong to the JMenuItem or any of its subclass.
Question: What is JMenu class declaration?
Answer: public class JMenu extends JMenuItem implements MenuElement, Accessible
Question: What is JMenuItem class declaration?
Answer: public class JMenuItem extends AbstractButton implements Accessible, MenuElement
Question: Can you provide an example of Java JMenuItem and JMenu?
Answer: import javax.swing.*;  
class MenuExample  
{  
          JMenu menu, submenu;  
          JMenuItem i1, i2, i3, i4, i5;  
          MenuExample(){  
          JFrame f= new JFrame("Menu and MenuItem Example");  
          JMenuBar mb=new JMenuBar();  
          menu=new JMenu("Menu");  
          submenu=new JMenu("Sub Menu");  
          i1=new JMenuItem("Item 1");  
          i2=new JMenuItem("Item 2");  
          i3=new JMenuItem("Item 3");  
          i4=new JMenuItem("Item 4");  
          i5=new JMenuItem("Item 5");  
          menu.add(i1); menu.add(i2); menu.add(i3);  
          submenu.add(i4); submenu.add(i5);  
          menu.add(submenu);  
          mb.add(menu);  
          f.setJMenuBar(mb);  
          f.setSize(400,400);  
          f.setLayout(null);  
          f.setVisible(true);  
}  
public static void main(String args[])  
{  
new MenuExample();  
}}
Question: Can you provided an Example of creating Edit menu for Notepad?
Answer: import javax.swing.*;    
import java.awt.event.*;    
public class MenuExample implements ActionListener{    
JFrame f;    
JMenuBar mb;    
JMenu file,edit,help;    
JMenuItem cut,copy,paste,selectAll;    
JTextArea ta;    
MenuExample(){    
f=new JFrame();    
cut=new JMenuItem("cut");    
copy=new JMenuItem("copy");    
paste=new JMenuItem("paste");    
selectAll=new JMenuItem("selectAll");    
cut.addActionListener(this);    
copy.addActionListener(this);    
paste.addActionListener(this);    
selectAll.addActionListener(this);    
mb=new JMenuBar();    
file=new JMenu("File");    
edit=new JMenu("Edit");    
help=new JMenu("Help");     
edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);    
mb.add(file);mb.add(edit);mb.add(help);    
ta=new JTextArea();    
ta.setBounds(5,5,360,320);    
f.add(mb);f.add(ta);    
f.setJMenuBar(mb);  
f.setLayout(null);    
f.setSize(400,400);    
f.setVisible(true);    
}     
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 MenuExample();    
}    
}
Question: What is Java JPopupMenu?
Answer: PopupMenu can be dynamically popped up at specific position within a component. It inherits the JComponent class.
Question: What is JPopupMenu class declaration?
Answer: public class JPopupMenu extends JComponent implements Accessible, MenuElement
Question: Can you provide an example of Java JPopupMenu?
Answer: import javax.swing.*;  
import java.awt.event.*;  
class PopupMenuExample  
{  
     PopupMenuExample(){  
         final JFrame f= new JFrame("PopupMenu Example");  
         final JPopupMenu popupmenu = new JPopupMenu("Edit");   
         JMenuItem cut = new JMenuItem("Cut");  
         JMenuItem copy = new JMenuItem("Copy");  
         JMenuItem paste = new JMenuItem("Paste");  
         popupmenu.add(cut); popupmenu.add(copy); popupmenu.add(paste);        
         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 PopupMenuExample();  
}}
Question: Can you provide an example Java JPopupMenu with MouseListener and ActionListener?
Answer: import javax.swing.*;  
import java.awt.event.*;  
class PopupMenuExample   
{  
     PopupMenuExample(){  
         final JFrame f= new JFrame("PopupMenu Example");  
         final JLabel label = new JLabel();          
         label.setHorizontalAlignment(JLabel.CENTER);  
         label.setSize(400,100);  
         final JPopupMenu popupmenu = new JPopupMenu("Edit");   
         JMenuItem cut = new JMenuItem("Cut");  
         JMenuItem copy = new JMenuItem("Copy");  
         JMenuItem paste = new JMenuItem("Paste");  
         popupmenu.add(cut); popupmenu.add(copy); popupmenu.add(paste);        
         f.addMouseListener(new MouseAdapter() {  
            public void mouseClicked(MouseEvent e) {              
                popupmenu.show(f , e.getX(), e.getY());  
            }                 
         });  
        cut.addActionListener(new ActionListener(){  
         public void actionPerformed(ActionEvent e) {              
             label.setText("cut MenuItem clicked.");  
         }  
        });  
        copy.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e) {              
                label.setText("copy MenuItem clicked.");  
            }  
           });  
        paste.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e) {              
                label.setText("paste MenuItem clicked.");  
            }  
           });  
         f.add(label); f.add(popupmenu);   
         f.setSize(400,400);  
         f.setLayout(null);  
         f.setVisible(true);  
     }  
public static void main(String args[])  
{  
        new PopupMenuExample();  
}  
}
Question: What is Java JCheckBoxMenuItem?
Answer: JCheckBoxMenuItem class represents checkbox which can be included on a menu . A CheckBoxMenuItem can have text or a graphic icon or both, associated with it. MenuItem can be selected or deselected. MenuItems can be configured and controlled by actions.
Question: Can you provide a Java JCheckBoxMenuItem Example?
Answer: import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.awt.event.KeyEvent;  
import javax.swing.AbstractButton;  
import javax.swing.Icon;  
import javax.swing.JCheckBoxMenuItem;  
import javax.swing.JFrame;  
import javax.swing.JMenu;  
import javax.swing.JMenuBar;  
import javax.swing.JMenuItem;  
  
public class JavaCheckBoxMenuItem {  
    public static void main(final String args[]) {  
        JFrame frame = new JFrame("Jmenu Example");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        JMenuBar menuBar = new JMenuBar();  
        // File Menu, F - Mnemonic  
        JMenu fileMenu = new JMenu("File");  
        fileMenu.setMnemonic(KeyEvent.VK_F);  
        menuBar.add(fileMenu);  
        // File->New, N - Mnemonic  
        JMenuItem menuItem1 = new JMenuItem("Open", KeyEvent.VK_N);  
        fileMenu.add(menuItem1);  
  
        JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Option_1");  
        caseMenuItem.setMnemonic(KeyEvent.VK_C);  
        fileMenu.add(caseMenuItem);  
  
        ActionListener aListener = new ActionListener() {  
            public void actionPerformed(ActionEvent event) {  
                AbstractButton aButton = (AbstractButton) event.getSource();  
                boolean selected = aButton.getModel().isSelected();  
                String newLabel;  
                Icon newIcon;  
                if (selected) {  
                    newLabel = "Value-1";  
                } else {  
                    newLabel = "Value-2";  
                }  
                aButton.setText(newLabel);  
            }  
        };  
  
        caseMenuItem.addActionListener(aListener);  
        frame.setJMenuBar(menuBar);  
        frame.setSize(350, 250);  
        frame.setVisible(true);  
    }  
}
Question: What is Java JSeparator?
Answer: The object of JSeparator class is used to provide a general purpose component for implementing divider lines. It is used to draw a line to separate widgets in a Layout. It inherits JComponent class.
Question: What is JSeparator class declaration?
Answer: public class JSeparator extends JComponent implements SwingConstants, Accessible
Question: Can you provide an example of Java JSeparator?
Answer: import javax.swing.*;    
class SeparatorExample    
{    
          JMenu menu, submenu;    
          JMenuItem i1, i2, i3, i4, i5;    
          SeparatorExample()  {    
          JFrame f= new JFrame("Separator Example");    
          JMenuBar mb=new JMenuBar();    
          menu=new JMenu("Menu");    
          i1=new JMenuItem("Item 1");    
          i2=new JMenuItem("Item 2");       
          menu.add(i1);  
          menu.addSeparator();  
          menu.add(i2);  
          mb.add(menu);    
          f.setJMenuBar(mb);    
          f.setSize(400,400);    
          f.setLayout(null);    
          f.setVisible(true);    
}    
public static void main(String args[])    
{    
new SeparatorExample();    
}}
Question: What is Java JProgressBar?
Answer: The JProgressBar class is used to display the progress of the task. It inherits JComponent class.
Question: What is JProgressBar class declaration?
Answer: public class JProgressBar extends JComponent implements SwingConstants, Accessible
Question: Can you provide an example of Java JProgressBar?
Answer: import javax.swing.*;    
public class ProgressBarExample extends JFrame{    
JProgressBar jb;    
int i=0,num=0;     
ProgressBarExample(){    
jb=new JProgressBar(0,2000);    
jb.setBounds(40,40,160,30);         
jb.setValue(0);    
jb.setStringPainted(true);    
add(jb);    
setSize(250,150);    
setLayout(null);    
}    
public void iterate(){    
while(i<=2000){    
  jb.setValue(i);    
  i=i+20;    
  try{Thread.sleep(150);}catch(Exception e){}    
}    
}    
public static void main(String[] args) {    
    ProgressBarExample m=new ProgressBarExample();    
    m.setVisible(true);    
    m.iterate();    
}    
}
Question: What is Java JTree?
Answer: The JTree class is used to display the tree structured data or hierarchical data. JTree is a complex component. It has a 'root node' at the top most which is a parent for all nodes in the tree. It inherits JComponent class.
Question: What is JTree class declaration?
Answer: public class JTree extends JComponent implements Scrollable, Accessible
Question: Can you provide an example of Java JTree?
Answer: import javax.swing.*;  
import javax.swing.tree.DefaultMutableTreeNode;  
public class TreeExample {  
JFrame f;  
TreeExample(){  
    f=new JFrame();   
    DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style");  
    DefaultMutableTreeNode color=new DefaultMutableTreeNode("color");  
    DefaultMutableTreeNode font=new DefaultMutableTreeNode("font");  
    style.add(color);  
    style.add(font);  
    DefaultMutableTreeNode red=new DefaultMutableTreeNode("red");  
    DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");  
    DefaultMutableTreeNode black=new DefaultMutableTreeNode("black");  
    DefaultMutableTreeNode green=new DefaultMutableTreeNode("green");  
    color.add(red); color.add(blue); color.add(black); color.add(green);      
    JTree jt=new JTree(style);  
    f.add(jt);  
    f.setSize(200,200);  
    f.setVisible(true);  
}  
public static void main(String[] args) {  
    new TreeExample();  
}}
Question: What is Java JColorChooser?
Answer: The JColorChooser class is used to create a color chooser dialog box so that user can select any color. It inherits JComponent class.
Question: What is JColorChooser class declaration?
Answer: public class JColorChooser extends JComponent implements Accessible
Question: Can you provide an example of Java JColorChooser?
Answer: import java.awt.event.*;    
import java.awt.*;    
import javax.swing.*;     
public class ColorChooserExample extends JFrame implements ActionListener {    
JButton b;    
Container c;    
ColorChooserExample(){    
    c=getContentPane();    
    c.setLayout(new FlowLayout());         
    b=new JButton("color");    
    b.addActionListener(this);         
    c.add(b);    
}    
public void actionPerformed(ActionEvent e) {    
Color initialcolor=Color.RED;    
Color color=JColorChooser.showDialog(this,"Select a color",initialcolor);    
c.setBackground(color);    
}    
    
public static void main(String[] args) {    
    ColorChooserExample ch=new ColorChooserExample();    
    ch.setSize(400,400);    
    ch.setVisible(true);    
    ch.setDefaultCloseOperation(EXIT_ON_CLOSE);    
}    
}
Question: Can you provide an example in Java JColorChooser with ActionListener?
Answer: import javax.swing.*;  
import java.awt.*;  
import java.awt.event.*;  
public class ColorChooserExample extends JFrame implements ActionListener{  
JFrame f;  
JButton b;  
JTextArea ta;  
ColorChooserExample(){  
    f=new JFrame("Color Chooser Example.");  
    b=new JButton("Pad Color");  
    b.setBounds(200,250,100,30);  
    ta=new JTextArea();  
    ta.setBounds(10,10,300,200);  
    b.addActionListener(this);  
    f.add(b);f.add(ta);  
    f.setLayout(null);  
    f.setSize(400,400);  
    f.setVisible(true);  
}  
public void actionPerformed(ActionEvent e){  
    Color c=JColorChooser.showDialog(this,"Choose",Color.CYAN);  
    ta.setBackground(c);  
}  
public static void main(String[] args) {  
    new ColorChooserExample();  
}  
}
Question: What is Java JTabbedPane?
Answer: The JTabbedPane class is used to switch between a group of components by clicking on a tab with a given title or icon. It inherits JComponent class.
Question: What is JTabbedPane class declaration?
Answer: public class JTabbedPane extends JComponent implements Serializable, Accessible, SwingConstants
Question: Can you provide an example Java JTabbedPane?
Answer: import javax.swing.*;  
public class TabbedPaneExample {  
JFrame f;  
TabbedPaneExample(){  
    f=new JFrame();  
    JTextArea ta=new JTextArea(200,200);  
    JPanel p1=new JPanel();  
    p1.add(ta);  
    JPanel p2=new JPanel();  
    JPanel p3=new JPanel();  
    JTabbedPane tp=new JTabbedPane();  
    tp.setBounds(50,50,200,200);  
    tp.add("main",p1);  
    tp.add("visit",p2);  
    tp.add("help",p3);    
    f.add(tp);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);  
}  
public static void main(String[] args) {  
    new TabbedPaneExample();  
}}
Question: What is Java JSlider?
Answer: The Java JSlider class is used to create the slider. By using JSlider, a user can select a value from a specific range.
Question: Can you provide an example of Java JSlider?
Answer: import javax.swing.*;  
public class SliderExample1 extends JFrame{  
public SliderExample1() {  
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);  
JPanel panel=new JPanel();  
panel.add(slider);  
add(panel);  
}  
  
public static void main(String s[]) {  
SliderExample1 frame=new SliderExample1();  
frame.pack();  
frame.setVisible(true);  
}  
}
Question: Can you provide an example of Java JSlider: painting ticks?
Answer: import javax.swing.*;  
public class SliderExample extends JFrame{  
public SliderExample() {  
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);  
slider.setMinorTickSpacing(2);  
slider.setMajorTickSpacing(10);  
slider.setPaintTicks(true);  
slider.setPaintLabels(true);  
  
JPanel panel=new JPanel();  
panel.add(slider);  
add(panel);  
}  
public static void main(String s[]) {  
SliderExample frame=new SliderExample();  
frame.pack();  
frame.setVisible(true);  
}  
}
Question: What is Java JSpinner?
Answer: The object of JSpinner class is a single line input field that allows the user to select a number or an object value from an ordered sequence.
Question: What is JSpinner class declaration?
Answer: public class JSpinner extends JComponent implements Accessible
Question: Can you provide an example of Java JSpinner?
Answer: import javax.swing.*;    
public class SpinnerExample {  
    public static void main(String[] args) {    
    JFrame f=new JFrame("Spinner Example");    
    SpinnerModel value =  
             new SpinnerNumberModel(5, //initial value  
                0, //minimum value  
                10, //maximum value  
                1); //step  
    JSpinner spinner = new JSpinner(value);   
            spinner.setBounds(100,100,50,30);    
            f.add(spinner);    
            f.setSize(300,300);    
            f.setLayout(null);    
            f.setVisible(true);     
}  
}
Question: Can you provide an example of Java JSpinner with ChangeListener?
Answer: import javax.swing.*;    
import javax.swing.event.*;  
public class SpinnerExample {  
    public static void main(String[] args) {    
    JFrame f=new JFrame("Spinner Example");    
    final JLabel label = new JLabel();            
             label.setHorizontalAlignment(JLabel.CENTER);    
             label.setSize(250,100);    
    SpinnerModel value =  
             new SpinnerNumberModel(5, //initial value  
                0, //minimum value  
                10, //maximum value  
                1); //step  
    JSpinner spinner = new JSpinner(value);   
            spinner.setBounds(100,100,50,30);    
            f.add(spinner);  f.add(label);  
           f.setSize(300,300);    
           f.setLayout(null);    
           f.setVisible(true);     
           spinner.addChangeListener(new ChangeListener() {  
        public void stateChanged(ChangeEvent e) {  
         label.setText("Value : " + ((JSpinner)e.getSource()).getValue());  
        }  
     });  
}  
}
Question: What is Java JDialog?
Answer: The JDialog control represents a top level window with a border and a title used to take some form of input from the user. It inherits the Dialog class.

Unlike JFrame, it doesn't have maximize and minimize buttons.
Question: What is JDialog class declaration?
Answer: public class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneContainer