Java Swing Archives - Asjava Java development blog Tue, 05 Mar 2024 10:57:11 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.3 https://asjava.com/wp-content/uploads/2024/03/cropped-javascript-736400_640-32x32.png Java Swing Archives - Asjava 32 32 Using JLabel https://asjava.com/java-swing/using-jlabel/ Tue, 26 Dec 2023 10:49:00 +0000 https://asjava.com/?p=54 Swing is a toolkit for creating a graphical user interface (GUI) in the Java programming language. It is part of the Java Foundation Classes (JFC) library.

The post Using JLabel appeared first on Asjava.

]]>
Swing is a toolkit for creating a graphical user interface (GUI) in the Java programming language. It is part of the Java Foundation Classes (JFC) library.

Swing was developed to provide a more functional set of program components for creating a graphical user interface than the previous AWT toolkit. Swing components support specific look-and-feel modules that are dynamically connected. Thanks to them, it is possible to emulate the graphical interface of the platform (that is, you can dynamically connect other, specific for a given operating system look and feel to the component). The main disadvantage of such components is their relatively slow operation, although this has not been confirmed recently due to the growth of personal computers. The positive side is the universality of the interface of the created programs on all platforms.

JLabel

This is the simplest component available in Java Swing. With JLabel, you can display text with an icon. If you need a component to display a message to the user, or to make a text label for the input field, or to show an icon, use JLabel. The text that the JLabel displays cannot be selected, only viewed.

To create a JLabel object, which we will then have at our disposal in the window, you can use the constructor with a string parameter
public JLabel (String text)

The string parameter is the text that will be displayed in the JLabel. In addition, the text that will be displayed in the JLabel can be set using the setText method. The only parameter of the method is the string of text to be displayed String.

JLabel allows you to customize the font that will be used to display the text. The font is set using the
setFont method of the JLabel class
This method is passed a Font object as a parameter.

What do we want when we specify or create a font?

  • The first thing is the typeface, Verdana or Courier New, or something else;
  • The second is the font size and;
  • third – whether the font will be bold or normal, or maybe italicized.

Therefore, the appropriate constructor for Font is the one that contains all three parameters at once – public Font (String name, int style, int size). The first parameter, as is already clear, is the name of the font. If this parameter is not specified, the default font is used. The second parameter sets the style – bold, normal, or italic. Here you need to pass one of the constants of the Font object – Font.BOLD (bold), Font.PLAIN (normal or flat) and Font.ITALIC (italic). And the third parameter is the font size. The Font object can be created like this:

Font font = new Font("Verdana", Font.PLAIN, 11);

After setting the text and font in JLabel, sometimes you need to determine the vertical and horizontal alignment of the text. This is done using two methods: setVerticalAlignment and setHorizontalAlignment, respectively. As parameters for setVerticalAlignment, you need to use something from the following list:

  • JLabel.TOP (align to the top edge),
  • JLabel.CENTER, and
  • JLabel.BOTTOM (align to the bottom edge).
  • For the setHorizontalAlignment method, there are more parameters –
  • JLabel.LEFT (left alignment),
  • JLabel.CENTER (centered),
  • JLabel.RIGHT (on the right edge),
  • JLabel.LEADING and
  • JLabel.TRAILING.

The last two constants are interesting. For languages in which the text is written from left to right, JLabel.LEADING is the left edge, and JLabel.TRAILING is the right edge. For languages that are written from right to left, JLabel.LEADING is the right edge and JLabel.TRAILING is the left edge.

JLabel allows you to display an icon along with the text. To set the icon, use the setIcon method. The only parameter of the method is the Icon object. When an icon is set for the JLabel, you can set the location of the text relative to the icon. To do this, use the setVerticalTextPosition and setHorizontalTextPosition methods. The same constants are used as parameters as when aligning text.

The post Using JLabel appeared first on Asjava.

]]>
How Swing works https://asjava.com/java-swing/how-swing-works/ Sun, 17 Dec 2023 10:54:00 +0000 https://asjava.com/?p=58 Unlike other graphical program interfaces (hereafter APIs), Swing works on a single thread model. This single thread is responsible for both the GUI

The post How Swing works appeared first on Asjava.

]]>
Unlike other graphical program interfaces (hereafter APIs), Swing works on a single thread model. This single thread is responsible for both the GUI (graphical user interface) and the processing of all events received from the user. This means that the developer must pay special attention to the processing of everything that touches the GUI in any way, as well as to how long it will take to process a particular event.

Neglecting this may result in an application that is slower to respond to user actions or fails to work at all. Worse, the application may throw an emergency exception in the event stream, resulting in unpredictable behavior of the entire application.

Single thread

The Swing API is entirely designed to run in a single thread. This means that when that single thread is blocked or slowed down, the entire GUI will run slower or stop responding to user actions altogether. To avoid this, the application should do most of its work in a separate thread and only interact with the event thread when it is handling an event or when there is a need to update (or redraw) the GUI.

Fortunately, most of the Swing API is already designed for this multi-threaded approach, but that’s if the application conforms to the design fundamentals of that API. The pitfalls are not difficult to avoid. The difficulties arise when dealing with events.

Unsafe operations in threads

Another common situation is trying to update a GUI from outside of an event stream. This situation is the exact opposite of the one we discussed above. Listing 2 shows an example where we have more than one thread in an application and a third-party thread tries to update the GUI. While this code may look perfectly safe (and in fact, in a simple situation like this, it probably is safe) – it’s a recipe for getting a lot of no-one’s problems.

Because Swing is designed on a single-threaded model, its API is not synchronized and therefore not protected from attempts by other threads to modify its data. This means that we can get a dangerous collision in the GUI if it tries to update items from any other thread rather than directly from the event thread. Such actions can lead to data corruption, exception throwing, and a host of other dangerous problems that are damn hard to detect and debug. To avoid this problem, you should always update the GUI from the event stream.

Event Stream

If we can’t work in an event stream, how will our application do anything at all? It’s simple. Whenever we’re dealing with a situation where we know an event might take a long time to process, bring it into the worker thread.

Listing 3 is a rewrite of Listing 1, where instead of blocking the event thread until the wait method completes, we create an anonymous inner class that extends java.lang.Thread that will call the wait method for us. In this version of the code, the event will no longer wait for the wait method to complete, but will simply return control to the rest of the application (passing the event to other listeners) after the new thread has initialized and started. As a result, we have a fast response to events and not a slow GUI.

The post How Swing works appeared first on Asjava.

]]>