I want to be able to have a window listener that when the window is opened, some graphics appear. This comes with some problem though, because you can't set a window listener while the window is set to visible (or at least that's what I've found), but if you wait to set the window to visible until after you set the listener you can't set the Graphics.
This is the code with the window set to visible at the start:
public class FrameTest {
static Frame myFrame;
static Graphics myGraphics;
public static void main(String[] args) {
//Initializing Window
myFrame = new Frame();
myFrame.setTitle("Frame Test");
myFrame.setSize(570, 570);
myFrame.setVisible(true);
myGraphics = myFrame.getGraphics();
myFrame.requestFocus();
//Close Button
myFrame.addWindowListener(new WindowAdapter()
{
public void windowOpened(WindowEvent e)
{
System.out.println("opened");
myGraphics.setColor(Color.red);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
Related
Here is a piece of code that I wrote but the close button on the top of the application doesn't work please help
Code:
import java.awt.*;
import java.awt.event.*;
public class App extends Frame implements MouseMotionListener {
App() {
addMouseMotionListener(this);
setSize(200, 200);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.setColor(Color.RED);
g.fillRect(e.getX(), e.getY(),10, 10);
}
public void mouseMoved(MouseEvent e) {
}
public static void main(String[] args)throws Exception {
App a = new App();
}
}
Image:
You need to add Listener and call dispose while initializing your applet.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
You need to add above line of code inside the constructor.
Try it with that little code:
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
You have to insert it in "App()". It will close the program "System.exit(0);"
when you press the close button.
im not using AWT often but heres my Solution :)
addWindowListener(new WindowAdapter(){
#Override
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
Add a new WindowAdapter to your App Constructor and call system.exit(0) on the Window Close Event
You can use WindowContants on JFrame#setDefaultCloseOperation to achieve the desired action. This allows you to dispose the frame, entirely terminate the application and a few more with a single line in the constructor:
this.setDefaultCloseOperation(WindowContants.DISPOSE_ON_CLOSE);
will dispose the frame containing the app. This is sufficient to terminate the program you presented.
This approach doesn't allow handling any events though, but simply closes the frame.
I have a main frame and a kind of toolbar in a JDialog window. I want that "toolbar" to be always on top of MY program only, so I wrote this code :
public class Test {
private static JFrame mainFrame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
mainFrame = new JFrame("test");
mainFrame.setSize(800,600);
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.setVisible(true);
MyDialog d = new MyDialog();
}
});
}
public static class MyDialog extends JDialog {
public MyDialog() {
super(mainFrame);
setAlwaysOnTop(true);
setSize(80,60);
setVisible(true);
mainFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowActivated(WindowEvent e) {MyDialog.this.setAlwaysOnTop(true);}
#Override
public void windowDeactivated(WindowEvent e) {
MyDialog.this.toBack();
}
});
}
}
}
To summarize, I create a mainFrame, then a JDialog owned by mainFrame. the JDialog will listen to the mainFrame. When mainFrame is desactivated, the dialog is set "toBack". When activated, it is set "alwaysOnTop".
Everything sounds fine, except that when I try to switch from my program to another, the focus seems to go from MyApp to Firefox (for instance), then from Firefox to the JDialog. How can I avoid that ?
My JFrame opens in a minimized Mode but it can be maximized.
I want to disable the maximize icon so that user cannot maximize the frame and can see it only in the minimized or default mode.
Is it possible?
Use frame.setResizable(false). It disables the maximize button but let the the 'close' and 'minimize' buttons active.
Use this in your code, try using JDialog instead of JFrame for main window.
frame.setResizeable(false);
there is no direct way to remove the maximize button off the
Resizable JFrame as it is created by Windows and it is not painted
using Swing so U can’t touch this.
so you can replace JFrame with JDialog or remove the title bar and implement customized title bar.
You can disable it by using frame.setResizeable(false); or you can remove it completely by using following code
public class Test extends JDialog {
public Test(JFrame frame, String str) {
super(frame, str);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public static void main(String[] args) {
try {
Test myFrame = new Test(new JFrame(), "Removing maximize button");
JPanel panel = new JPanel();
panel.setSize(100, 100);
myFrame.add(panel);
myFrame.setSize(100, 100);
myFrame.setVisible(true);
} catch (IllegalArgumentException e) {
System.exit(0);
}
} }
Can someone tell me how to set my X button in main application window visible to false and how to set Alt + F4 function not available or just disable it?
Update
I added it in:
public ZalumView(SingleFrameApplication app) {
super(app);
initComponents();
mainFrame = this.getFrame();
mainFrame.setTitle("Zalum - zarzadzanie zasobami ludzkimi");
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
"A frame may have its native decorations (i.e. Frame and Titlebar) turned off with setUndecorated."—Frame
Addendum: You can send a WINDOW_CLOSING event and bind that Action to your desired Keystroke, as shown here.
For setting X button to invisible is very much described by #trashgod and For disabling your ALT + F4 thing, you can simply write frameObject.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
or you can addWindowListener(...) to your JFrame like this :
Code snippet to show what i am saying :
import java.awt.event.*;
import javax.swing.*;
public class FrameTest
{
private WindowAdapter windowAction;
private JFrame frame;
public FrameTest()
{
frame = new JFrame("FRAME TEST");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
windowAction = new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
};
frame.addWindowListener(windowAction);
frame.setSize(100, 100);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new FrameTest();
}
});
}
}
I have a class developed with windowbuilderpro that i want to close also from a JButton further than with the standard X button on the window, so here the example of the class :
public class MainWindow {
public JFrame frame;
public MainWindow() {
initialize();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void show() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
//Show the main Frame
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
When i close the window from the X button the window close correctly and the process terminate.
When i close instead from a JButton that have this listener :
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Close the application main form
frame.setVisible(false);
frame.dispose();
}
});
the frame window close correctly but the process remain alive ... Why ?
As you can see there is an AWT-Shutdown thread that start and terminate continuously, How can i achieve the same behaviour of the X button that close also the application process ?
Notes :
System.exit(0); is not suitable because it terminate the application also if there are another background running thread and i don't want that . The MainWindow class should close and release it's resource, the same behaviour that have closing the application with the X button that close the MainWindow instance but if there are background thread running it doesn't kill they but wait until they finished their work...
Enviroment :
JDK 7
Eclipse 3.7.1
not sure what you really needed, that looks like that you create new JFrame again an again, don't do that, create JFrame once and re-use this Container
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // do nothing
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); // same as setVisible(false)
then for visibily you can only to call frame.setVisible(true);
for more Confortable is override WindowListener, then you can control some Events
All threads in this code stop when either the x button or the Exit button are activated. Are you getting different behavior?
import java.awt.event.*;
import javax.swing.*;
public class MainWindow {
public JFrame frame;
JButton mntmExit = new JButton("Exit");
public MainWindow() {
frame = new JFrame("Close Me!");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Close the application main form
frame.setVisible(false);
frame.dispose();
}
});
frame.add(mntmExit);
frame.pack();
show();
}
public void show() {
//Show the main Frame
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainWindow mw = new MainWindow();
mw.show();
}
});
}
}
Just add one line:
System.exit(0);