Found 4337 Articles for Java 8

Display multiple lines of text in a component’s tooltip with Java

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

536 Views

Let’s first see how we set text in a components tooltip −JLabel label3 = new JLabel("Password", SwingConstants.CENTER); label3.setToolTipText("Enter Password");To display multiple lines of text in a tooltip, use . Here, we have used the HTML tag for new line and that would create multiple lines of text in the tooltip −label3.setToolTipText("" + "This will create multiple lines for the" + "" + "component! Yay!" + "");The following is an example to display multiple lines of text in a component’s tooltip −Examplepackage my; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Point; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; import ... Read More

Java Program to set major tick marks in a JSlider every 25 units

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

251 Views

Major Tick marks is the number passed in representing the distance between each major tick mark. For example, a slider with range from 0 to 75 and major tick spacing 25, would give major ticks next to the following values: 0, 25, 50, 75.To set major tick marks, use the setMajorTickSpacing() method −JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 55); slider.setMajorTickSpacing(25);Note − For major ticks to be painted, you need to set setPaintTicks to true.The following is an example to set major tick marks in a slider every 25 units −Examplepackage my; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.WindowConstants; public ... Read More

How to create input Pop-Ups (Dialog) and get input from user in Java?

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

630 Views

Use the JOptionPane.showInputDialog() to get input from user in a dialog box like “Which sports you play the most”, “What is your name”, etc. The following is an example to create input Pop-Ups (Dialog) and get input from user −Examplepackage my; import javax.swing.JOptionPane; public class SwingDemo {    public static void main(String[] args) {       String[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" };       String res = (String) JOptionPane.showInputDialog(null, "Which sports you play the most?", "Sports",          JOptionPane.PLAIN_MESSAGE, null, sports, sports[0]);       switch (res) {     ... Read More

How to display vertical grid lines in a table with Java?

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

294 Views

To display vertical grid lines in a table, use the setShowVerticalLines() method. Let us first create a table −String[][] rec = {    { "1", "Steve", "AUS" },    { "2", "Virat", "IND" },    { "3", "Kane", "NZ" },    { "4", "David", "AUS" },    { "5", "Ben", "ENG" },    { "6", "Eion", "ENG" }, }; String[] header = { "Rank", "Player", "Country" }; JTable table = new JTable(rec, header);Now, let us display vertical grid lines −table.setShowVerticalLines(true);You can also give a color to these lines −table.setGridColor(Color.blue);The following is an example to display vertical grid lines in JTable ... Read More

How to create a Confirmation Dialog Box in Java?

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

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

142 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

558 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

124 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

327 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

Advertisements