Show JFrame in a specific screen in dual monitor configuration

public static void showOnScreen( int screen, JFrame frame ) { GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); if( screen > -1 && screen < gs.length ) { gs[screen].setFullScreenWindow( frame ); } else if( gs.length > 0 ) { gs[0].setFullScreenWindow( frame ); } else { throw new RuntimeException( “No Screens Found” ); } }

Close one JFrame without closing another?

If you do not want your application to terminate when a JFrame is closed, use setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) instead of setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); From the documentation: DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don’t do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object. HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking … Read more

Java – how do I prevent WindowClosing from actually closing the window

I’ve just tried this minimal test case: import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.WindowConstants; public class Test { public static void main(String[] args) { final JFrame frame = new JFrame(“Test”); frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { //frame.dispose(); } }); frame.setVisible(true); } } If I keep the dispose call commented, and hit … Read more

Swing: Obtain Image of JFrame

ComponentImageCapture.java import java.awt.BorderLayout; import java.awt.Component; import java.awt.Image; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.InputEvent; import javax.swing.*; import javax.swing.border.TitledBorder; import javax.imageio.ImageIO; import java.io.File; /** Create a screenshot of a component. @author Andrew Thompson */ class ComponentImageCapture { static final String HELP = “Type Ctrl-0 to get a screenshot of the current … 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

Unresponsive KeyListener for JFrame

If you don’t want to register a listener on every component, you could add your own KeyEventDispatcher to the KeyboardFocusManager: public class MyFrame extends JFrame { private class MyDispatcher implements KeyEventDispatcher { @Override public boolean dispatchKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent.KEY_PRESSED) { System.out.println(“tester”); } else if (e.getID() == KeyEvent.KEY_RELEASED) { System.out.println(“2test2”); } else if … Read more

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

import javax.swing.JOptionPane; import javax.swing.JFrame; /*Some piece of code*/ frame.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent windowEvent) { if (JOptionPane.showConfirmDialog(frame, “Are you sure you want to close this window?”, “Close Window?”, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){ System.exit(0); } } }); If you also want to prevent the window from closing unless the user chooses ‘Yes’, you can … Read more

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

Usage depends on whether the component’s parent has a layout manager or not. setSize() — use when a parent layout manager does not exist; setPreferredSize() (also its related setMinimumSize and setMaximumSize) — use when a parent layout manager exists. The setSize() method probably won’t do anything if the component’s parent is using a layout manager; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)