Found 9150 Articles for Object Oriented Programming

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

194 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

192 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

214 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

277 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

How to set the alignment of the JLabel content along the X axis on the left in Java

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

150 Views

To set the alignment of the label content along the X axis on the left, use the setHorizontalAlignment() 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("Country "); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.BLUE); label.setForeground(Color.WHITE);Now, we will align the label content along the X axis on the left by seeting location as LEFT −label.setHorizontalAlignment(JLabel.LEFT);The following is an example to set the alignment of the JLabel content along the X axis on the ... Read More

Add multiple number input fields with JOptionPane and display the sum in Console with Java

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

2K+ Views

At first, set multiple number input fields −JTextField text1 = new JTextField(10); JTextField text2 = new JTextField(10); JTextField text3 = new JTextField(10); JTextField text4 = new JTextField(10); JTextField text5 = new JTextField(10); JTextField text6 = new JTextField(10); JTextField text7 = new JTextField(10); JTextField text8 = new JTextField(10); panel.add(text1); panel.add(text2); panel.add(text3); panel.add(text4); panel.add(text5); panel.add(text6); panel.add(text7); panel.add(text8);Now, let us add the values of the multiple number input fields created above −System.out.println(Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText()) +    Integer.parseInt(text3.getText())+ Integer.parseInt(text4.getText())+    Integer.parseInt(text5.getText())+ Integer.parseInt(text6.getText())+    Integer.parseInt(text7.getText())+ Integer.parseInt(text8.getText()));Above, we have displayed the sum in the Console.The following is an example to sum multiple number input fields with ... Read More

How to use JOptionPane with Array Elements in Java?

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

1K+ Views

Let us first create and array and add elements −String[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" };Now, set the above array elements to the JOptionPane −String res = (String) JOptionPane.showInputDialog(null, "Which sports you play the most?", "Sports",    JOptionPane.PLAIN_MESSAGE, null, sports, sports[0]);Above, we have also set the initial value i.e. sports(0).The following is an example to use JOptionPane with array elements in Java −Examplepackage my; import javax.swing.JOptionPane; public class SwingDemo {    public static void main(String[] args) {       String[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" };       ... Read More

Advertisements