Questions tagged [swing]

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

Swing is the GUI toolkit packaged with the standard Java SDK (since 1.2).

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
78220 questions
544
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
  • 5,985
  • 4
  • 16
  • 22
490
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 alternative to their use when I want to define proportions between…
Heisenbug
  • 37,414
  • 27
  • 126
  • 181
351
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 they…
Leonel
  • 26,575
  • 25
  • 73
  • 95
270
votes
17 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 does…
JustJeff
  • 11,830
  • 5
  • 43
  • 62
233
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
  • 5,852
  • 6
  • 26
  • 28
228
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
  • 8,694
  • 6
  • 37
  • 66
224
votes
12 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
  • 7,930
  • 23
  • 80
  • 116
218
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
  • 12,346
  • 13
  • 54
  • 72
208
votes
11 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,319
  • 3
  • 15
  • 14
203
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
  • 5,757
  • 10
  • 55
  • 83
166
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
  • 163,965
  • 36
  • 203
  • 405
163
votes
4 answers

Java Look and Feel (L&F)

I am developing a desktop application with Java Swing for my personal use.I am in need of some beautiful Look and Feel for my application. How can I do it using Java or a 3rd party API?
rajesh
  • 1,763
  • 3
  • 12
  • 5
158
votes
10 answers

Java Desktop application: SWT vs. Swing

I'm a web developer at day and thinking about building my first real desktop application. The idea is to build a tool that automates a very repetitive task in a web application where no API is available. I know I want to use Java. I used it before…
janpio
  • 9,466
  • 12
  • 54
  • 99
157
votes
5 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,573
  • 2
  • 10
  • 4
156
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
  • 6,750
  • 2
  • 21
  • 38
1
2 3
99 100