Java Swing Invisible Image

4 days ago 9
ARTICLE AD BOX

I'm trying to make an app with java swing, however when trying to add a background to the Login Page, the image does not show up. It is there and moves other JComponents when adding a layout to the root JPanel of the Page, it just, doesn't display the image. I've also tried JavaFX but it seems like I'm having the same problem over there.

I've also tried to find if anyone else has the same problem, however couldn't find anything useful. After spending a whole day trying to figure out a solution, I resorted to asking over here

Pc Info:

This is my PC info if it sums up to be a DE promblem

-` mmt007@mmt007desktop .o+` -------------------- `ooo/ OS: Arch Linux x86_64 `+oooo: Host: B75M-D3P `+oooooo: Kernel: Linux 6.17.6-arch1-1 -+oooooo+: Uptime: 21 mins `/:-:++oooo+: Packages: 1022 (pacman), 26 (flatpak) `/++++/+++++++: Shell: bash 5.3.3 `/++++++++++++++: Display (WX942): 1440x900 in 19", 60 Hz [External] `/+++ooooooooooooo/` WM: Hyprland 0.51.1 (Wayland) ./ooosssso++osssssso+` Theme: Adwaita-dark [GTK2/3/4] .oossssso-````/ossssss+` Icons: Adwaita [GTK2/3/4] -osssssso. :ssssssso. Font: Noto Sans (12pt) [Qt], Adwaita Sans (11pt) [GTK2/3/4] :osssssss/ osssso+++. Cursor: default (24px) /ossssssss/ +ssssooo/- Terminal: kitty 0.43.1 `/ossssso+/:- -:/+osssso+- Terminal Font: NimbusMonoPS-Regular (11pt) `+sso+:-` `.-/+oso: CPU: Intel(R) Core(TM) i5-3570 (4) @ 3.80 GHz `++:. `-/+/ GPU: Intel Xeon E3-1200 v2/3rd Gen Core processor Graphics Controller @ 1.15 GHz [Integrated] .` `/ Memory: 3.73 GiB / 7.62 GiB (49%) Swap: 0 B / 4.00 GiB (0%) Disk (/): 82.46 GiB / 214.08 GiB (39%) - ext4 Locale: en_US.UTF-8

The Code:

And here is the code that is giving me problems:

Login Page class:

public class LoginPage extends Page { protected void init() { root.setLayout(new FlowLayout()); JImageLabel background = new JImageLabel(new ImageIcon("assets/backgrounds/login_background.png")); background.setSize(400,400); background.setPreferredSize(new Dimension(800,100)); root.add(background); root.setBackground(Color.BLACK); } }

Page class:

public abstract class Page { protected final JPanel root = new JPanel(); public Page() { init(); } protected abstract void init(); }

JImageLabel class:

public class JImageLabel extends JLabel { private ImageIcon image; public JImageLabel(ImageIcon image) {super(image);} }

Window class:

public class Window extends JFrame { private static final Window INSTANCE = new Window("Disbot"); private static final Dimension DEFAULT_WINDOW_SIZE = new Dimension(1280,720); private static Page page = null; private Window(String title) throws HeadlessException {super(title);} public static void init(){ INSTANCE.setDefaultCloseOperation(EXIT_ON_CLOSE); INSTANCE.setSize(DEFAULT_WINDOW_SIZE); INSTANCE.setVisible(true); } public static void setPage(Page new_page){ page = new_page; INSTANCE.setContentPane(page.root); INSTANCE.repaint(); } }

Main Class:

public class Main { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { Window.init(); Window.setPage(new LoginPage()); }); } }

Screenshots:

image1 - black blank screen image2 - two jbuttons on either side of the invisible image

Read Entire Article