Found 7442 Articles for Java

How to create a Confirmation Dialog Box in Java?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

4K+ Views

To create a confirmation dialog box in Java, use the Java Swing JOptionPane.showConfirmDialog() method, which allows you to create a dialog box that asks for confirmation from the user. For example, Do you want to restart the system?, “This file contains a virus, Do you want to still download?”, etc. Kt comes with a type JOptionPane.YES_NO_CANCEL_OPTION for the same confirmation.The following is an example to create a Confirmation Dialog Box in Java −Examplepackage my; import java.awt.Dimension; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.UIManager; public class SwingDemo {    public static void main(String[] args) {   ... Read More

How to create a JMenuBar Component in Java?

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

195 Views

To create a JMenuBar component, use the JMenuBar class −JMenuBar menuBar = new JMenuBar();Now, create menus inside the MenuBar −JMenu fileMenu = new JMenu("File");Add the above menu to the MenuBar −menuBar.add(fileMenu);The following is an example to create a JMenuBar Component in Java −Examplepackage my; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class SwingDemo {    public static void main(final String args[]) {       JFrame frame = new JFrame("MenuBar Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JMenuBar menuBar = new JMenuBar();       JMenu fileMenu = new JMenu("File");       fileMenu.setMnemonic(KeyEvent.VK_F);   ... Read More

How to create Message Pop-Ups with Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

700 Views

To create Message Pop-ups, use the following JOptionPane −JOptionPane.showMessageDialogWe are displaying a tree inside the message pop-ups. The following is an example to create Message Pop-Ups with Java −Examplepackage my; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Project");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("App");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Website");       DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("WebApp");       node.add(node1);       ... Read More

Can we set JOptionPane with predefined selection in Java?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

239 Views

For predefined selection, use the setSelectedIndex() method, wherein you need to set the index of the item you want to be visible first.Let’s say the following is our aComboBox with elements −Object[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" }; JComboBox comboBox = new JComboBox(sports);Now, set the initial selection with the index of the item −comboBox.setSelectedIndex(3);The following is an example to set JOptionPane with predefined selection in Java −Examplepackage my; import java.awt.GridBagLayout; import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) throws Exception {       JPanel panel = ... Read More

Get the path of the file selected in the JFileChooser component with Java

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

1K+ Views

To get the path of the selected file, at first get the selected file −java.io.File f = file.getSelectedFile();Now, get the path of the selected file which we will get using the above method −System.err.println(f.getPath());The following is an example to get the path of the file selected in the JFileChooser component −Examplepackage my; import javax.swing.JFileChooser; public class SwingDemo {    public static void main(String[] args) {       JFileChooser file = new JFileChooser();       file.setMultiSelectionEnabled(true);       file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);       file.setFileHidingEnabled(false);       if (file.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {          java.io.File f = ... Read More

How to display the JList items from top to bottom and left to right in Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

443 Views

For this, set the layout orientation to the following −setLayoutOrientation(JList.VERTICAL_WRAP);The following is an example to display the JList items from top to bottom and left to right −Examplepackage my; import java.awt.BorderLayout; import java.util.ArrayList ; import java.util.List; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel(new BorderLayout());       List myList = new ArrayList(10);       for (int index = 0; index < 20; index++) {          myList.add("List Item " + index);       }     ... Read More

Java program to get the previous sibling from a JTree

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

181 Views

Use the getPreviousSibling() method to get the previous sibling. Here, we are getting the previous sibling of child node “five” and displaying on Console −System.out.println("Get Previous Sibling = "+five.getPreviousSibling());The following is an example to get the previous sibling from a JTree −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing (Product1 - P66778)");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Accessories (Product2 - P66779)"); ... Read More

How to make JOptionPane to handle Yes, No and Closed buttons in Java?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

2K+ Views

For this, create JOptionPane.QUESTION_MESSAGE and with the user action display individual messages, for example −int res = JOptionPane.showOptionDialog(new JFrame(), "Do you like Cricket?", "Hobbies", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Yes", "No" }, JOptionPane.YES_OPTION); if (res == JOptionPane.YES_OPTION) {    System.out.println("Selected Yes!"); }Above, we have displayed a message on console if the user will selects YES button. The following is an example to make JOptionPane to handle Yes, No and Closed buttons −Examplepackage my; import javax.swing.JFrame; import javax.swing.JOptionPane; public class SwingDemo {    public static void main(String args[]) {       int res = JOptionPane.showOptionDialog(new JFrame(), "Do you like ... Read More

How to set multidimensional array into JTable with Java?

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

840 Views

To set multidimensional array into a table, we need the values for rows and columns. Therefore, create a multidimensional array for rows −Integer[][] marks = {    { 70, 66, 76, 89, 67, 98 },    { 67, 89, 64, 78, 59, 78 },    { 68, 87, 71, 65, 87, 86 },    { 80, 56, 89, 98, 59, 56 },    { 75, 95, 90, 73, 57, 79 },    { 69, 49, 56, 78, 76, 77 } };Now, columns −String students[] = { "S1", "S2", "S3", "S4", "S5", "S6"};Add the rows and columns set above to the ... Read More

Java program to set JComboBox in JOptionPane

George John
Updated on 29-Sep-2024 02:47:14

2K+ Views

In this article, we will explore how to create a graphical user interface (GUI) in Java using JComboBox and JOptionPane. The program will display a pop-up dialog that contains a drop-down list, allowing the user to select their favorite sport from a list. By default, one of the options will be pre-selected, but users can change the selection.  Steps to set JComboBox in JOptionPane Following are the steps to set JComboBox in JOptionPane − Create a JPanel by initializing a JPanel to hold the components. Create a JComboBox by setting up a JComboBox ... Read More

Advertisements