
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9150 Articles for Object Oriented Programming

2K+ Views
In this article, we will learn to determine when a Frame or Window is closing in Java. We will create a simple window in Java Swing titled "Demo". It displays styled text (italic, black on orange) using a text pane inside a scrollable area. The program also listens for window-closing events, printing a message when you close it. This basic example is how to build a GUI with text styling and event handling.Steps to determine when a Frame or Window is closing Following are the steps to determine when a Frame or Window is closing in Java − ... Read More

922 Views
To determine when a Window is opened in Java, use the WindowListener, which is the listener interface for receiving window events..WindowListener listener = new WindowAdapter() { public void windowOpened(WindowEvent evt) { Frame frame = (Frame) evt.getSource(); System.out.println("Opened "+frame.getTitle()); } };Above, we have used the windowOpened() method, which is invoked when a window has been opened −public void windowOpened(WindowEvent evt) { Frame frame = (Frame) evt.getSource(); System.out.println("Opened "+frame.getTitle()); }The following is an example to determine when a frame or window is opened in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import ... Read More

482 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

985 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

630 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

862 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

364 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

367 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

564 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

334 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