Found 131 Articles for Swing

What are the differences between JFrame and JDialog in Java?

raja
Updated on 07-Feb-2020 06:54:28

2K+ Views

JFrameThe components added to the frame are referred to as its contents, these are managed by the contentPane. To add a component to a JFrame, we must use its contentPane instead.A JFrame contains a window with title, border, (optional) menu bar and user-specified components.A JFrame can be moved, resized, iconified and it is not a subclass of JComponent.By default, JFrame is displayed in the upper-left corner of the screen. To display a frame at a specified location, we can use the setLocation(x, y) method in the JFrame class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JFrameDemo {    public static void main(String s[]) {       ... Read More

What are the differences between GridLayout and GridBagLayout in Java?

raja
Updated on 29-Apr-2025 19:16:31

8K+ Views

In Java, a GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles, and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size. Java GridLayout A GridLayout arranges the components in a rectangular grid. It arranges components in the cells, and each cell has the same size. Components are placed in columns and rows. GridLayout(int rows, int columns) takes two parameters that are a column and a row. Syntax The following is the ... Read More

Is Swing thread-safe in Java?

Alshifa Hasnain
Updated on 23-Apr-2025 17:17:58

2K+ Views

No, Java Swing components are not thread-safe in Java. This means that whenever Swing components should be accessed or changed, it is done mostly by using a single thread, the EDT. Why Swing Components are not thread-safe One of the main reasons Java Swing is not thread-safe is to simplify the task of extending its components. Another reason for that Java Swing is not thread-safe due to the overhead involved in obtaining and releasing locks and restoring the state. Below are some methods given by Swing for safe operation with its UI: SwingUtilities.invokeLater(): In ... Read More

How can we create a login form in Java?

Alshifa Hasnain
Updated on 16-Apr-2025 18:54:02

33K+ Views

We can develop a login form in Java using Java Swing technology. In this example, we can create two labels username and password, two text fields for the user to enter valid credentials,  and finally, one submit button. Once the user is able to enter the valid credentials in the two text fields, we can able to see Hello admin in the login form. Setting Up the Project We’ll use Java Swing, a GUI toolkit for building desktop applications. Required Libraries are: javax.swing.* (for Swing components) java.awt.* (for layout management) ... Read More

Move the first row to the end of the JTable in Java Swing

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

639 Views

To move the first row to the end of the table in Java, use the moveRow() method. It has three parameters. The first two parameters allows you to set the starting and ending row index to be moved. The last parameter sets the destination of the rows to be moved.As discussed above, move the first row to the end −tableModel.moveRow(0, 0, tableModel.getRowCount() - 1);The following is an example to move the first row to the end of the table −Examplepackage my; 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 ... Read More

How to apply adjustments to the next column of a JTable only, when the width of a column is changed in Java Swing?

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

187 Views

To apply adjustments to the next column only, use the setAutoResizeMode and set the mode. The mode here would be AUTO_RESIZE_NEXT_COLUMN. This will allow you to adjust only the next column even if any of the column header is dragged to resize.Let us first see an example to create a table −Examplepackage my; 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("Technology");       tableModel.addColumn("BCA"); ... Read More

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

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

625 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 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

What is Double-buffering in Java?

raja
Updated on 30-Jul-2019 22:30:26

2K+ Views

Double-buffering is the process of drawing graphics into an off-screen image buffer and then copying the contents of the buffer to the screen all at once.For the complex graphics, using double-buffering can reduce flickering issues.Java Swing automatically supports double-buffering for all of its components.Double-buffering is memory intensive, its use is only justified for components that are repainted very frequently or have particularly complex graphics to display.If a container uses double-buffering, any double-buffered children it has shared the off-screen buffer of the container, the required off-screen buffer is never larger than the on-screen size of the application.To enable double buffering, simply ... Read More

Why AWT components are heavy-weight while Swing components are light-weight in Java?

raja
Updated on 06-Feb-2020 10:46:30

2K+ Views

AWT stands for Abstract Window ToolKit and it supports Java GUI programming. It is a portable GUI library for Stand-alone Java applications/applets. The AWT provides the connection between our application and the native GUI while Java Swing implements a set of GUI components that build on AWT technology and it can provide a pluggable look and feel. Java Swing is implemented entirely in the Java programming language.First of all, by a heavy-weight, it means the code will take comparatively more time to load and it will consume more System resources. AWT is considered to be heavy-weight because its components are dependent ... Read More

Advertisements