Found 133 Articles for Swing

What are the differences between JTextField and JTextArea in Java?

raja
Updated on 07-Feb-2020 06:48:30

3K+ Views

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.JTextFieldA JTextFeld is one of the most important components that allow the user to an input text value in a single line format.A JTextField will generate an ActionListener interface when we trying to enter some input inside it.The JTextComponent is a superclass of JTextField that provides a common set of methods used by JTextfield.The important methods in the JTextField class are setText(), getText(), setEnabled(), etc.Exampleimport javax.swing.*; import java.awt.*; public class JTextFieldTest {    public static ... Read More

How can we implement a splash screen using JWindow in Java?

raja
Updated on 07-Feb-2020 06:51:51

473 Views

A JWindow is a container that can be displayed anywhere on the user desktop. It does not have the title bar, window management buttons,  etc like a JFrame.The JWindow contains a JRootPane as its only child class. The contentPane can be the parent of any children of the JWindow. Like a JFrame, a JWindow is another top-level container and it is as an undecorated JFrame. It does not have features like a title bar, windows menu, etc. A JWindow can be used as a splash screen window that displays once when the application is launched and then automatically disappears after a few seconds.Exampleimport javax.swing.*; import java.awt.*; public class ... Read More

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 07-Feb-2020 06:09:22

5K+ Views

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. Each GridBagLayout object maintains a dynamic, rectangular grid of cells with each component occupying one or more cells called Component display area.GridLayoutA GridLayout arranges the components in a rectangular grid. It arranges component 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 ... Read More

Is Swing thread-safe in Java?

raja
Updated on 07-Feb-2020 06:16:23

1K+ Views

No, Java Swing components are not thread-safe in Java.Why Swing Components are not thread-safeOne of the main reason for Java Swing is not thread-safe is to simplify the task of extending its components.Another reason for the Java Swing is not thread-safe due to the overhead involved in obtaining and releasing locks and restoring the state.Some of the Java Swing component methods will support multi-threaded access like repaint(), revalidate(), and invalidate() methods of JComponent class.Event Dispatch Thread (EDT)The Java Swing components can only be accessed from the Event Dispatch Thread (EDT) once a component is available for painting onscreen. The EDT thread is the thread that ... Read More

How can we create a login form in Java?

raja
Updated on 07-Feb-2020 06:20:10

23K+ 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.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class LoginDemo extends JFrame implements ActionListener {    JPanel panel;    JLabel user_label, password_label, message;    JTextField userName_text;    JPasswordField password_text;    JButton submit, cancel;    LoginDemo() {       // Username Label       ... 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

420 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

122 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

458 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

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

5K+ Views

Let us first create a table with DefaulTabelMode −DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel);Now, add a column to the table −tableModel.addColumn("Languages");The insertRow() method will now add a row −tableModel.insertRow(0, new Object[] { "CSS" }); tableModel.insertRow(0, new Object[] { "HTML5" }); tableModel.insertRow(0, new Object[] { "JavaScript" }); tableModel.insertRow(0, new Object[] { "jQuery" });The following is an example to add a new row to JTable −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 Exception {       DefaultTableModel tableModel = new DefaultTableModel();     ... Read More

Advertisements