Question
stringlengths
1
113
Answer
stringlengths
22
6.98k
Can you provide an example of Java JTable?
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(); } }
Java JTable Example with ListSelectionListener
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); } }
What is Java JList?
The object of JList class represents a list of text items. The list of text items can be set up so that the user can choose either one item or multiple items. It inherits JComponent class.
What is JList class declaration?
public class JList extends JComponent implements Scrollable, Accessible
Can you provide an example of Java JList?
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(); }}
Can you provid a Java JList Example with ActionListener?
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(); }}
What is Java JOptionPane?
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.
Can you provide an example of JOptionPane class declaration?
public class JOptionPane extends JComponent implements Accessible
Can you provide an example of Java JOptionPane: showMessageDialog()?
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(); } }
Can you provide an example of Java JOptionPane: showInputDialog()?
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(); } }
Can you provide an example of Java JOptionPane showConfirmDialog()?
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(); } }
What is Java JScrollBar?
The object of JScrollbar class is used to add horizontal and vertical scrollbar. It is an implementation of a scrollbar. It inherits JComponent class.
What is JScrollBar class declaration?
public class JScrollBar extends JComponent implements Adjustable, Accessible
Can you provide an example of Java JScrollBar?
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(); }}
Can you provide an example of Java JScrollBar with AdjustmentListener
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(); }}
What are the Java JMenuBar, JMenu and JMenuItem?
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.
What is JMenu class declaration?
public class JMenu extends JMenuItem implements MenuElement, Accessible
What is JMenuItem class declaration?
public class JMenuItem extends AbstractButton implements Accessible, MenuElement
Can you provide an example of Java JMenuItem and JMenu?
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(); }}
Can you provided an Example of creating Edit menu for Notepad?
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(); } }
What is Java JPopupMenu?
PopupMenu can be dynamically popped up at specific position within a component. It inherits the JComponent class.
What is JPopupMenu class declaration?
public class JPopupMenu extends JComponent implements Accessible, MenuElement
Can you provide an example of Java JPopupMenu?
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(); }}
Can you provide an example Java JPopupMenu with MouseListener and ActionListener?
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(); } }
What is Java JCheckBoxMenuItem?
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.
Can you provide a Java JCheckBoxMenuItem Example?
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); } }
What is Java JSeparator?
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.
What is JSeparator class declaration?
public class JSeparator extends JComponent implements SwingConstants, Accessible
Can you provide an example of Java JSeparator?
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(); }}
What is Java JProgressBar?
The JProgressBar class is used to display the progress of the task. It inherits JComponent class.
What is JProgressBar class declaration?
public class JProgressBar extends JComponent implements SwingConstants, Accessible
Can you provide an example of Java JProgressBar?
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(); } }
What is Java JTree?
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.
What is JTree class declaration?
public class JTree extends JComponent implements Scrollable, Accessible
Can you provide an example of Java JTree?
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(); }}
What is Java JColorChooser?
The JColorChooser class is used to create a color chooser dialog box so that user can select any color. It inherits JComponent class.
What is JColorChooser class declaration?
public class JColorChooser extends JComponent implements Accessible
Can you provide an example of Java JColorChooser?
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); } }
Can you provide an example in Java JColorChooser with ActionListener?
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(); } }
What is Java JTabbedPane?
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.
What is JTabbedPane class declaration?
public class JTabbedPane extends JComponent implements Serializable, Accessible, SwingConstants
Can you provide an example Java JTabbedPane?
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(); }}
What is Java JSlider?
The Java JSlider class is used to create the slider. By using JSlider, a user can select a value from a specific range.
Can you provide an example of Java JSlider?
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); } }
Can you provide an example of Java JSlider: painting ticks?
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); } }
What is Java JSpinner?
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.
What is JSpinner class declaration?
public class JSpinner extends JComponent implements Accessible
Can you provide an example of Java JSpinner?
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); } }
Can you provide an example of Java JSpinner with ChangeListener?
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()); } }); } }
What is Java JDialog?
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.
What is JDialog class declaration?
public class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneContainer
Can you provide an example of Java JDialog?
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(); } }
What is Java JPanel?
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.
What is JPanel class declaration?
public class JPanel extends JComponent implements Accessible
Can you provide an example of Java JPanel?
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(); } }
What is Java JFileChooser?
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.
Can you provide an example of Constructors Present in Java JFileChooser Class?
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); } }
What is 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); } }
Can you provide an example ofJFileChooser(File currentDirectory):?
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 } } .
Can you provide an example of JFileChooser(File currentDirectory, FileSystemViewfsv):?
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 } }
Can you provide an example of JFileChooser(FileSystemViewfsv):?
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 } }
Can you provide an example of JFileChooser(String currentDirectoryPath):?
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 } }
Can you provide an example of JFileChooser(String currentDirectoryPath, FileSystemViewfsv):?
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 } }
Can you provide an example of JFileChooser( File ):?
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 } }
Can you provide an example of JFileChooser(File, FileSystemView):?
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 } }
What are the Advantages of JFileChooser()?
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.
Can you give me Methods in JFileChooser Class?
The JFileChooser class fundamentally gives three strategies that show the record chooser discoursed, as in the accompanying:
what is showOpenDialog(component owner ):?
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);
What is showSaveDialog( component owner ):?
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);
What is showDialog(component owner, string):?
3. showDialog(component owner, string): This technique shows an exchange box with an endorsement button which is client characterized. Syntax: intshowDialog(component owner,string);
What is getSelectedFile(): ?
4. getSelectedFile(): This technique returns the document that is chosen by the client. Syntax: filegetSelectedFile();
What is setCurrentDirectory(File f):?
5. setCurrentDirectory(File f): This technique sets the ongoing catalog as determined. Syntax: voidsetCurrentDirectory(File f);
What is getCurrentDirectory()?
6. getCurrentDirectory(): This technique for JFileChooser returns the ongoing index as chosen by the client. Syntax: filegetCurrentDirectory();
What is (File f)?
7. (File f):This strategy returns the name of the document as given by the record contention. Syntax: String getName(File f);
Can you provide an example of the program to create a JFrame window:?
A example program to create a JFrame window: // java program to create the frame and perform the action in that // importing the required packages and classes import java.io.*; importjavax.swing.*; importjava.awt.event.*; importjavax.swing.filechooser.*; classHelloWorld extends JFrame implements ActionListener { // declaring a Jlable to show the files whichare selected by the user staticJLabel l; // a default constructor HelloWorld() { } public static void main(String args[]) { // // creating an object to the Jframe class and giving the name of the frame JFrame f = new JFrame(" JavaTpoint"); // fixing the size of the frame f.setSize(400, 400); // giving the frame visibility f.setVisible(true); // closing the frame f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // creating a JButton to save the dialog JButton b1 = new JButton("save dialog"); // creating a JButton to open the dialog JButton b2 = new JButton("open dialog"); // creating an object to the HelloWorld class HelloWorld f1 = new HelloWorld(); // Using action listener class to capture the response of the user using buttons b1.addActionListener(f1); // adding b1 button in actionListener b2.addActionListener(f1); // adding b2 button in actionListener // Creating a panel to add buttons and labels JPanel p = new JPanel(); // adding buttons to the frame p.add(b1); p.add(b2); // set the label to its initial value l = new JLabel("no file selected"); // adding the panel to the frame p.add(l); f.add(p); f.show(); } public void actionPerformed(ActionEvent e) { // if the client presses the save button, show the save dialog String com = e.getActionCommand(); if (com.equals("save")) { // creating an object to the JFileChooser class JFileChooserjf = new JFileChooser(FileSystemView.getFileSystemView()); // calling the showsSaveDialog method to display the save dialog on the frame int r = jf.showSaveDialog(null); // if the user selects a file if (r == JFileChooser.APPROVE_OPTION) { // setting the label as the path of the selected file l.setText(jf.getSelectedFile().getAbsolutePath()); } // if the user canceled the operation else l.setText("The user cancelled the operation"); } // if the user presses the open dialog, show the open dialog else { // create an object of JFileChooser class JFileChooserjf = new JFileChooser(FileSystemView.getFileSystemView()); // calling the showOpenDialog method to display the save dialog on the frame int r = jf.showOpenDialog(null); // if the user selects a file if (r == JFileChooser.APPROVE_OPTION) { // setting the label as the path of the selected file l.setText(jf.getSelectedFile().getAbsolutePath()); } // if the user canceled the operation else l.setText("The user cancelled the operation"); } } }
What is Java JToggleButton?
JToggleButton is used to create toggle button, it is two-states button to switch on or off.
Can you provide an example of JToggleButton ?
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"); } }
What is Java JToolBar?
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.
Can you provide an example of Java JToolBar?
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); } }
What is Java JViewport?
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.
Can you provide an example of JViewport?
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); } }
What is Java JFrame?
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.
Can you provide an example of JFrame?
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); } }
What is Java JComponent?
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.
Can you provide an example of Java JComponent?
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); } }
What is Java JLayeredPane?
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.
What is JLayeredPane class declaration?
public class JLayeredPane extends JComponent implements Accessible
Can you provide an example of Java JLayeredPane?
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); } }
What is Java JDesktopPane?
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.
Can you provide an example of Java JDesktopPane?
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; } } }
What is Java JEditorPane?
JEditorPane class is used to create a simple text editor window. This class has setContentType() and setText() methods.
What is setContentType("text/plain")?
setContentType("text/plain"): This method is used to set the content type to be plain text.
What is setText(text)?
setText(text): This method is used to set the initial text content.
Can you provide an example of JEditorPane?
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); } }
What is Java JScrollPane?
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.
Can you provide an example of JScrollPane?
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(); } }); } }
What is Java JSplitPane?
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.
Can you provide an example of JSplitPane?
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(); } }); } }
What is Java JTextPane?
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.
Can you provide an example of JTextPane?
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); } }