Java Articles - Page 322 of 745

How to set Row Header View for JScrollPane in Java?

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

713 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

340 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

224 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

207 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

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

How to set color to MatteBorder in Java?

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

198 Views

Set color to MatteBorder using the Color class −Border border = new MatteBorder(5, 10, 5, 5, Color.BLUE);Now, set it to a button component in Java −JButton button = new JButton("Matte Border"); button.setBorder(border);The following is an example to set color to MatteBorder in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.MatteBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);       Border border = new MatteBorder(5, ... Read More

How to create a Vertical menu bar in Java?

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

1K+ Views

Let us create a MenuBar −JMenuBar menuBar = new JMenuBar();Now, set its layout to create a Vertical Menu Bar with GridLayout −menuBar.setLayout(new GridLayout(0, 1));The following is an example to create a Vertical menu bar in Java −Examplepackage my; import java.awt.GridLayout; 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();       menuBar.setLayout(new GridLayout(0, 1));       JMenu fileMenu = new JMenu("File");       ... Read More

How can I set an EtchedBorder from BorderFactory class to a component in Java?

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

225 Views

Set an EtchedBorder from BorderFactory class −EtchedBorder etchedBorder = (EtchedBorder)BorderFactory.createEtchedBorder();Now, set it for a component −JButton button = new JButton("Etched Border"); button.setBorder(etchedBorder);The following is an example to set an EtchedBorder from BorderFactory class to a component −Examplepackage my; import java.awt.BorderLayout; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);       EtchedBorder etchedBorder = (EtchedBorder)BorderFactory.createEtchedBorder();       JButton raisedButton = new JButton("Raised Border");   ... Read More

How to create a left-right split pane in Java?

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

289 Views

To create a left-right split pane, let us create two components and split them −JComponent one = new JLabel("Left Split"); one.setBorder(BorderFactory.createLineBorder(Color.MAGENTA)); JComponent two = new JLabel("Right Split"); two.setBorder(BorderFactory.createLineBorder(Color.ORANGE));Now, we will split them. The two components will be split one to the left of the other using HORIZONTAL_PANE constant −JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, one, two);The following is an example to create a left-right 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 ... Read More

How to add a new row to JTable with insertRow() in Java Swing

Alshifa Hasnain
Updated on 24-Dec-2024 17:46:46

8K+ Views

The JTable component in Java Swing is widely used for displaying and managing tabular data. Adding a new row dynamically to a JTable can enhance user interactivity, especially in applications that require real-time data manipulation. In this article we will tell you a step-by-step procedure, to use the insertRow() method to add a new row to a JTable, complete with a sample implementation and an algorithm. What is JTable in Java Swing? JTable is a Swing component used for displaying and editing tabular data. It is highly customizable and supports features like sorting, selection, and dynamic data manipulation, making it ... Read More

Advertisements