Java Swing : why must resize frame, so that can show components have added

Do not add components to JFrame after the JFrame is visible (setVisible(true)) Not really good practice to call setSize() on frame rather call pack() (Causes JFrame to be sized to fit the preferred size and layouts of its subcomponents) and let LayoutManager handle the size. Use EDT (Event-Dispatch-Thread) call JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) as said by @Gilbert Le … Read more

JFrame background image

This is a simple example for adding the background image in a JFrame: import javax.swing.*; import java.awt.*; import java.awt.event.*; class BackgroundImageJFrame extends JFrame { JButton b1; JLabel l1; public BackgroundImageJFrame() { setTitle(“Background Color for JFrame”); setSize(400,400); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); /* One way —————– setLayout(new BorderLayout()); JLabel background=new JLabel(new ImageIcon(“C:\\Users\\Computer\\Downloads\\colorful design.png”)); add(background); background.setLayout(new FlowLayout()); l1=new JLabel(“Here … Read more

How to make a transparent JFrame but keep everything else the same?

Basically, you need to make a transparent window and a translucent content pane. This will mean anything added to the content pane will continue to be rendered without additional alphering… public class TranscluentWindow { public static void main(String[] args) { new TranscluentWindow(); } public TranscluentWindow() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try … Read more

JFrame Exit on close Java

You need the line frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Because the default behaviour for the JFrame when you press the X button is the equivalent to frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); So almost all the times you’ll need to add that line manually when creating your JFrame I am currently referring to constants in WindowConstants like WindowConstants.EXIT_ON_CLOSE instead of the same constants declared … Read more

Java JFrame .setSize(x, y) not working?

Create a custom component, extending from JPanel, override its getPreferredSize method to return the size of the window you would like. Either add this to your frame or set it as the frame’s content pane. Call pack on the frame Updated with example On my PC, the Frame size = java.awt.Dimension[width=216,height=238] import java.awt.BorderLayout; import java.awt.Dimension; … Read more

How to hide a JFrame in system tray of taskbar

import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.UIManager; /** * * @author Mohammad Faisal * ermohammadfaisal.blogspot.com * facebook.com/m.faisal6621 * */ public class HideToSystemTray extends JFrame{ TrayIcon trayIcon; SystemTray tray; HideToSystemTray(){ super(“SystemTray test”); System.out.println(“creating instance”); try{ System.out.println(“setting look and feel”); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception e){ System.out.println(“Unable to set LookAndFeel”); } if(SystemTray.isSupported()){ System.out.println(“system tray supported”); tray=SystemTray.getSystemTray(); Image image=Toolkit.getDefaultToolkit().getImage(“/media/faisal/DukeImg/Duke256.png”); ActionListener … Read more