
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9150 Articles for Object Oriented Programming

675 Views
To clear all selections, use the List clearSelection() method in Java −JList list = new JList(sports); list.clearSelection();Above, the elements in Sports array is a String array −String sports[]= { "Cricket", "Football", "Hockey", "Rugby"};The following is an example to clear all selection in JList −Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame { static JFrame frame; static JList list; public static void main(String[] args) { frame = new JFrame("JList Demo"); SwingDemo s = new SwingDemo(); JPanel panel = new JPanel(); String sports[]= ... Read More

394 Views
To select the second index, use the setSelectedIndex() method −JList new JList(sports); list.setSelectedIndex(2);The following is an example to select the second index in Java JList −Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame { static JFrame frame; static JList list; public static void main(String[] args) { frame = new JFrame("JList Demo"); SwingDemo s = new SwingDemo(); JPanel panel = new JPanel(); String sports[]= { "Cricket","Football","Hockey","Rugby"}; list = new JList(sports); list.setSelectedIndex(2); panel.add(list); frame.add(panel); frame.setSize(400,400); frame.setVisible(true); } }Output

133 Views
The following is an example to create JRadioButton from text −Examplepackage my; import java.awt.FlowLayout; import java.awt.event.KeyEvent; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; public class SwingDemo { public static void main(String[] args) { JRadioButton radio1 = new JRadioButton("Cricket"); JRadioButton radio2 = new JRadioButton("Football"); ButtonGroup group = new ButtonGroup(); group.add(radio1); group.add(radio2); radio1.setSelected(true); JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel("Fav Sports:")); frame.add(radio1); ... Read More

16K+ Views
To add background image to JFrame, use the getImage() method of the Image class −Image img = Toolkit.getDefaultToolkit().getImage("E:\rahul.jpg");Now, draw the image −public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 0, 0, null); }The following is an example to add Background Image to JFrame −Exampleimport java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.io.IOException; import javax.swing.JPanel; public class SwingDemo extends javax.swing.JFrame { Image img = Toolkit.getDefaultToolkit().getImage("E:\rahul.jpg"); public SwingDemo() throws IOException { this.setContentPane(new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); ... Read More

712 Views
To create a JFrame with no border and title bar, use setUndecorated() −JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(400, 300)); frame.setUndecorated(true);The following is an example to create JFrame with no border and title bar −Exampleimport java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(400, 300)); frame.setUndecorated(true); JPanel panel = new JPanel(); panel.add(new JLabel("Demo!")); panel.add(new JButton(new AbstractAction("Close") { ... Read More

558 Views
Here, we are activating a frame. For deactivation, we have used dispose −Thread.sleep(2000); frame.setVisible(false);The frame first activates and then deactivates after 2 seconds since we have set sleep to 2000 miliseconds.The following is an example to activate and deactivate JFrame −Exampleimport java.awt.Frame; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo { public static void main(String[] args) throws InterruptedException { JFrame frame = new JFrame(); frame.add(new JLabel("Demo")); frame.pack(); frame.setVisible(true); Thread.sleep(2000); frame.setState(Frame.ICONIFIED); Thread.sleep(2000); frame.setVisible(false); frame.dispose(); ... Read More

216 Views
To get directories from JFileChoose, use the mode setFileSelectionMode −JFileChooser file = new JFileChooser(); file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);The following is an example to get Directories from JFileChooser −Exampleimport javax.swing.JFileChooser; public class SwingDemo { public static void main(String[] args) { JFileChooser file = new JFileChooser(); file.setMultiSelectionEnabled(false); file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int res = file.showOpenDialog(null); if (res == JFileChooser.APPROVE_OPTION) { java.io.File f = file.getSelectedFile(); System.err.println(f.getPath()); } } }Output

434 Views
The following is an example to display the contents of a text file in JTextArea −Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo { private JFrame mainFrame; private JLabel statusLabel; private JPanel controlPanel; public SwingDemo() { prepareGUI(); } public static void main(String[] args) { SwingDemo demo = new SwingDemo(); demo.showTextAreaDemo(); } private void prepareGUI() { mainFrame = new JFrame("Java Swing"); mainFrame.setSize(400, 400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { ... Read More

977 Views
To create FileFilter, use the FileNamExtensionFilter class. The following is an example to display File Type in JFileChooser −Exampleimport javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; public class SwingDemo { public static void main(String[] args) { JFileChooser file = new JFileChooser(); file.setAcceptAllFileFilterUsed(false); FileNameExtensionFilter extFilter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg"); file.addChoosableFileFilter(extFilter); file.showOpenDialog(null); } }Output

313 Views
The JDialog Modality type APPLICATION_MODAL blocks all top-level windows and it has restrictions. The following is an example to set JDialog with Modality type APPLICATION_MODAL:Exampleimport java.awt.Cursor; import java.awt.Dialog.ModalityType; import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(new Dimension(600, 400)); JDialog dialog = new JDialog(frame, "New", ModalityType.APPLICATION_MODAL); dialog.setSize(300, 300); frame.add(new JButton(new AbstractAction("Click to generate") { @Override public void ... Read More