Found 9150 Articles for Object Oriented Programming

How to create a border with a raised beveled edge in Java?

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

243 Views

Use the createRaisedBevelBorder() method to create a border with a raised beveled edge. We will set it on the label component −JLabel label; label = new JLabel("This has a border with a raised bevel edge!"); label.setBorder(BorderFactory.createRaisedBevelBorder());The following is an example to create a border with a raised beveled edge −Examplepackage my; import javax.swing.BorderFactory; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       JLabel label;       label = new JLabel("This has a border with a raised bevel edge!"); ... Read More

How to move the horizontal slider right-to-left in Java?

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

203 Views

At first, let us create a horizontal slider −JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 55);Now, we will set it to move right-to-left using setInverted() −slider.setInverted(true);The following is an example to move the horizontal slider right-to-left −Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Frame with Slider");       JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 55);       slider.setInverted(true);       slider.setMinorTickSpacing(10);       slider.setMajorTickSpacing(25);       slider.setPaintTicks(true);     ... Read More

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

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

774 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 07-Nov-2024 01:05:52

356 Views

In this article, we will learn how to set major tick marks in a JSlider component in Java. A JSlider is a Swing component that allows users to select a numeric value from a range. Tick marks help improve user experience by marking specific intervals along the slider. Major tick marks represent larger intervals, and we can control their spacing using the setMajorTickSpacing() method. Problem StatementGiven a slider with a range of values, write a Java program to set major tick marks every 25 units in a JSlider and display the slider in a GUI window.Input A slider with a range ... 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

877 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

424 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

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

194 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

696 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

235 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

Advertisements