Found 7442 Articles for Java

How to set vertical gap between elements in a GridLayout with Java?

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

1K+ Views

Use the setVgap() method to set the vertical gap between elements in a GridLayout. Let’s say we have a GridLaypout −GridLayout layout = new GridLayout(3, 3);Set the horizontal gap −layout.setVgap(30);The following is an example −Examplepackage my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Sections");       JPanel panel = new JPanel();       panel.setBackground(Color.blue);       GridLayout layout = new GridLayout(3, 3);     ... Read More

Display tick marks in a JSlider with Java

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

917 Views

To display tick marks in a JSlider, you need to use the setPaintTicks() method and set it to TRUE −JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 75); slider.setPaintTicks(true);The following is an example to display tick marks in a slider in Java −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, 75);       slider.setMinorTickSpacing(5);       slider.setMajorTickSpacing(20);       slider.setPaintTicks(true);   ... Read More

How to remove menus from MenuBar in Java?

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

454 Views

Remove a menu from the MenuBar using the remove() method. Set the index for the menu you want to remove from the MenuBar.Let’s say we have the following two menus initially −The following is an example to remove one the above menus. Let’s say we are removing the 2nd menus “Edit” −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 ... Read More

How to hide the track on the slider in Java?

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

254 Views

To hide the track on the slider, you need to use the setPaintTrack() method and set it to FALSE −JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 55); slider.setInverted(true); slider.setMinorTickSpacing(10); slider.setMajorTickSpacing(25); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setPaintTrack(false);The above method setPaintTrack() is by default set to TRUE.The following is an example to hide the track on the slider −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);   ... Read More

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

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

246 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

205 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

776 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

359 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

882 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

425 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

Advertisements