hereral/Clara-Training-Data huggingface dataset:
Question: Can you provide an example of Java JDialog?
Answer: import javax.swing.*;  
import java.awt.*;  
import java.awt.event.*;  
public class DialogExample {  
    private static JDialog d;  
    DialogExample() {  
        JFrame f= new JFrame();  
        d = new JDialog(f , "Dialog Example", true);  
        d.setLayout( new FlowLayout() );  
        JButton b = new JButton ("OK");  
        b.addActionListener ( new ActionListener()  
        {  
            public void actionPerformed( ActionEvent e )  
            {  
                DialogExample.d.setVisible(false);  
            }  
        });  
        d.add( new JLabel ("Click button to continue."));  
        d.add(b);   
        d.setSize(300,300);    
        d.setVisible(true);  
    }  
    public static void main(String args[])  
    {  
        new DialogExample();  
    }  
}
Question: What is Java JPanel?
Answer: The JPanel is a simplest container class. It provides space in which an application can attach any other component. It inherits the JComponents class.

It doesn't have title bar.
Question: What is JPanel class declaration?
Answer: public class JPanel extends JComponent implements Accessible
Question: Can you provide an example of Java JPanel?
Answer: import java.awt.*;  
import javax.swing.*;  
public class PanelExample {  
     PanelExample()  
        {  
        JFrame f= new JFrame("Panel Example");    
        JPanel panel=new JPanel();  
        panel.setBounds(40,80,200,200);    
        panel.setBackground(Color.gray);  
        JButton b1=new JButton("Button 1");     
        b1.setBounds(50,100,80,30);    
        b1.setBackground(Color.yellow);   
        JButton b2=new JButton("Button 2");   
        b2.setBounds(100,100,80,30);    
        b2.setBackground(Color.green);   
        panel.add(b1); panel.add(b2);  
        f.add(panel);  
                f.setSize(400,400);    
                f.setLayout(null);    
                f.setVisible(true);    
        }  
        public static void main(String args[])  
        {  
        new PanelExample();  
        }  
    }
Question: What is Java JFileChooser?
Answer: JFileChooser is a class that is present in the java Swing package. The java Swing package is essential for JavaTM Foundation Classes(JFC). JFileChooser contains many elements that assist in building a graphical user Interface in java. Java Swing gives components like buttons, panels, dialogs, etc. JFileChooser is a simple and successful method for inciting the client to pick a file or a directory. JFileChooser inherited the properties of JComponent class and implemented them with an Accessible interface.
Question: Can you provide an example of Constructors Present in Java JFileChooser Class?
Answer: 1. JFileChooser():

Constructs a JFileChooser highlighting the client's default directory.

Example program on JFileChooser()

// program to demonstrate the JFileChooser() constructor  
// importing the required packages  
import java.io.*;  
importjavax.swing.*;  
importjava.awt.event.*;  
importjavax.swing.filechooser.*;  
classHelloWorld  
{  
public static void main(String[] args) {  
        // creating object to the JFileChooser class  
        JFileChooserjf = new JFileChooser(); // default constructor JFileChooser is called.  
        jf.showSaveDialog(null);  
    }  
}
Question: What is JFileChooser():?
Answer: Constructs a JFileChooser highlighting the client's default directory.

Example program on JFileChooser()

// program to demonstrate the JFileChooser() constructor  
// importing the required packages  
import java.io.*;  
importjavax.swing.*;  
importjava.awt.event.*;  
importjavax.swing.filechooser.*;  
classHelloWorld  
{  
public static void main(String[] args) {  
        // creating object to the JFileChooser class  
        JFileChooserjf = new JFileChooser(); // default constructor JFileChooser is called.  
        jf.showSaveDialog(null);  
    }  
}
Question: Can you provide an example ofJFileChooser(File currentDirectory):?
Answer: Constructs a JFileChooser involving the given File as the way.

Example program on JFileChooser(File currentDirectory)

// program to demonstrate the JFileChooser(File currentDirectory) constructor  
// importing the required packages  
import java.io.*;  
importjavax.swing.*;  
importjava.awt.event.*;  
importjavax.swing.filechooser.*;  
classHelloWorld  
{  
public static void main(String[] args) {  
        // creating object to the JFileChooser class  
        JFileChooserjf = new JFileChooser("c:"); // parameterised constructor JFileChooser( File currentDirectory) is called.  
        jf.showSaveDialog(null); // opening the saved dialogue  
    }  
}  .
Question: Can you provide an example of JFileChooser(File currentDirectory, FileSystemViewfsv):?
Answer: Constructs a JFileChooser utilizing the given current catalog and FileSystemView.

Example program on JFileChooser(File currentDirectory, FileSystemViewfsv)

// program to demonstrate the JFileChooser(File currentDirectory, FileSystemViewfsv) constructor  
// importing the required packages  
import java.io.*;  
importjavax.swing.*;  
importjava.awt.event.*;  
importjavax.swing.filechooser.*;  
classHelloWorld  
{  
public static void main(String[] args) {  
        // creating object to the JFileChooser class  
        JFileChooserjf = new JFileChooser("c:", FileSystemView.getFileSystemView()); // parameterised constructor JFileChooser(File currentDirectory, FileSystemViewfsv) is called.  
        jf.showSaveDialog(null); // opening the saved dialogue  
    }  
}
Question: Can you provide an example of JFileChooser(FileSystemViewfsv):?
Answer: Constructs a JFileChooser utilizing the given FileSystemView.

Example program on JFileChooser(FileSystemViewfsv)

// program to demonstrate the JFileChooser(FileSystemViewfsv) constructor  
// importing the required packages  
import java.io.*;  
importjavax.swing.*;  
importjava.awt.event.*;  
importjavax.swing.filechooser.*;  
classHelloWorld  
{  
public static void main(String[] args) {  
        // creating object to the JFileChooser class  
        JFileChooserjf = new JFileChooser(FileSystemView.getFileSystemView()); // parameterised constructor JFileChooser(FileSystemViewfsv) is called.  
        jf.showSaveDialog(null); // opening the saved dialogue  
    }  
}
Question: Can you provide an example of JFileChooser(String currentDirectoryPath):?
Answer: Constructs a JFileChooser utilizing the given way.

Example program on JFileChooser(String currentDirectoryPath):

// program to demonstrate the JFileChooser(String currentDirectoryPath) constructor  
// importing the required packages  
import java.io.*;  
importjavax.swing.*;  
importjava.awt.event.*;  
importjavax.swing.filechooser.*;  
classHelloWorld  
{  
public static void main(String[] args) {  
        // creating object to the JFileChooser class  
        JFileChooserjf = new JFileChooser("C:\Altaf zameer"); // parameterised constructor JFileChooser(string current DirectoryPath) is called.  
        jf.showSaveDialog(null); // opening the saved dialogue  
    }  
}
Question: Can you provide an example of JFileChooser(String currentDirectoryPath, FileSystemViewfsv):?
Answer: Constructs a JFileChooser utilizing the given current catalog way and FileSystemView.

Example program on JFileChooser( String currentDirectoryPath, FileSystemViewfsv ):

// program to demonstrate the JFileChooser( String currentDirectoryPath, FileSystemViewfsv ) constructor  
// importing the required packages  
import java.io.*;  
importjavax.swing.*;  
importjava.awt.event.*;  
importjavax.swing.filechooser.*;  
classHelloWorld  
{  
    public static void main(String[] args) {  
            // creating object to the JFileChooser class  
JFileChooserjf = new JFileChooser("C:", FileSystemView.getFileSystemView());// parameterised constructor JFileChooser( String currentDirectoryPath, FileSystemViewfsv ) is called.  
jf.showSaveDialog(null); // opening the saved dialogue  
    }  
}
Question: Can you provide an example of JFileChooser( File ):?
Answer: Example program on JFileChooser( File ) constructor:

// program to demonstrate the JFileChooser( file ) constructor  
// importing the required packages  
import java.io.*;  
importjavax.swing.*;  
importjava.awt.event.*;  
importjavax.swing.filechooser.*;  
classHelloWorld  
{  
public static void main(String[] args) {  
        // creating object to the JFileChooser class  
        JFileChooserjf = new JFileChooser(new File(" C:\\Users "));// parameterised constructor JFileChooser( file ) is called.  
        jf.showSaveDialog(null); // opening the saved dialogue  
    }  
}
Question: Can you provide an example of JFileChooser(File, FileSystemView):?
Answer: Example on JFileChooser( File, FileSystemView )

// program to demonstrate the JFileChooser( file, FileSystemView ) constructor  
// importing the required packages  
import java.io.*;  
importjavax.swing.*;  
importjava.awt.event.*;  
importjavax.swing.filechooser.*;  
classHelloWorld  
{  
public static void main(String[] args) {  
        // creating object to the JFileChooser class  
        // File class  
        File f = new File("C:\\Altafzameer\\");  
        JFileChooserjf = new JFileChooser(f, FileSystemView.getFileSystemView());// parameterised constructor JFileChooser( file ) is called.  
        jf.showSaveDialog(null); // opening the saved dialogue  
    }  
}
Question: What are the Advantages of JFileChooser()?

Answer: 1.Statement of the JFileChooser() beyond the occasion audience additionally can be used within the occasion audience.
2.JFileChooser return esteem, which portrays regardless of whether the record has been picked.
3.Boundary given to the accompanying strategy for JFileChooser can confine clients effectively to choose either record or envelope or both.
4.The JFileChooser() declaration made external to the event listener can moreover be utilized inside the event listener.
5.The JFileChooser return result shows whether the file has been chosen.
6.The accompanying JFileChooser technique's boundary permits clients to handily be limited from choosing either a file, a folder, or both.
Question: Can you give me Methods in JFileChooser Class?
Answer: The JFileChooser class fundamentally gives three strategies that show the record chooser discoursed, as in the accompanying:
Question: what is showOpenDialog(component owner ):?
Answer: 1. showOpenDialog(component owner ): This technique opens an exchange box that contains an endorsement button, as such, "Open", that opens a document from the discourse.

Syntax:

intshowOpenDialog(component proprietor);
Question: What is showSaveDialog( component owner ):?
Answer: 2. showSaveDialog( component owner ): This strategy is utilized when the client needs to save the particular document that he/she is utilizing. This exchange box contains an endorsement button, as such, "Save", to save the document.

Syntax:

intshowSaveDialog(component proprietor);
Question: What is showDialog(component owner, string):?
Answer: 3. showDialog(component owner, string): This technique shows an exchange box with an endorsement button which is client characterized.

Syntax:

intshowDialog(component owner,string);
Question: What is getSelectedFile(): ?
Answer: 4. getSelectedFile(): This technique returns the document that is chosen by the client.

Syntax:

filegetSelectedFile();
Question: What is setCurrentDirectory(File f):?
Answer: 5. setCurrentDirectory(File f): This technique sets the ongoing catalog as determined.

Syntax:

voidsetCurrentDirectory(File f);
Question: What is getCurrentDirectory()?
Answer: 6. getCurrentDirectory(): This technique for JFileChooser returns the ongoing index as chosen by the client.

Syntax:

filegetCurrentDirectory();
Question: What is (File f)?
Answer: 7. (File f):This strategy returns the name of the document as given by the record contention.

Syntax:

String getName(File f);
Question: What is Java JToggleButton?
Answer: JToggleButton is used to create toggle button, it is two-states button to switch on or off.
Question: Can you provide an example of JToggleButton ?
Answer: import java.awt.FlowLayout;  
import java.awt.event.ItemEvent;  
import java.awt.event.ItemListener;  
import javax.swing.JFrame;  
import javax.swing.JToggleButton;  
  
public class JToggleButtonExample extends JFrame implements ItemListener {  
    public static void main(String[] args) {  
        new JToggleButtonExample();  
    }  
    private JToggleButton button;  
    JToggleButtonExample() {  
        setTitle("JToggleButton with ItemListener Example");  
        setLayout(new FlowLayout());  
        setJToggleButton();  
        setAction();  
        setSize(200, 200);  
        setVisible(true);  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }  
    private void setJToggleButton() {  
        button = new JToggleButton("ON");  
        add(button);  
    }  
    private void setAction() {  
        button.addItemListener(this);  
    }  
    public void itemStateChanged(ItemEvent eve) {  
        if (button.isSelected())  
            button.setText("OFF");  
        else  
            button.setText("ON");  
    }  
}
Question: What is Java JToolBar?
Answer: JToolBar container allows us to group other components, usually buttons with icons in a row or column. JToolBar provides a component which is useful for displaying commonly used actions or controls.
Question: Can you provide an example of Java JToolBar?
Answer: import java.awt.BorderLayout;  
import java.awt.Container;  
import javax.swing.JButton;  
import javax.swing.JComboBox;  
import javax.swing.JFrame;  
import javax.swing.JScrollPane;  
import javax.swing.JTextArea;  
import javax.swing.JToolBar;  
  
public class JToolBarExample {  
    public static void main(final String args[]) {  
        JFrame myframe = new JFrame("JToolBar Example");  
        myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        JToolBar toolbar = new JToolBar();  
        toolbar.setRollover(true);  
        JButton button = new JButton("File");  
        toolbar.add(button);  
        toolbar.addSeparator();  
        toolbar.add(new JButton("Edit"));  
        toolbar.add(new JComboBox(new String[] { "Opt-1", "Opt-2", "Opt-3", "Opt-4" }));  
        Container contentPane = myframe.getContentPane();  
        contentPane.add(toolbar, BorderLayout.NORTH);  
        JTextArea textArea = new JTextArea();  
        JScrollPane mypane = new JScrollPane(textArea);  
        contentPane.add(mypane, BorderLayout.EAST);  
        myframe.setSize(450, 250);  
        myframe.setVisible(true);  
    }  
}
Question: What is Java JViewport?
Answer: The JViewport class is used to implement scrolling. JViewport is designed to support both logical scrolling and pixel-based scrolling. The viewport's child, called the view, is scrolled by calling the JViewport.setViewPosition() method.
Question: Can you provide an example of JViewport?
Answer: import java.awt.BorderLayout;  
import java.awt.Color;  
import java.awt.Dimension;  
import javax.swing.JButton;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JScrollPane;  
import javax.swing.border.LineBorder;  
public class ViewPortClass2 {  
    public static void main(String[] args) {  
        JFrame frame = new JFrame("Tabbed Pane Sample");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  
        JLabel label = new JLabel("Label");  
        label.setPreferredSize(new Dimension(1000, 1000));  
        JScrollPane jScrollPane = new JScrollPane(label);  
  
        JButton jButton1 = new JButton();  
        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);  
        jScrollPane.setViewportBorder(new LineBorder(Color.RED));  
        jScrollPane.getViewport().add(jButton1, null);  
  
        frame.add(jScrollPane, BorderLayout.CENTER);  
        frame.setSize(400, 150);  
        frame.setVisible(true);  
    }  
}
Question: What is Java JFrame?
Answer: The javax.swing.JFrame class is a type of container which inherits the java.awt.Frame class. JFrame works like the main window where components like labels, buttons, textfields are added to create a GUI.

Unlike Frame, JFrame has the option to hide or close the window with the help of setDefaultCloseOperation(int) method.
Question: Can you provide an example of JFrame?
Answer: import java.awt.FlowLayout;  
import javax.swing.JButton;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JPanel;  
public class JFrameExample {  
    public static void main(String s[]) {  
        JFrame frame = new JFrame("JFrame Example");  
        JPanel panel = new JPanel();  
        panel.setLayout(new FlowLayout());  
        JLabel label = new JLabel("JFrame By Example");  
        JButton button = new JButton();  
        button.setText("Button");  
        panel.add(label);  
        panel.add(button);  
        frame.add(panel);  
        frame.setSize(200, 300);  
        frame.setLocationRelativeTo(null);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setVisible(true);  
    }  
}
Question: What is Java JComponent?
Answer: The JComponent class is the base class of all Swing components except top-level containers. Swing components whose names begin with "J" are descendants of the JComponent class. For example, JButton, JScrollPane, JPanel, JTable etc. But, JFrame and JDialog don't inherit JComponent class because they are the child of top-level containers.

The JComponent class extends the Container class which itself extends Component. The Container class has support for adding components to the container.
Question: Can you provide an example of Java JComponent?
Answer: import java.awt.Color;  
import java.awt.Graphics;  
import javax.swing.JComponent;  
import javax.swing.JFrame;  
class MyJComponent extends JComponent {  
      public void paint(Graphics g) {  
        g.setColor(Color.green);  
        g.fillRect(30, 30, 100, 100);  
      }  
}  
public class JComponentExample {  
      public static void main(String[] arguments) {  
        MyJComponent com = new MyJComponent();  
        // create a basic JFrame  
        JFrame.setDefaultLookAndFeelDecorated(true);  
        JFrame frame = new JFrame("JComponent Example");  
        frame.setSize(300,200);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        // add the JComponent to main frame  
        frame.add(com);  
        frame.setVisible(true);  
      }  
}
Question: What is Java JLayeredPane?
Answer: The JLayeredPane class is used to add depth to swing container. It is used to provide a third dimension for positioning component and divide the depth-range into several different layers.
Question: What is JLayeredPane class declaration?
Answer: public class JLayeredPane extends JComponent implements Accessible
Question: Can you provide an example of Java JLayeredPane?
Answer: import javax.swing.*;  
import java.awt.*;  
public class LayeredPaneExample extends JFrame {  
  public LayeredPaneExample() {  
    super("LayeredPane Example");  
    setSize(200, 200);  
    JLayeredPane pane = getLayeredPane();  
    //creating buttons  
    JButton top = new JButton();  
    top.setBackground(Color.white);  
    top.setBounds(20, 20, 50, 50);  
    JButton middle = new JButton();  
    middle.setBackground(Color.red);  
    middle.setBounds(40, 40, 50, 50);  
    JButton bottom = new JButton();  
    bottom.setBackground(Color.cyan);  
    bottom.setBounds(60, 60, 50, 50);  
    //adding buttons on pane  
    pane.add(bottom, new Integer(1));  
    pane.add(middle, new Integer(2));  
    pane.add(top, new Integer(3));  
  }  
  public static void main(String[] args) {  
      LayeredPaneExample panel = new  LayeredPaneExample();  
      panel.setVisible(true);  
  }  
}
Question: What is Java JDesktopPane?
Answer: The JDesktopPane class, can be used to create "multi-document" applications. A multi-document application can have many windows included in it. We do it by making the contentPane in the main window as an instance of the JDesktopPane class or a subclass. Internal windows add instances of JInternalFrame to the JdesktopPane instance. The internal windows are the instances of JInternalFrame or its subclasses.
Question: Can you provide an example of Java JDesktopPane?
Answer: import java.awt.BorderLayout;  
import java.awt.Container;  
import javax.swing.JDesktopPane;  
import javax.swing.JFrame;  
import javax.swing.JInternalFrame;  
import javax.swing.JLabel;  
public class JDPaneDemo extends JFrame  
{  
  public JDPaneDemo()   
  {  
    CustomDesktopPane desktopPane = new CustomDesktopPane();  
    Container contentPane = getContentPane();  
    contentPane.add(desktopPane, BorderLayout.CENTER);  
    desktopPane.display(desktopPane);  
  
    setTitle("JDesktopPane Example");  
    setSize(300,350);  
    setVisible(true);  
  }  
  public static void  main(String args[])  
  {  
    new JDPaneDemo();  
  }  
}  
class CustomDesktopPane extends JDesktopPane  
{  
  int numFrames = 3,  x = 30, y = 30;  
  public void display(CustomDesktopPane dp)   
  {  
    for(int  i = 0; i < numFrames ; ++i )   
    {  
      JInternalFrame jframe = new JInternalFrame("Internal Frame " + i ,  true, true, true, true);  
  
      jframe.setBounds(x, y, 250, 85);  
     Container c1 = jframe.getContentPane( ) ;  
     c1.add(new JLabel("I love my country"));  
     dp.add( jframe );  
     jframe.setVisible(true);         
     y += 85;  
    }  
  }  
}
Question: What is Java JEditorPane?
Answer: JEditorPane class is used to create a simple text editor window. This class has setContentType() and setText() methods.
Question: What is setContentType("text/plain")?
Answer: setContentType("text/plain"): This method is used to set the content type to be plain text.
Question: What is setText(text)?
Answer: setText(text): This method is used to set the initial text content.
Question: Can you provide an example of JEditorPane?
Answer: import javax.swing.JEditorPane;  
import javax.swing.JFrame;  
  
public class JEditorPaneExample {  
    JFrame myFrame = null;  
  
    public static void main(String[] a) {  
        (new JEditorPaneExample()).test();  
    }  
  
    private void test() {  
        myFrame = new JFrame("JEditorPane Test");  
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        myFrame.setSize(400, 200);  
        JEditorPane myPane = new JEditorPane();  
        myPane.setContentType("text/plain");  
        myPane.setText("Sleeping is necessary for a healthy body."  
                + " But sleeping in unnecessary times may spoil our health, wealth and studies."  
                + " Doctors advise that the sleeping at improper timings may lead for obesity during the students days.");  
        myFrame.setContentPane(myPane);  
        myFrame.setVisible(true);  
    }  
}
Question: What is Java JScrollPane?
Answer: A JscrollPane is used to make scrollable view of a component. When screen size is limited, we use a scroll pane to display a large component or a component whose size can change dynamically.
Question: Can you provide an example of JScrollPane?
Answer: import java.awt.FlowLayout;  
import javax.swing.JFrame;  
import javax.swing.JScrollPane;  
import javax.swing.JtextArea;  
  
public class JScrollPaneExample {  
    private static final long serialVersionUID = 1L;  
  
    private static void createAndShowGUI() {  
  
        // Create and set up the window.  
        final JFrame frame = new JFrame("Scroll Pane Example");  
  
        // Display the window.  
        frame.setSize(500, 500);  
        frame.setVisible(true);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  
        // set flow layout for the frame  
        frame.getContentPane().setLayout(new FlowLayout());  
  
        JTextArea textArea = new JTextArea(20, 20);  
        JScrollPane scrollableTextArea = new JScrollPane(textArea);  
  
        scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
        scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);  
  
        frame.getContentPane().add(scrollableTextArea);  
    }  
    public static void main(String[] args) {  
  
  
        javax.swing.SwingUtilities.invokeLater(new Runnable() {  
  
            public void run() {  
                createAndShowGUI();  
            }  
        });  
    }  
}
Question: What is Java JSplitPane?
Answer: JSplitPane is used to divide two components. The two components are divided based on the look and feel implementation, and they can be resized by the user. If the minimum size of the two components is greater than the size of the split pane, the divider will not allow you to resize it.

The two components in a split pane can be aligned left to right using JSplitPane.HORIZONTAL_SPLIT, or top to bottom using JSplitPane.VERTICAL_SPLIT. When the user is resizing the components the minimum size of the components is used to determine the maximum/minimum position the components can be set to.
Question: Can you provide an example of JSplitPane?
Answer: import java.awt.FlowLayout;  
import java.awt.Panel;  
import javax.swing.JComboBox;  
import javax.swing.JFrame;  
import javax.swing.JSplitPane;  
public class JSplitPaneExample {  
    private static void createAndShow() {  
        // Create and set up the window.  
        final JFrame frame = new JFrame("JSplitPane Example");  
        // Display the window.  
        frame.setSize(300, 300);  
        frame.setVisible(true);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        // set flow layout for the frame  
        frame.getContentPane().setLayout(new FlowLayout());  
        String[] option1 = { "A","B","C","D","E" };  
        JComboBox box1 = new JComboBox(option1);  
        String[] option2 = {"1","2","3","4","5"};  
        JComboBox box2 = new JComboBox(option2);  
        Panel panel1 = new Panel();  
        panel1.add(box1);  
        Panel panel2 = new Panel();  
        panel2.add(box2);  
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel2);  
        // JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,  
        // panel1, panel2);  
        frame.getContentPane().add(splitPane);  
    }  
    public static void main(String[] args) {  
        // Schedule a job for the event-dispatching thread:  
        // creating and showing this application's GUI.  
        javax.swing.SwingUtilities.invokeLater(new Runnable() {  
            public void run() {  
                createAndShow();  
            }  
        });  
    }  
}
Question: What is Java JTextPane?
Answer: JTextPane is a subclass of JEditorPane class. JTextPane is used for styled document with embedded images and components. It is text component that can be marked up with attributes that are represented graphically. JTextPane uses a DefaultStyledDocument as its default model.
Question: Can you provide an example of JTextPane?
Answer: import java.awt.BorderLayout;  
import java.awt.Color;  
import java.awt.Container;  
import javax.swing.JFrame;  
import javax.swing.JScrollPane;  
import javax.swing.JTextPane;  
import javax.swing.text.BadLocationException;  
import javax.swing.text.Document;  
import javax.swing.text.SimpleAttributeSet;  
import javax.swing.text.StyleConstants;  
public class JTextPaneExample {  
    public static void main(String args[]) throws BadLocationException {  
        JFrame frame = new JFrame("JTextPane Example");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        Container cp = frame.getContentPane();  
        JTextPane pane = new JTextPane();  
        SimpleAttributeSet attributeSet = new SimpleAttributeSet();  
        StyleConstants.setBold(attributeSet, true);  
  
        // Set the attributes before adding text  
        pane.setCharacterAttributes(attributeSet, true);  
        pane.setText("Welcome");  
  
        attributeSet = new SimpleAttributeSet();  
        StyleConstants.setItalic(attributeSet, true);  
        StyleConstants.setForeground(attributeSet, Color.red);  
        StyleConstants.setBackground(attributeSet, Color.blue);  
  
        Document doc = pane.getStyledDocument();  
        doc.insertString(doc.getLength(), "To Java ", attributeSet);  
  
        attributeSet = new SimpleAttributeSet();  
        doc.insertString(doc.getLength(), "World", attributeSet);  
  
        JScrollPane scrollPane = new JScrollPane(pane);  
        cp.add(scrollPane, BorderLayout.CENTER);  
  
        frame.setSize(400, 300);  
        frame.setVisible(true);  
      }  
}
Question: What is Java JRootPane?
Answer: RootPane is a lightweight container used behind the scenes by JFrame, JDialog, JWindow, JApplet, and JInternalFrame.
Question: Can you provide an example of JRootPane?
Answer: import javax.swing.JButton;  
import javax.swing.JFrame;  
import javax.swing.JMenu;  
import javax.swing.JMenuBar;  
import javax.swing.JRootPane;  
  
public class JRootPaneExample {  
     public static void main(String[] args) {  
            JFrame f = new JFrame();  
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            JRootPane root = f.getRootPane();  
  
            // Create a menu bar  
            JMenuBar bar = new JMenuBar();  
            JMenu menu = new JMenu("File");  
            bar.add(menu);  
            menu.add("Open");  
            menu.add("Close");  
            root.setJMenuBar(bar);  
  
            // Add a button to the content pane  
            root.getContentPane().add(new JButton("Press Me"));  
  
            // Display the UI  
            f.pack();  
            f.setVisible(true);  
          }  
}
Question: How to use ToolTip in Java Swing?
Answer: You can create a tool tip for any JComponent with setToolTipText() method. This method is used to set up a tool tip for the component.

For example, to add tool tip to PasswordField, you need to add only one line of code:

field.setToolTipText("Enter your Password");
Question: Can you provide an example of Simple ToolTip?
Answer: import javax.swing.*;    
public class ToolTipExample {  
    public static void main(String[] args) {    
     JFrame f=new JFrame("Password Field Example");    
     //Creating PasswordField and label  
     JPasswordField value = new JPasswordField();   
     value.setBounds(100,100,100,30);    
     value.setToolTipText("Enter your Password");  
     JLabel l1=new JLabel("Password:");    
     l1.setBounds(20,100, 80,30);    
     //Adding components to frame  
     f.add(value);  f.add(l1);  
     f.setSize(300,300);    
     f.setLayout(null);    
     f.setVisible(true);      
}  
}
Question: How to change TitleBar icon in Java AWT and Swing?
Answer: The setIconImage() method of Frame class is used to change the icon of Frame or Window. It changes the icon which is displayed at the left side of Frame or Window.

The Toolkit class is used to get instance of Image class in AWT and Swing.

Toolkit class is the abstract super class of every implementation in the Abstract Window Toolkit(AWT). Subclasses of Toolkit are used to bind various components. It inherits Object class.
Question: Can you provide an Example to change TitleBar icon in Java AWT?
Answer: import java.awt.*;     
class IconExample {     
IconExample(){     
Frame f=new Frame();     
Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");    
f.setIconImage(icon);    
f.setLayout(null);     
f.setSize(400,400);     
f.setVisible(true);     
}     
public static void main(String args[]){     
new IconExample();     
}     
}
Question: Can you provide an Example to change TitleBar icon in Java Swing?
Answer: import javax.swing.*;  
import java.awt.*;  
class IconExample {   
IconExample(){   
JFrame f=new JFrame();   
Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");  
f.setIconImage(icon);  
f.setLayout(null);   
f.setSize(200,200);   
f.setVisible(true);   
}   
public static void main(String args[]){   
new ToolkitExample();   
}   
}
Question: How to make an executable jar file in Java?
Answer: The jar (Java Archive) tool of JDK provides the facility to create the executable jar file. An executable jar file calls the main method of the class if you double click it.

To create the executable jar file, you need to create .mf file, also known as manifest file.
Question: What is jar (Java Archive)?
Answer: The jar (Java Archive) tool of JDK provides the facility to create the executable jar file. An executable jar file calls the main method of the class if you double click it.

To create the executable jar file, you need to create .mf file, also known as manifest file.
Question: What is Creating manifest file?
Answer: To create manifest file, you need to write Main-Class, then colon, then space, then classname then enter. For example:

myfile.mf
Main-Class: First  
As you can see, the mf file starts with Main-Class colon space class name. Here, class name is First.

In mf file, new line is must after the class name.
Question: What is Creating executable jar file using jar tool?
Answer: The jar tool provides many switches, some of them are as follows:

1-c creates new archive file
2-v generates verbose output. It displays the included or extracted resource on the standard output.
3-m includes manifest information from the given mf file.
4-f specifies the archive file name
5.-x extracts files from the archive file
Now, let's write the code to generated the executable jar using mf file.

You need to write jar then swiches then mf_file then jar_file then .classfile as given below:

jar -cvmf myfile.mf myjar.jar First.class
Question: Can you provide an Example of digital clock in swing:?
Answer: import javax.swing.*;  
import java.awt.*;  
import java.text.*;  
import java.util.*;  
public class DigitalWatch implements Runnable{  
JFrame f;  
Thread t=null;  
int hours=0, minutes=0, seconds=0;  
String timeString = "";  
JButton b;  
  
DigitalWatch(){  
    f=new JFrame();  
      
    t = new Thread(this);  
        t.start();  
      
    b=new JButton();  
        b.setBounds(100,100,100,50);  
      
    f.add(b);  
    f.setSize(300,400);  
    f.setLayout(null);  
    f.setVisible(true);  
}  
  
 public void run() {  
      try {  
         while (true) {  
  
            Calendar cal = Calendar.getInstance();  
            hours = cal.get( Calendar.HOUR_OF_DAY );  
            if ( hours > 12 ) hours -= 12;  
            minutes = cal.get( Calendar.MINUTE );  
            seconds = cal.get( Calendar.SECOND );  
  
            SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");  
            Date date = cal.getTime();  
            timeString = formatter.format( date );  
  
            printTime();  
  
            t.sleep( 1000 );  // interval given in milliseconds  
         }  
      }  
      catch (Exception e) { }  
 }  
  
public void printTime(){  
b.setText(timeString);  
}  
  
public static void main(String[] args) {  
    new DigitalWatch();  
          
  
}  
}
Question: What is Displaying graphics in swing:?
Answer: java.awt.Graphics class provides many methods for graphics programming.
Question: What is public abstract void drawString(String str, int x, int y)?
Answer: public abstract void drawString(String str, int x, int y): is used to draw the specified string.
Question: What is public void drawRect(int x, int y, int width, int height)?
Answer: public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height.
Question: What is public abstract void fillRect(int x, int y, int width, int height)?
Answer: public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and specified width and height.
Question: What is  public abstract void drawOval(int x, int y, int width, int height)?
Answer: public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the specified width and height.
Question: What is  public abstract void fillOval(int x, int y, int width, int height)?
Answer: public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the default color and specified width and height.
Question: What is public abstract void drawLine(int x1, int y1, int x2, int y2)?
Answer: public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and (x2, y2).
Question: What is  public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)?
Answer: public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image.
Question: What is public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)?
Answer: public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used draw a circular or elliptical arc.
Question: What is  public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)?
Answer: public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used to fill a circular or elliptical arc.
Question: What is  public abstract void setColor(Color c)?
Answer: public abstract void setColor(Color c): is used to set the graphics current color to the specified color.
Question: What is public abstract void setFont(Font font)?
Answer: public abstract void setFont(Font font): is used to set the graphics current font to the specified font.
Question: Can you provide an Example of displaying graphics in swing?
Answer: import java.awt.*;  
import javax.swing.JFrame;  
  
public class DisplayGraphics extends Canvas{  
      
    public void paint(Graphics g) {  
        g.drawString("Hello",40,40);  
        setBackground(Color.WHITE);  
        g.fillRect(130, 30,100, 80);  
        g.drawOval(30,130,50, 60);  
        setForeground(Color.RED);  
        g.fillOval(130,130,50, 60);  
        g.drawArc(30, 200, 40,50,90,60);  
        g.fillArc(30, 130, 40,50,180,40);  
          
    }  
        public static void main(String[] args) {  
        DisplayGraphics m=new DisplayGraphics();  
        JFrame f=new JFrame();  
        f.add(m);  
        f.setSize(400,400);  
        //f.setLayout(null);  
        f.setVisible(true);  
    }  
  
}
Question: What is Displaying image in swing?
Answer: For displaying image, we can use the method drawImage() of Graphics class.
Question: What is Syntax of drawImage() method?
Answer: public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image.
Question: What is public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)?
Answer: public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image.
Question: Can you provide an Example of displaying image in swing?
Answer: import java.awt.*;  
import javax.swing.JFrame;  
  
public class MyCanvas extends Canvas{  
      
    public void paint(Graphics g) {  
  
        Toolkit t=Toolkit.getDefaultToolkit();  
        Image i=t.getImage("p3.gif");  
        g.drawImage(i, 120,100,this);  
          
    }  
        public static void main(String[] args) {  
        MyCanvas m=new MyCanvas();  
        JFrame f=new JFrame();  
        f.add(m);  
        f.setSize(400,400);  
        f.setVisible(true);  
    }  
  
}
Question: What are the Java LayoutManagers?
Answer: The LayoutManagers are used to arrange components in a particular manner. The Java LayoutManagers facilitates us to control the positioning and size of the components in GUI forms. LayoutManager is an interface that is implemented by all the classes of layout managers. There are the following classes that represent the layout managers:

1.java.awt.BorderLayout
2.java.awt.FlowLayout
3.java.awt.GridLayout
4.java.awt.CardLayout
5.java.awt.GridBagLayout
6.javax.swing.BoxLayout
7.javax.swing.GroupLayout
8.javax.swing.ScrollPaneLayout
9.javax.swing.SpringLayout etc.
Question: What are the Java BorderLayout?
Answer: The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the default layout of a frame or window. The BorderLayout provides five constants for each region:

1.public static final int NORTH
2.public static final int SOUTH
3.public static final int EAST
4.public static final int WEST
5.public static final int CENTER
Question: What are the  Constructors of BorderLayout class:?
Answer: -BorderLayout(): creates a border layout but with no gaps between the components.
-BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and vertical gaps between the components.
Question: Can you provide an Example of BorderLayout class: Using BorderLayout() constructor?
Answer: FileName: Border.java

import java.awt.*;    
import javax.swing.*;    
    
public class Border   
{    
JFrame f;    
Border()  
{    
    f = new JFrame();    
        
     // creating buttons  
    JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH   
    JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH  
    JButton b3 = new JButton("EAST");; // the button will be labeled as EAST  
    JButton b4 = new JButton("WEST");; // the button will be labeled as WEST  
    JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER  
        
    f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction    
    f.add(b2, BorderLayout.SOUTH);  // b2 will be placed in the South Direction    
    f.add(b3, BorderLayout.EAST);  // b2 will be placed in the East Direction    
    f.add(b4, BorderLayout.WEST);  // b2 will be placed in the West Direction    
    f.add(b5, BorderLayout.CENTER);  // b2 will be placed in the Center    
        
    f.setSize(300, 300);    
    f.setVisible(true);    
}    
public static void main(String[] args) {    
    new Border();    
}    
}
Question: What is Java GridLayout?
Answer: The Java GridLayout class is used to arrange the components in a rectangular grid. One component is displayed in each rectangle.
Question: What are the Constructors of GridLayout class?
Answer: 1.GridLayout(): creates a grid layout with one column per component in a row.
2.GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components.
3.GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns along with given horizontal and vertical gaps.
Question: Can you provide an Example of GridLayout class: Using GridLayout() Constructor?
Answer: The GridLayout() constructor creates only one row. The following example shows the usage of the parameterless constructor.

FileName: GridLayoutExample.java

// import statements  
import java.awt.*;    
import javax.swing.*;    
    
public class GridLayoutExample  
{    
JFrame frameObj;    
  
// constructor  
GridLayoutExample()  
{    
frameObj = new JFrame();    
  
// creating 9 buttons  
JButton btn1 = new JButton("1");    
JButton btn2 = new JButton("2");    
JButton btn3 = new JButton("3");    
JButton btn4 = new JButton("4");    
JButton btn5 = new JButton("5");    
JButton btn6 = new JButton("6");    
JButton btn7 = new JButton("7");    
JButton btn8 = new JButton("8");    
JButton btn9 = new JButton("9");    
    
// adding buttons to the frame  
// since, we are using the parameterless constructor, therfore;   
// the number of columns is equal to the number of buttons we   
// are adding to the frame. The row count remains one.  
frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);  
frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);  
frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);    
  
// setting the grid layout using the parameterless constructor    
frameObj.setLayout(new GridLayout());    
  
  
frameObj.setSize(300, 300);    
frameObj.setVisible(true);    
}  
  
// main method  
public static void main(String argvs[])   
{    
new GridLayoutExample();    
}    
}    .
Question: What is Java FlowLayout?
Answer: The Java FlowLayout class is used to arrange the components in a line, one after another (in a flow). It is the default layout of the applet or panel.
Question: What are the Fields of FlowLayout class?
Answer: 1.public static final int LEFT
2.public static final int RIGHT
3.public static final int CENTER
4.public static final int LEADING
5.public static final int TRAILING
Question: What are the Constructors of FlowLayout class?
Answer: 1.FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.
2.FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.
3.FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment and the given horizontal and vertical gap.
Question: Can you provide an Example of FlowLayout class: Using FlowLayout() constructor?
Answer: FileName: FlowLayoutExample.java

// import statements  
import java.awt.*;    
import javax.swing.*;    
    
public class FlowLayoutExample  
{   
   
JFrame frameObj;  
  
// constructor    
FlowLayoutExample()  
{    
    // creating a frame object  
    frameObj = new JFrame();    
        
     // creating the buttons  
    JButton b1 = new JButton("1");    
    JButton b2 = new JButton("2");    
    JButton b3 = new JButton("3");    
    JButton b4 = new JButton("4");    
    JButton b5 = new JButton("5");  
    JButton b6 = new JButton("6");    
    JButton b7 = new JButton("7");    
    JButton b8 = new JButton("8");    
    JButton b9 = new JButton("9");    
    JButton b10 = new JButton("10");    
    
         
    // adding the buttons to frame        
    frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(b4);      
    frameObj.add(b5); frameObj.add(b6);  frameObj.add(b7);  frameObj.add(b8);    
    frameObj.add(b9);  frameObj.add(b10);      
       
    // parameter less constructor is used  
    // therefore, alignment is center   
    // horizontal as well as the vertical gap is 5 units.  
    frameObj.setLayout(new FlowLayout());    
        
    frameObj.setSize(300, 300);    
    frameObj.setVisible(true);    
}    
  
// main method  
public static void main(String argvs[])   
{    
    new FlowLayoutExample();    
}    
}
Question: What is Java BoxLayout?
Answer: The Java BoxLayout class is used to arrange the components either vertically or horizontally. For this purpose, the BoxLayout class provides four constants. They are as follows:

Note: The BoxLayout class is found in javax.swing package.