Found 4338 Articles for Java 8

Java program to get the previous sibling from a JTree

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

111 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

615 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 30-Jul-2019 22:30:26

1K+ Views

Let us first crate a ComboBox and add items to it −Object[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" }; JComboBox comboBox = new JComboBox(sports);Now, add the combo box to the JOptionPane −JOptionPane.showMessageDialog(null, comboBox, "Fav Sports", JOptionPane.QUESTION_MESSAGE);The following is an example to set JComboBox in JOptionPane −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 = new JPanel(new GridBagLayout());       Object[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" };       JComboBox ... Read More

Java Program to enable column selection in a JTable

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

168 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

287 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

187 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

1K+ 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

How to apply adjustments to the next column of a JTable only, when the width of a column is changed in Java Swing?

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

125 Views

To apply adjustments to the next column only, use the setAutoResizeMode and set the mode. The mode here would be AUTO_RESIZE_NEXT_COLUMN. This will allow you to adjust only the next column even if any of the column header is dragged to resize.Let us first see an example to create a table −Examplepackage my; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       DefaultTableModel tableModel = new DefaultTableModel();       JTable table = new JTable(tableModel);       tableModel.addColumn("Technology");       tableModel.addColumn("BCA"); ... Read More

Advertisements