Found 4336 Articles for Java 8

How to activate and deactivate JFrame in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

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

How to get Directories from JFileChooser in Java

Samual Sam
Updated on 30-Jul-2019 22:30:26

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

Java Program to display the contents in JTextArea

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

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

How to create FileFilter for JFileChooser in Java and display File Type accordingly?

Samual Sam
Updated on 30-Jul-2019 22:30:26

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

static keyword in C++ vs Java

Smita Kapse
Updated on 30-Jul-2019 22:30:26

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

Inheritance in C++ vs Java

Anvi Jain
Updated on 30-Jul-2019 22:30:26

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

What happens when JDialog is set with Modality type APPLICATION_MODAL

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

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

How to pre-select JComboBox item by index in Java?

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

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

How to display the items in a JComboBox in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

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

How to display the first element in a JComboBox in Java

Nancy Den
Updated on 30-Jul-2019 22:30:26

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

Advertisements