Found 4338 Articles for Java 8

How to set the maximized bounds for a Frame in Java?

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

316 Views

To set maximized bounds, use the setMaximizedBounds() method. Here, we will create a frame, which when maximized will form a shape −JFrame frame = new JFrame("Login!");Above, we have created a frame and now we will use the Rectangle class to specify an area of coordinates −Rectangle bounds = new Rectangle(50, 10, 100, 200); Now, set maximized bounds − frame.setMaximizedBounds(bounds);The following is an example to set the maximized bounds for a frame −Examplepackage my; import java.awt.GridLayout; import java.awt.Rectangle; 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 { ... Read More

How to disable close button on a JFrame in Java?

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

759 Views

To disable the close button, let us first create a frame −JFrame frame = new JFrame("Login!");Use the setDefaultCloseOperation() to set the status of the close button. Set it to DO_NOTHING_ON_CLOSE to disable it −frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);The following is an example to disable close button on a JFrame 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 = ... Read More

How can I position JButtons vertically one after another in Java Swing?

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

480 Views

Position buttons vertically one after another using the Box class. Use the createVerticalBox() method that displays components from top to bottom −JButton button1 = new JButton("One"); JButton button2 = new JButton("Two"); JButton button3 = new JButton("Three"); Box box = Box.createVerticalBox(); box.add(button1); box.add(button2); box.add(button3);The following is an example to position buttons vertically one after another −Examplepackage my; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JButton button1 = new JButton("One");       JButton button2 = new JButton("Two");       JButton button3 = new JButton("Three");       ... Read More

How to set the text of the JLabel to be right-justified and vertically centered in Java?

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

573 Views

To set the text of the label component to be right- justified and vertically centered, you need to set the alignment while creating a new label.Set the label to be on the right -JLabel label = new JLabel("Name", JLabel.RIGHT);Here, we have set the size of the label as well as the color that includes foreground and background color -label.setPreferredSize(new Dimension(170, 70)); label.setOpaque(true); label.setBackground(Color.MAGENTA); label.setForeground(Color.WHITE);The following is an example to set the text of the label to be right-justified and vertically centered −Examplepackage my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public ... Read More

How to arrange components in a FlowLayout to be left-justified in Java?

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

239 Views

Use FlowLayout.LEFT to arrange components in a FlowLayout to be left-justified. The following is an example to arrange components in a FlowLayout to be left-justified −Examplepackage my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Internet Language");       frame.setLayout(new FlowLayout(FlowLayout.LEFT));       JLabel label = new JLabel("Top Internet Language");       label.setPreferredSize(new Dimension(240, 70));       label.setOpaque(true);       label.setBackground(Color.RED);       label.setForeground(Color.WHITE);       Font ... Read More

Set the content of the label horizontally and vertically centered in Java

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

215 Views

To simply set the content of the label horizontally and vertically centered, use the constant CENTER.JLabel label = new JLabel("Best IDE", JLabel.CENTER);The following is an example to set the content of the lable horizontally and verticaly centered −Examplepackage my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Frame");       frame.setLayout(new FlowLayout());       JLabel label = new JLabel("Best IDE", JLabel.CENTER);       label.setPreferredSize(new Dimension(190, 100));       label.setOpaque(true);   ... Read More

How to set the alignment of the JLabel content along the Y axis in the bottom with Java

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

362 Views

Set the alignment of the label’s content along the Y axis on the bottom, 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("Project Name"); 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 bottom by seeting location as BOTTOM −label.setVerticalAlignment(JLabel.BOTTOM);The following is an example to set the alignment of the label content along the Y axis in the bottom ... Read More

How to create an invisible fixed width component between two components in Java?

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

221 Views

Use the createHorizontalStrut() method to create an invisible width component between two components. Let’s say we have some button and we are creating a fixed width between them −box.add(button4); box.add(Box.createHorizontalStrut(50)); box.add(button5); box.add(Box.createHorizontalStrut(30)); box.add(button6);The following is an example to create an invisible fixed width component between two components −Examplepackage my; import java.awt.BorderLayout; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Groups");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("CSK");       JButton button2 = new JButton("DC"); ... Read More

Set the alignment of the JLabel content along the X axis on the right in Java

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

142 Views

To set the alignment of the label’s content along the X axis on the right, 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("Stationery"); 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 X axis on the top by seeting location as RIGHT −label.setHorizontalAlignment(JLabel.RIGHT);The following is an example: to set the alignment of the label content along the X axis on the right ... Read More

Java Program to set different height for multiple rows in JTable

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

127 Views

To set different height for multiple rows, use the setRowHeight() method for separate rows for which you want to increase the row height. Let us first see an example to create a table with same height for all the rows −Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       DefaultTableModel tableModel = new DefaultTableModel();       JTable table = new JTable(tableModel);       tableModel.addColumn("Language/ Technology");       tableModel.addColumn("Text Tutorial");       tableModel.addColumn("Video Tutorial");     ... Read More

Advertisements