Drag and Resize undecorated JFrame

You can check out mr Rob Camick’s ComponentResizer class. Pretty simple and straight forward to use. Just instantiate the ComponentResizer and register the frame with something like: JFrame frame = new JFrame(); ComponentResizer cr = new ComponentResizer(); cr.registerComponent(frame); cr.setSnapSize(new Dimension(10, 10)); cr.setMaximumSize(new Dimension(…)); cr.setMinimumSize(new Dimension(…)); Here’s a complete example of using the class import java.awt.*; … Read more

How do I draw an image to a JPanel or JFrame?

Try this: package com.sandbox; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.WindowConstants; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class SwingSandbox { public static void main(String[] args) throws IOException { JFrame frame = buildFrame(); final BufferedImage image = ImageIO.read(new File(“C:\\Projects\\MavenSandbox\\src\\main\\resources\\img.jpg”)); JPanel pane = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); … Read more

How to remove title bar in JFrame

The title bar can be removed by calling setUndecorated(true) on the Frame or JFrame instance, i.e. by setting the “undecorated” property to true. This removes both the title bar and the surrounding frame. Here’s the required code for the question: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Already there frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setUndecorated(true); // <– the title bar is removed here On … Read more

SwingWorker in another SwingWorker’s done method

SwingWorker supports PropertyChange events, that is, you can listener to when the SwingWorker changes state or updates it’s progress…yes, SwingWorker even supports progress notification, for example This means you could set up a PropertyChangeListener to monitor changes to the progress and state properties and take appropriate actions… A worker that simple sets progress updates… public … 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 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