New answers tagged swing
1
vote
Resize JPanel with animation
Try to use only one timer and add a boolean to track the direction and make sure that the animation is happening in one direction
public class Main {
private static final int INITIAL_HEIGHT = 150;
...
0
votes
How to set image size in JTextPane with HTML and CSS (Java, Swing)?
As Gilbert Le Blank mentioned in the comment, the width block shall be in <img> statement. So the HTML string should look like this:
<html>
<body>
<p color="red"&...
0
votes
Accepted
How can I make the tabs in the menu bar with rounded corners and margins in Java/Swing using FlatLaf Theme Editor?
Okay i figured it out. I should have used MenuBar.selectionInsets along with MenuBar.itemMargins. So the FlatLaf.properties file now looks like this:
MenuBar.itemMargins = 10, 15, 10, 15
MenuBar....
0
votes
Loading resources like images while running project distributed as JAR archive
Step-1:
check your .Jar file
open the .jar file in 7-zip or winScp and look for the resources folder.(resources you need in your program)
ex: I have added Images folder inside src/resources folder so ...
0
votes
Accepted
How to Set JComponent to Traverse with TAB instead of CTRL+TAB without using isManagingFocus()
Yes, I know this question is five years old, and yes, I know that I'm the one who originally submitted it. However, I have returned to this issue and finally found a way to solve it. The main impetus ...
0
votes
setLocationRelativeTo(null) is not centering the frame
import javax.swing.JFrame;
public class Maze {
public static void main(String[] args) {
new Maze();
}
public Maze(){
JFrame f = new JFrame();
f.setTitle("...
0
votes
How can I create a bar in the bottom of a Java app, like a status bar?
You should use the Layered Pane of the frame you are working on:
JPanel statusPanel = new JPanel();
statusPanel.setBorder(new MatteBorder(1, 0, 0, 0, Color.gray));
statusPanel.setLayout(...
1
vote
How to use a TableCellRenderer to display an object in a table in Swing
You are, in fact, selecting rows—you just can’t see it. Notice how the TableCellRenderer method has an isSelected argument? Your renderer is not doing anything with it, so it renders selected and ...
0
votes
"Flowing" JToolBar
It seems, it's not possible OOB
The goal can be achieved by adding a component listener to the toolbar (or using a custom layout, but I'll let camickr post it if they wish)
Notice the listener code ...
0
votes
Why doesn't this code swap images, as intended, when I press any key?
The problem is that you add your panels panels[0] and panels[1] at the same location BorderLayout.WEST in your JFrame instance. When one position in a BorderLayout is filled, it gets overwritten when ...
-1
votes
HTMLEditorKit and Custom tags in the JEditorPane
http://java-sl.com/custom_tag_html_kit.html is no longer available.
A more general approach is described here Which HTML tags are supported in Swing components ?.
1
vote
Display adjacent Java Swing Polygons without gap even when using antialiasing
but the polygon is drawn/filled twice and takes more than twice the time to just fill it.
Not sure why painting would be double.
Maybe you can use an Area for the painting:
import javax.swing.*;
...
2
votes
Accepted
"Stretchable" JLabel with dynamic text
You are implementing a feature that does already exist. Apparently, there is a bug in the Basic/Metal Look&Feel that makes it calculating with wrong sizes when no font has been set.
There are two ...
0
votes
To set delay on a button click in java?
This is a great tutorial on Java Swing timers that I've found: https://www.youtube.com/watch?v=XHd2s9hV8Tc
Hope it helps!
The source code from the video is:
import javax.swing.Timer;
import java.awt....
0
votes
How to pause execution for X milliseconds in AWTEventQueue
To achieve your goal, you can use the Timer class of Swing:
timer = new Timer( 1500, e -> {
panel.setBackground( Color.darkGray );
frame.setTitle( "XYZ" );
timer.stop();
} );
...
0
votes
How to test a hybrid Swing / Compose Desktop application
Here is a Compose for Desktop application.
https://github.com/philipplackner/StopWatchComposeDesktop
Here is how it looks.
I will assume that you know how to set it up. I will use this to show a ...
0
votes
Swing GUI does not show or show with errors when launched on Mac
Hard to believe, but I hit this wall yesterday, 10+ years later. A similar exception within the same method. In my case, it happened when trying to display a JDialog that happens to have a JMenuBar ...
1
vote
How to make translucent image in Java with swing?
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import ...
0
votes
Select row in a JTable after row sorting
You need to convert the row from the model index to the view index via:
table.getRowSorter().convertRowIndexToView(rowIndex);
(Since apparently table.setRowSelectionInterval() operates on view ...
1
vote
Java Mail Mailgun GUI project attaching images and not embedding them
This is just a proof of concept.
Test.java
package com.example.mail;
import jakarta.mail.Session;
import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import jakarta....
0
votes
How do I scale or set the the size of an ImageIcon in Java?
Nevermind, I found a way:
Just create a class ScaledIcon which implements the Icon Interface, takes the image, width and height as constructor, then implemented my own paintIcon method, which draws ...
0
votes
Change background and text color of JMenuBar and JMenu objects inside it
nothing worked for me, only this code gave me something similar to what I tried to get:
fileMenu.getPopupMenu().setBorder(new LineBorder(Color.GRAY, 1));
1
vote
Accepted
Java JSCrollPane won't resize below minimum size of JButton with text
So, after wrestling with this issue for a few days and going down multiple leads which turned into dead ends, I finally found the issue at the root of it - and it was PEBKAC on my part. It wasn't the ...
0
votes
In Java, what is the default JPanel height and width?
This is quite important when you are dealing with a GridBagLayout which respects the preferred size property:
new JPanel().getPreferredSize() == java.awt.Dimension[width=10,height=10]
-1
votes
JTable in JScrollPane, how to set background?
pane.getViewport().getView().setBackground(Color.RED)
Not pane.getViewport().setBackground(Color.RED)
1
vote
Java JSCrollPane won't resize below minimum size of JButton with text
I made a Java class with your code.
There's no need to size the individual JButtons The JPanel GridLayout will make them all the same size. I added some whitespace to the button panel so you could ...
0
votes
How can I install java Swing in 2024 (for eclipse)?
I need this Swing window Builder Wizard in order to create Jframe and elements graphically
I think I found it I think I found it Swing elements Wizard
1
vote
Display SVG file
Times have changed and there are now better alternatives to Apache Batik, depending on your use case.
If you only need to display an SVG image in a component or use an SVG file as an icon for a ...
0
votes
Dynamically adding JTable to JScrollPane
Also setting auto resize mode off helped me when I didn't get scrollbar after adding table to JscrollPane and adjusting its view port size.
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
3
votes
How can I install java Swing in 2024 (for eclipse)?
Swing built into Java
Java 2 through today’s Java 22 all include Swing.
Swing has always been defined as part of Java SE. Every implementation of the Java Specifications include all the classes ...
0
votes
JPanel doesn't update until resize Jframe
After the container component has been already displayed you should notify Swing about your change.
In my case I had to call 2 methods after change:
containerPanel.revalidate();
containerPanel.repaint(...
2
votes
Accepted
How to display a popup menu during a drop operation
It looks, that the current mouse event ("drop event") causes the menu to direct disappear. You simply need to show your menu in "invokeLater" method.
import static java.awt.dnd....
-1
votes
How to display a popup menu during a drop operation
Your code seems to work fine if you add a drag source for the String:
// ....
final JPanel jp = new JPanel(new BorderLayout());
JTextField jtf = new JTextField("Drag me");
jtf.setDragEnabled(...
Community wiki
0
votes
How to reload a JPanel?
You could also refresh the entire JFrame to update every component, and not just the panel and its components:
this.revalidate();
this being the instance of your JFrame, if your class extends JFrame, ...
0
votes
Accepted
How can I do a progressive bar with JProgressBar from Swing on Java?
It looks like you click the button, then you have a main process that keeps track of your subprocesses. As a subprocess finishes your main process is updated.
Ditch the busy loop happening on the EDT.
...
0
votes
Creating Popup Options Box with multiple buttons
jDialog1.setTitle("New Customer");
jDialog1.setBounds(new java.awt.Rectangle(50, 50, 1092, 638));
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel2....
0
votes
Setting the preferred size of a JTextFieldis not working
As was pointed out, it depends on the layout.
For example, if we have a frame with GridLayout,
this example will not work:
JTextField text = new JTextField();
text.setPrefferedSize(new Dimension(300, ...
0
votes
Java: HTML in Swing, link margin not working
Here's an excerpt from my "YouLearn.txt" file :
Tags not accepting any/some margins...
...will display as if a margin was specified if you put style='position:relative; top:5%/left:5%...
0
votes
Accepted
My components in two different panels are too small and don't fit to the frames size, how can I fix this?
To follow the java standard, use initial uppercase letters for class names.
For the GridPane set the minimum size as well.
For the buttons set the minimum size and the font size.
import javax.swing.*;...
0
votes
Bringing JFileChooser on top of all windows
You wrote (in your comment):
Actually no, that's the only method in the whole program that makes use of a UI the rest still is pure console.
In other words, you want to add GUI components to a ...
1
vote
Adaptable size of a CardLayout-managed panel
Original Answer
I rearranged your code to create the following GUI.
Left-clicking the Switch cards button shows the following card.
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip ...
1
vote
Accepted
Java Swing rendering inconsistencies and flickering
The main reason for this is that you need to call getToolkit().sync() after every repaint.
However, it’s not your only issue. You must not dispose of a Graphics object unless you yourself created it. ...
1
vote
Issues with Panels not displaying in basic Swing application
Your code is schizoid. You say extends JPanel but you don't. You should run with that, in which case, this is the ctor you need:
public WelcomeNetbeans() {
setBackground(BACKGROUND);
...
Top 50 recent answers are included
Related Tags
swing × 81381java × 79849
jframe × 7597
jtable × 7162
user-interface × 6477
jpanel × 6286
jbutton × 3953
awt × 3624
layout-manager × 2833
jlabel × 2642
jtextfield × 2279
netbeans × 2222
actionlistener × 2191
jcombobox × 1953
jscrollpane × 1915
multithreading × 1773
image × 1716
graphics × 1707
jtextarea × 1620
jlist × 1448
paintcomponent × 1364
layout × 1325
eclipse × 1110
joptionpane × 1092
gridbaglayout × 1059