Found 9150 Articles for Object Oriented Programming

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

440 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

179 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

834 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

Java Program to enable column selection in a JTable

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

227 Views

To enable colum selection, use the setColumnSelectionAllowed() method and set it to TRUE −table.setCell setColumnSelectionAllowed(true);The following is an example to enable column selection in a table −Examplepackage my; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JPanel panel = new JPanel();       panel.setBorder(BorderFactory.createTitledBorder(          BorderFactory.createEtchedBorder(), "ODI Rankings", TitledBorder.CENTER, TitledBorder.TOP));       String[][] rec = {          { "1", "Steve", "AUS" },     ... Read More

How to enable row selection in a JTable with Java

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

2K+ Views

To enable row selection, use the setRowSelectionAllowed () method and set it to TRUE −table.setCell setRowSelectionAllowed(true);The following is an example to enable row selection in a JTable −Examplepackage my; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       JPanel panel = new JPanel();       panel.setBorder(BorderFactory.createTitledBorder(          BorderFactory.createEtchedBorder(), "ODI Rankings", TitledBorder.CENTER, TitledBorder.TOP));       String[][] rec = {          { "1", "Steve", "AUS" },   ... Read More

Java Program to create DefaultTableModel from two dimensional array

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

463 Views

The DefaultTableModel is an implementation of TableModel that uses a Vector of Vectors to store the cell value objects. At first create a two dimensional array for rows and columns −DefaultTableModel tableModel = new DefaultTableModel(new Object[][] {    { "India", "Asia" }, { "Canada", "North America" }, { "Singapore", "Asia" },    { "Malaysia", "Asia" }, { "Philippins", "Asia" }, { "Oman", "Asia" },    { "Germany", "Europe" }, { "France", "Europe" }  }, new Object[] { "Country", "Continent" });Above, “Country” and “Continent” are the columns. Now, set the above set of rows and columns to JTable −JTable table = ... Read More

Can we read from JOptionPane by requesting input from user in Java?

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

258 Views

Yes, we can read from JOptionPane. Here, we will get the result of the showInputDialog() in a String variable −String input = JOptionPane.showInputDialog("Enter the C++ lessons you covered till now?");After getting the result, we will convert it to integer with parseInt() and display it on Console −int res = Integer.parseInt(input); System.out.println("Lessons covered = "+res);The following is an example to read from JOptionPane by requesting input from user −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) throws Exception { ... Read More

Advertisements