- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 4336 Articles for Java 8
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
392 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
142 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
305 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
637 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
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
492 Views
In C++ or Java we can get the static keyword. They are mostly same, but there are some basic differences between these two languages. Let us see the differences between static in C++ and static in Java.The static data members are basically same in Java and C++. The static data members are the property of the class, and it is shared to all of the objects.Examplepublic class Test { static int ob_count = 0; Test() { ob_count++; } public static void main(String[] args) { Test object1 = new Test(); ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
1K+ Views
In C++ and Java, there are the concept of Inheritance. The inheritance properties are used to reuse the code and also make a relationship between two objects. Here we will see some basic differences between inheritance in C++ and inheritance in Java.In Java, all of the classes are extending the Object class. So there is always a single level inheritance tree of classes. The object class is present at the root of the tree. Let us check this is true or not using a simple code.Example//This is present in the different file named MyClass.java public class MyClass { MyClass() ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
195 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
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
1K+ Views
The following is an example to pre-select JComboBox item by index in Java. Here, we have selected the 3rd item by default i.e. whenever the Swing program will run, the third item would be visible instead of the 1st.Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class SwingDemo { public static void main(String[] args) { JPanel panel = new JPanel(new BorderLayout()); String[] strArr = new String[] { "Laptop", "Mobile", "Desktop", "Tablet" }; JComboBox comboBox = new JComboBox(strArr); panel.add(comboBox, ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
625 Views
The following is an example to display the first element in a JComboBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class SwingDemo { public static void main(String[] args) { JPanel panel = new JPanel(new BorderLayout()); String[] strArr = new String[] { "Laptop", "Mobile", "Desktop", "Tablet" }; JComboBox comboBox = new JComboBox(strArr); panel.add(comboBox, BorderLayout.NORTH); JTextArea text = new JTextArea(5, 5); panel.add(text, BorderLayout.CENTER); JButton btn = new JButton("Click"); ... Read More
![Nancy Den](https://www.tutorialspoint.com/assets/profiles/13557/profile/60_88439-1512714529.jpg)
566 Views
To display the first element in a JComboBox, use the getSelectedIndex():comboBox.setSelectedIndex(0);The following is an example to display the first element in a JComboBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class SwingDemo { public static void main(String[] args) { JPanel panel = new JPanel(new BorderLayout()); String[] strArr = new String[] { "Laptop", "Mobile", "Desktop", "Tablet" }; JComboBox comboBox = new JComboBox(strArr); panel.add(comboBox, BorderLayout.NORTH); JTextArea text = new JTextArea(5, 5); panel.add(text, ... Read More