Questions tagged [swing]

Swing is a user-interface toolkit in Java and is shipped with the standard Java SDK. It is contained within the package javax.swing.

Swing is a GUI toolkit that is packaged with the standard Java SDK (since 1.2). Recently it is no longer being actively enhanced and has largely been superseded by JavaFX.

General information

Specialized articles

Essential topics in Swing programming

Layout managers: Layout managers are responsible for determining the size and position of components in a Container.

Swing threading rules: Swing is single threaded and any access to Swing components should happen on the Swing thread (the Event Dispatch Thread or EDT).

  • Start with the Concurrency in Swing tutorial
  • Detect EDT violations using one of the approaches cited here
  • Use SwingWorker to perform lengthy operations in the background and update the UI on the EDT
  • Use a Swing Timer for animation
81029 questions
548
votes
9 answers

The Use of Multiple JFrames: Good or Bad Practice?

I'm developing an application which displays images, and plays sounds from a database. I'm trying to decide whether or not to use a separate JFrame to add images to the database from the GUI. I'm just wondering whether it is good practice to use…
Peddler
  • 6,045
  • 4
  • 18
  • 22
506
votes
9 answers

Should I avoid the use of set(Preferred|Maximum|Minimum) size methods in Java Swing?

Several times I've been criticized for having suggested the use of the following methods: setPreferredSize() setMinimumSize() setMaximumSize() on Swing components. I don't see any alternatives to their use when I want to define proportions between…
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
367
votes
14 answers

How to add an image to a JPanel?

I have a JPanel to which I'd like to add JPEG and PNG images that I generate on the fly. All the examples I've seen so far in the Swing Tutorials, specially in the Swing examples use ImageIcons. I'm generating these images as byte arrays, and are…
Leonel
  • 28,541
  • 26
  • 76
  • 103
292
votes
16 answers

How to programmatically close a JFrame?

What's the correct way to get a JFrame to close, the same as if the user had hit the X close button, or pressed Alt + F4 (on Windows)? I have my default close operation set the way I want, via: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); It…
JustJeff
  • 12,640
  • 5
  • 49
  • 63
246
votes
9 answers

What is the difference between Swing and AWT?

Can someone please explain me what's the difference between Swing and AWT? Are there any cases where AWT is more useful/advised to use than swing or vice-versa?
Samiksha
  • 6,122
  • 6
  • 29
  • 28
246
votes
14 answers

Value Change Listener to JTextField

I want the message box to appear immediately after the user changes the value in the textfield. Currently, I need to hit the enter key to get the message box to pop out. Is there anything wrong with my code? textField.addActionListener(new…
user236501
  • 8,538
  • 24
  • 85
  • 119
237
votes
12 answers

How to set JFrame to appear centered, regardless of monitor resolution?

While working with Java, I find it hard to position my main window in the center of the screen when I start the application. Is there any way I can do that? It doesn't have to be vertically centered, horizontal alignment is the more important goal…
AmateurProgrammer
  • 2,609
  • 3
  • 19
  • 14
231
votes
5 answers

Java Swing revalidate() vs repaint()

I'm putting together a Swing application where I often want to replace the contents of a JPanel. To do this, I'm calling removeAll(), then adding my new content, then calling revalidate(). However I'm finding that the old content is still actually…
William
  • 13,332
  • 13
  • 60
  • 73
227
votes
9 answers

Java GUI frameworks. What to choose? Swing, SWT, AWT, SwingX, JGoodies, JavaFX, Apache Pivot?

There is quite a lot of gui frameworks out there for java, but what is recognized as today's framework of choice? The following is my understanding of the different frameworks, please correct me if im wrong. This is a very loosely defined set of…
netbrain
  • 9,194
  • 6
  • 42
  • 68
211
votes
6 answers

Swing vs JavaFx for desktop applications

I have a very big program that is currently using SWT. The program can be run on both Windows, Mac and Linux, and it is a big desktop application with many elements. Now SWT being somewhat old I would like to switch to either Swing or JavaFX. And I…
Quillion
  • 6,346
  • 11
  • 60
  • 97
187
votes
7 answers

Copying text to the clipboard using Java

I want to copy text from a JTable's cell to the clipboard, making it available to be pasted into other programs such as Microsoft Word. I have the text from the JTable, but I am unsure how to copy it to the clipboard.
user765134
  • 1,873
  • 2
  • 12
  • 4
184
votes
1 answer

DTO and DAO concepts and MVC

Why do we use DTO and DAO, and when should we use them. I am developing a GUI Java software to do with inserting, editing, deleting data. But I am struggling to distinguish between DTO/DAO and Model, View, Controller (MVC) structure? Are they…
Hoody
  • 2,942
  • 5
  • 28
  • 32
173
votes
6 answers

Providing white space in a Swing GUI

A GUI with no white space appears 'crowded'. How can I provide white space without resorting to explicitly setting the position or size of…
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
169
votes
8 answers

JComboBox Selection Change Listener?

I'm trying to get an event to fire whenever a choice is made from a JComboBox. The problem I'm having is that there is no obvious addSelectionListener() method. I've tried to use actionPerformed(), but it never fires. Short of overriding the model…
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
167
votes
4 answers

How do I set a JLabel's background color?

In my JPanel, I set the background of a JLabel to a different color. I can see the word "Test" and it's blue, but the background doesn't change at all. How can I get it to show? this.setBackground(Color.white); JLabel label = new…
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
1
2 3
99 100