Found 4338 Articles for Java 8

How to center a Window in Java?

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

749 Views

To center a Window in Java, use the getCenterPoint() method. Set the width and height and use the below formulae to set bounds −Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); int width = 500; int height = 200; frame.setBounds(center.x - width / 2, center.y - height / 2, width, height);The following is an example to center a window −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; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame.setDefaultLookAndFeelDecorated(true);       JFrame frame = new JFrame("Register!");     ... Read More

How to set preferred Size for BoxLayout Manager in Java?

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

908 Views

Let’s say we have a Frame with layout set as BoxLayout −JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));Set the preferred size using setPreferredSize() and withing that specify the values for width and height −JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(100, 500));The following is an example to set preferred size for BoxLayout Manager in Java −Examplepackage my; import java.awt.Component; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), ... Read More

How to combine FlowLayout and BoxLayout in Java?

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

474 Views

To combine both the layouts, here we have created two panels −Frame f = new JFrame("Combining Layouts"); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel();Now, set the layouts accordingly −panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS)); panel2.setLayout(new FlowLayout());Then after adding components to both the panels, add them to the frame −f.add(panel1, BorderLayout.WEST); f.add(panel2, BorderLayout.CENTER);Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; public class SwingDemo {    public static void main(String[] args) {       JFrame f = new JFrame("Combining Layouts");       JPanel panel1 = new JPanel();       ... Read More

How to left align components vertically using BoxLayout with Java?

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

2K+ Views

To align components vertically, use the BoxLayout −JFrame frame = new JFrame(); frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));Now, create a Panel and add some buttons to it. After that set left alignment of components which are already arranged vertically using Component.LEFT_ALIGNMENT constant −JPanel panel = new JPanel(); JButton btn1 = new JButton("One"); JButton btn2 = new JButton("Two"); JButton btn3 = new JButton("Three"); JButton btn4 = new JButton("Four"); JButton btn5 = new JButton("Five"); panel.add(btn1); panel.add(btn2); panel.add(btn3); panel.add(btn4); panel.add(btn5); panel.setAlignmentX(Component.LEFT_ALIGNMENT);The following is an example to left align components vertically with BoxLayout −Examplepackage my; import java.awt.Component; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; ... Read More

How to center align component using BoxLayout with Java?

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

6K+ Views

Let us first create a panel and set some buttons −JPanel panel = new JPanel(); JButton btn1 = new JButton("One"); JButton btn2 = new JButton("Two"); JButton btn3 = new JButton("Three"); JButton btn4 = new JButton("Four"); JButton btn5 = new JButton("Five"); panel.add(btn1); panel.add(btn2); panel.add(btn3); panel.add(btn4); panel.add(btn5);Now, use the setAlignmentX() and within that specify alignment to the center of the component −panel.setAlignmentX(Component.CENTER_ALIGNMENT);The following is an example to center align component using BoxLayout −Examplepackage my; import java.awt.Component; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       ... Read More

How to display JTextArea in the form of a table with GridLayout in Java?

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

608 Views

Display a component in the form of rows and columns using the GridLayout. Here, we have set a panel, within which we will create a layout with 3 rows and 5 columns −JPanel panel = new JPanel(new GridLayout(3, 5, 5, 5));Now, loop through and display JTextArea from 1 to 15 i.e. 3 rows and 5 columns −for (int i = 1; i

How to display labels in the form of a 4 column table with GridLayout in Java?

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

753 Views

We will displays labels in a label with 5 rows and 4 columns using GridLayout −JPanel panel = new JPanel(new GridLayout(5, 4, 10, 10));Use a for loop and loop through 1 to 20 to display 20 labels −for (int i = 1; i

Java program to create three vertical columns with equal number of buttons in GridLayout

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

201 Views

Here, use GridLayout with some buttons in a Panel −JPanel btnPanel = new JPanel(new GridLayout(5, 2, 5, 5)) -Now, set the buttons −btnPanel.add(new JButton("First Button")); btnPanel.add(new JButton("Second Button")); btnPanel.add(new JButton("Third Button")); btnPanel.add(new JButton("Fourth Button")); btnPanel.add(new JButton("Fifth Button")); btnPanel.add(new JButton("Sixth Button")); btnPanel.add(new JButton("Seventh Button")); btnPanel.add(new JButton("Eighth Button")); btnPanel.add(new JButton("Ninth Button")); btnPanel.add(new JButton("Tenth Button")); btnPanel.add(new JButton("Eleventh Button")); btnPanel.add(new JButton("Twelfth Button"));The following is an example to create three vertical columns with equal number of buttons −Examplepackage my; import java.awt.BorderLayout; import java.awt.GridBagLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class SwingDemo {    public static void main(String[] args) {   ... Read More

How to create vertical button column with GridLayout in Java?

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

1K+ Views

To create a vertical button column, let us first create some buttons and set the layout as well −JPanel btnPanel = new JPanel(new GridLayout(10, 1, 10, 5)); btnPanel.add(new JButton("First Button")); btnPanel.add(new JButton("Second Button")); btnPanel.add(new JButton("Third Button")); btnPanel.add(new JButton("Fourth Button")); btnPanel.add(new JButton("Fifth Button")); btnPanel.add(new JButton("Sixth Button")); btnPanel.add(new JButton("Seventh Button")); btnPanel.add(new JButton("Eighth Button"));Above, we have set the GridLayout to create rows and columns with vertical and horizontal gap.The following is an example to create vertical button column with GridLayout −Examplepackage my; import java.awt.BorderLayout; import java.awt.GridBagLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class SwingDemo {    public static ... Read More

Java Program to create a layout using GridBagLayout

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

162 Views

The GridBagLayout creates a grid bag layout manager. It arranges the components in a horizontal and vertical manner.Here, we have a frame and panel. The panel has two components arranged using GridBagLayout −JFrame frame = new JFrame("Demo Frame"); JPanel panel = new JPanel(); JLabel label = new JLabel("Email-Id: "); JTextArea text = new JTextArea(); text.setText("Add id here..."); panel.setLayout(new GridBagLayout());Now, set the components to the panel −panel.add(label); panel.add(text);The following is an example to create a layout using GridBagLayout −Examplepackage my; import java.awt.GridBagLayout; import javax.swing.BorderFactory; 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 ... Read More

Advertisements