Found 131 Articles for Swing

How can we implement the word wrap JTableHeader of a JTable in Java?

Alshifa Hasnain
Updated on 05-May-2025 18:32:47

821 Views

In this article, we will learn to implement the word wrap JTableHeader of a JTable in Java. The Java Swing default JTableHeader does not support word wrapping. This is a problem if you have long column headers. We can implement this by customizing the DefaultTableModel class or the AbstractTableModel class. JTableHeader A JTableHeader is a subclass of the JComponent class. When we create a JTable object, the constructor creates a new JTableHeader object to manage the table component's header. Syntax The following is the syntax for JTableHeader Declaration: public class JTableHeader extends JComponent The JTableHeader object is associated with ... Read More

How can we set the shortcut key to a JButton in Java?

Alshifa Hasnain
Updated on 05-May-2025 18:32:59

2K+ Views

In this article, we will learn to set the shortcut key to a JButton in Java. In Swing-based applications, we can implement the addition of keyboard shortcuts to buttons to enable quicker navigation by the user. What is a JButton? A JButton is a subclass of AbstractButton, and it can be used to add platform-independent buttons to a Java Swing application. When the button is pressed or clicked, a JButton can generate an ActionListener interface; it can also generate the MouseListener and KeyListener interfaces. Setting the Shortcut Key for JButton We can also set the shortcut keys for a JButton ... Read More

How to change each column width of a JTable in Java?

raja
Updated on 29-Apr-2025 19:13:05

9K+ Views

In this article, we will learn to change each column width of a JTable in Java. JTable is Swing's strongest component for displaying and editing tabular information. Adjusting column widths appropriately to display content is among the most prevalent needs for customization. JTable A JTable is a subclass of JComponent for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. The DefaultTableModel class can extend AbstractTableModel and it can be used to add the rows and columns to a JTable dynamically. Syntax The following is ... Read More

How to display a bold text inside the JTextArea in Java?

Alshifa Hasnain
Updated on 12-May-2025 18:38:50

2K+ Views

JTextArea is one of the most common Swing-based components. By default, it does not support text formatting such as bold, italics, or colored text inside a textarea. In this article, we will learn to display bold text inside the JTextArea in Java. What is a JTextArea? A JTextArea class can extend JTextComponent and allow a user to enter multiple lines of text inside it. A JTextArea can generate a CaretListener interface, which can listen to caret update events. Syntax The following is the syntax for JTextArea initialization: JTextArea textArea = new JTextArea(); Adding Bold Text Inside the JTextArea We ... Read More

What is the importance of JSeparator class in Java?

Alshifa Hasnain
Updated on 22-Apr-2025 18:28:00

237 Views

In this article, we will learn about the importance of the JSeparator class in Java. In GUI programming, appearance is highly important in creating good and user-friendly Java programs. Java Swing provides the JSeparator class as a simple but effective means of accomplishing this. What is JSeparator? A JSeparator is a horizontal or vertical line or an empty area used to split the components. A class called JSeparator is used to paint a line in order to divide the components within a Layout. The simplest way to insert a separator into a menu or a toolbar is by invoking the ... Read More

What is the importance of JViewport class in Java?

raja
Updated on 11-Feb-2020 11:11:40

479 Views

JViewportA JViewport class defines the basic scrolling model and it is designed to support both logical scrolling and pixel-based scrolling.The viewport's child called the view is scrolled by calling JViewport.setViewPosition() method.A JViewport class supports logical scrolling, that is a kind of scrolling in which view coordinates are not pixels.To support a logical scrolling, JViewport defines a small set of methods that can be used to define the geometry of a viewport and a view. By default, these methods just report the pixel dimensions of the viewport and view.Exampleimport java.awt.*; import javax.swing.*; public class JViewportTest extends JFrame {    public JViewportTest() {       setTitle("JViewport Test"); ... Read More

What are the differences between paint() method and repaint() method in Java?

Alshifa Hasnain
Updated on 15-Apr-2025 19:15:15

7K+ Views

In this article, we will learn about the differences between the paint() method and the repaint() method in Java. In the AWT and Swing frameworks, rendering graphical components is done in two different roles by the two methods paint() and repaint(). The paint() Method This method holds instructions to paint this component. In Java Swing, we can change the paintComponent() method instead of paint() method as paint calls paintBorder(), paintComponent() and paintChildren() methods. We cannot call this method directly instead we can call repaint(). Syntax The following is the syntax: public void paint(Graphics g) { // paint() method ... Read More

How can we catch a double click and enter key events for a JList in Java?

Alshifa Hasnain
Updated on 09-Jun-2025 14:19:19

829 Views

In this article, we will learn to catch a double click and enter key events for a JList in Java. We will be using Java Swing to detect double click and Enter key events on a JList. When we double click an item in the list or press "Enter" key after selecting an item, the selected item is displayed. What is a JList? A JList can extend the JComponent class, which allows us to choose either a single or multiple selections. A JList can generate a ListSelectionListener interface, and it includes one abstract method, valueChanged(). Below is the graphical representation ... Read More

What are the differences between a Font and a FontMetrics in Java?

raja
Updated on 29-Apr-2025 19:12:54

2K+ Views

The Font class in Java is responsible for setting screen fonts, which are mapped to characters of the language in specific regions. But a FontMetrics class is said to be a font metrics object that encapsulates the data about the rendering of a specific font on a particular screen. Font A Font class can be used to create an instance of a Font object to set the font for drawing text, labels, text fields, buttons, etc, and it can be specified by its name, style, and size. Syntax The following is the syntax for Font initialization: Font font = new ... Read More

How can we set the background color to a JPanel in Java?

Alshifa Hasnain
Updated on 08-May-2025 18:46:11

16K+ Views

In this article, we will learn to set the background color of a JPanel in Java. When designing GUIs in Swing, changing the background color of panels is important for creating visually appealing applications. What is a JPanel? A JPanel is a container, and it is an invisible component in Java. The FlowLayout is a default layout for a JPanel. We can add most of the components, like buttons, text fields, labels, tables, lists, trees, etc., into a JPanel. Syntax The following is the syntax for JPanel initialization: JPanel panel = new JPanel(); Methods The important methods of JPanel are: ... Read More

Previous 1 ... 5 6 7 8 9 ... 14 Next
Advertisements