Found 4338 Articles for Java 8

How to create a JSlider that snap to the closest tick mark in Java?

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

261 Views

At first, let us first create a slider in Java −JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 40); slider.setMinorTickSpacing(10); slider.setMajorTickSpacing(20); slider.setPaintTicks(true); slider.setPaintLabels(true);Now, we will snap closest tick mark value as shown below −slider.setSnapToTicks(true);The following is an example to create a JSlider that snap to the closest tick mark −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, 40);       slider.setMinorTickSpacing(10);     ... Read More

Can we remove the Title Bar of a Frame in Java?

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

1K+ Views

Yes, we can remove the title bar of a frame using the setUndecorated() method. Set it to TRUE if you don’t want to remove the Title Bar.The following is an example to remove the title bar of a frame in Java −Examplepackage my; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Login!");       JLabel label1, label2, label3;       frame.setLayout(new GridLayout(2, 2));       label1 = new JLabel("DeptId", SwingConstants.CENTER);       ... Read More

How to change the maximum value of a JSlider in Java?

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

309 Views

To change the maximum value of a slider in Java, use the setMaximum() method wherein set the maximum value.Let’s say the following is our slider in Java −JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 55); slider.setMinorTickSpacing(10); slider.setMajorTickSpacing(25); slider.setPaintTicks(true); slider.setPaintLabels(true);Now, set the maximum value −slider.setMaximum(150);The following is an example to change the maximum value of a slidier −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, ... Read More

I want to resize and position a JFrame in Java. How can I achieve that?

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

615 Views

To resize and position JFrame, use the Dimensions class. Here, we have set the bounds for the frame −int width = 500; int height = 400; Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); frame.setBounds((int) size.getWidth() - width, 0, width, height);The following is an example to resize and poisiton a frame −Examplepackage my; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame.setDefaultLookAndFeelDecorated(true);       JFrame frame = new JFrame("Register!");       JLabel label1, label2, label3;       ... Read More

How to create a nested JSplitPane in Java?

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

193 Views

Let us first create three components −JComponent one = new JLabel("Label One"); one.setBorder(BorderFactory.createLineBorder(Color.yellow)); JComponent two = new JLabel("Label Two"); two.setBorder(BorderFactory.createLineBorder(Color.orange)); JComponent three = new JLabel("Label Three"); three.setBorder(BorderFactory.createLineBorder(Color.blue));Now, we will split one and two components.JSplitPane split1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, one, two); split1.setDividerLocation(0.5); split1.setDividerSize(2);After that the above splitted pane would be splitted with component three making the process nested −JSplitPane split2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, split1, three ); split2.setDividerLocation(0.9); split2.setDividerSize(2);The following is an example to create nested JSplitPane −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSplitPane; public class SwingDemo {    public static void main(String[] ... Read More

How to set Row Header View for JScrollPane in Java?

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

475 Views

Set Row Header View using the setRowHeaderView() method. Let us first create a KScrollPane and set a list −List myList = new ArrayList(10); for (int index = 0; index < 20; index++) {    myList.add("List Item " + index); } final JList list = new JList(myList.toArray(new String[myList.size()])); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(list);Now, set the row header view −scrollPane.setRowHeaderView(new JLabel("All List Items "));The following is an example to set row header view for JScrollPane in Java −Examplepackage my; import java.awt.BorderLayout; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; public class SwingDemo {    public ... Read More

How to create a top-bottom split pane in Java?

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

269 Views

To create a top-bottom split pane, let us create two components and split them −JComponent one = new JLabel("Top Split"); one.setBorder(BorderFactory.createLineBorder(Color.MAGENTA)); JComponent two = new JLabel("Bottom Split"); two.setBorder(BorderFactory.createLineBorder(Color.ORANGE));Now, we will split them. The two components will be split one on top of the other using VERTICAL_PANE constant −JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, one, two);The following is an example to create a top-bottom split pane in Java −Examplepackage my; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSplitPane; public class SwingDemo {    public static void main(String[] a) {       JFrame frame = new JFrame("SplitPane Demo"); ... Read More

How to add JRadioButtonMenuItem in Java

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

136 Views

Let’s say we have a menu and within that we need to set the JRadioButtonMenuItem. Here’s the Menu −JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu);Now, set the editMenu and add it to the MenuBar −JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu);Now, create Options Menu and add it to the editMenu −JMenu optionsMenu = new JMenu("Options"); optionsMenu.setMnemonic(KeyEvent.VK_O); editMenu.add(optionsMenu);Now, set the JRadioButtonMenuItem and add it to the editMenu created above −ButtonGroup group = new ButtonGroup(); JRadioButtonMenuItem radioMenuItem = new JRadioButtonMenuItem("SmartInsertMode", true); radioMenuItem.setMnemonic(KeyEvent.VK_I); optionsMenu.add(radioMenuItem); group.add(radioMenuItem); JRadioButtonMenuItem radioMenuItem2 = new JRadioButtonMenuItem("SmartDeleteMode"); radioMenuItem.setMnemonic(KeyEvent.VK_D); optionsMenu.add(radioMenuItem2); group.add(radioMenuItem2);The following is an example add JRadioButtonMenuItem in Java −Examplepackage my; ... Read More

How to set the alignment of the JLabel content along the Y axis on the top in Java

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

128 Views

To set the alignment of the label’s content along the Y axis on the top, use the setVerticalAlignment() method and set the location. Let us first set a label component. We have set the label background color as well so that we can check the alignment of the label’s content properly −JLabel label = new JLabel("Favourite Movie"); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.GREEN); label.setForeground(Color.WHITE);Now, we will align the label content along the Y axis on the top by seeting location as TOP −label.setVerticalAlignment(JLabel.TOP);The following is an example to set the alignment of the JLabel content along the Y axis on the ... Read More

How to right-align a menu in the menu bar with Java?

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

777 Views

Let’s say we added a menu to the MenuBar −JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu);Add the glue component in between the menus to align some of them on the right, for example −menuBar.add(Box.createHorizontalGlue());The menu added after the usage of above method, would get right-aligned −JMenu sourceMenu = new JMenu("Source"); sourceMenu.setMnemonic(KeyEvent.VK_S); menuBar.add(sourceMenu);The following is an example to right-align a menu in the menu bar with Java −Examplepackage my; import java.awt.event.KeyEvent; import javax.swing.Box; 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[]) {       ... Read More

Advertisements