Programming Articles - Page 2584 of 3363

Java program to get the previous sibling from a JTree

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

201 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

887 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

Comparison of dates in PHP

Alok Prasad
Updated on 30-Jul-2019 22:30:26

7K+ Views

Matching two dates in PHP is quite smooth when both the dates are in a similar format but php failed to analyze when the two dates are in an unrelated format. In this article, we will discuss different cases of date comparison in PHP. We will figure out how to utilize DateTime class, strtotime() in comparing dates.Case 1:we can analyze the dates by simple comparison operator if the given dates are in a similar format.Output:2019-03-26 is latest than 2018-11-24Explanation:Here we have declared two dates $date1 and $date2 in the same format. So we have used a comparison operator(>) to compare ... Read More

Java Program to enable column selection in a JTable

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

265 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

507 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

295 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

Customize the JOptionPane layout with updated color and image in Java

George John
Updated on 30-Jul-2019 22:30:26

2K+ Views

Customize the layout by changing the look and feel of the panel in which you added the component −ImageIcon icon = new ImageIcon(new URL("http −//www.tutorialspoint.com/images/C-PLUS.png")); JLabel label = new JLabel(icon); JPanel panel = new JPanel(new GridBagLayout()); panel.add(label); panel.setOpaque(true); panel.setBackground(Color.ORANGE);Above, we have added an image and even updated the background color of the panel.Now, set it for the text panel −JPanel textPanel = new JPanel(new GridLayout(10, 5)); textPanel.setBackground(Color.Magenta);The following is an example to customize the JOptionPane layout −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 { ... Read More

Advertisements