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
123
votes
6 answers

Newline in JLabel

How can I display a newline in JLabel? For example, if I wanted: Hello World! blahblahblah This is what I have right now: JLabel l = new JLabel("Hello World!\nblahblahblah", SwingConstants.CENTER); This is what is displayed: Hello…
mportiz08
  • 10,206
  • 12
  • 40
  • 42
116
votes
5 answers

How do I create a right click context menu in Java Swing?

I'm currently creating a right-click context menu by instantiating a new JMenu on right click and setting its location to that of the mouse's position... Is there a better way?
Wayne
  • 4,094
  • 4
  • 21
  • 11
115
votes
10 answers

Detect enter press in JTextField

Is it possible to detect when someone presses Enter while typing in a JTextField in java? Without having to create a button and set it as the default.
a sandwhich
  • 4,352
  • 12
  • 41
  • 62
113
votes
5 answers

How to add row in JTable?

Do you know how I can add a new row to a jTable?
oneat
  • 10,778
  • 16
  • 52
  • 70
112
votes
10 answers

Multi-line tooltips in Java?

I'm trying to display tooltips in Java which may or may not be paragraph-length. How can I word-wrap long tooltips?
Amanda S
  • 3,266
  • 4
  • 33
  • 45
112
votes
9 answers

How do I speed up the scroll speed in a JScrollPane when using the mouse wheel?

I see the method JScrollPane.setWheelScrollingEnabled(boolean) to enable or disable the mouse wheel scrolling. Is there any way to adjust the speed of the scrolling, though? It is, in my opinion, ludicrously slow. No matter what size I make the…
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
111
votes
7 answers

How to make a JTable non-editable

How to make a JTable non-editable? I don't want my users to be able to edit the values in cells by double-clicking them.
Siddharth Raina
  • 1,255
  • 2
  • 8
  • 6
111
votes
5 answers

Java: Difference between the setPreferredSize() and setSize() methods in components

What is the main difference between setSize() and setPreferredSize(). Sometimes I used setSize(), sometimes setPreferredSize(), sometimes one does what I want, sometimes the other. What call should I use for JFrames and JPanels?
David Robles
  • 9,477
  • 8
  • 37
  • 47
108
votes
9 answers

Java JTable setting Column Width

I have a JTable in which I set the column size as…
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241
108
votes
7 answers

Open a link in browser with java button?

How can I open a link in default browser with a button click, along the lines of button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open("www.google.com"); // just what is the 'open' method? …
Malasuerte94
  • 1,454
  • 3
  • 14
  • 18
104
votes
5 answers

Creating a custom JButton in Java

Is there a way to create a JButton with your own button graphic and not just with an image inside the button? If not, is there another way to create a custom JButton in java?
Anton
  • 12,285
  • 20
  • 64
  • 84
103
votes
6 answers

How to capture a JFrame's close button click event?

I want to call a method confirmExit() when the red close button of the title bar of a JFrame is clicked. How can I capture that event? I'd also like to prevent the window from closing if the user chooses not to proceed.
alxcyl
  • 2,722
  • 7
  • 31
  • 47
102
votes
9 answers

How to close a Java Swing application from the code

What is the proper way to terminate a Swing application from the code, and what are the pitfalls? I'd tried to close my application automatically after a timer fires. But just calling dispose() on the JFrame didn't do the trick - the window vanished…
Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
98
votes
7 answers

Convert a RGB Color Value to a Hexadecimal String

In my Java application, I was able to get the Color of a JButton in terms of red, green and blue; I have stored these values in three ints. How do I convert those RGB values into a String containing the equivalent hexadecimal representation? Such as…
Lalchand
  • 7,627
  • 26
  • 67
  • 79
96
votes
9 answers

JFrame Maximize window

I'm putting together a quick and dirty animation using swing. I would like the window to be maximized. How can I do that?
Nope
  • 34,682
  • 42
  • 94
  • 119