JFrame Dispose Problem

Hello everyone,
This is probably a really easily answerable question and I think the reason is that I have the problem is because I'm getting a stack overflow but I am not sure how to solve it.
So, my program is a simple login and logout program
A login JFrame creates a new Main JFrame when log in is pressed, then login is disposed.
When a user logs out of the main frame, a new Login Frame is created and the Main Frame disposed
The problem is that when the user logs in again, the main frame is created but the login frame remains even though dispose is called,
why is this?
Any help at all is greatly appreciated!
Cheers

why is this?You have a bug in your code.
For more help create a [SSCCE (Short, Self Contained, Compilable and Executable, Example Program)|http://sscce.org], that demonstrates the incorrect behaviour.
Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.

Similar Messages

  • JFrame.dispose not firing the windowsClosing event

    No this one is not quite like the question a few posts down, well not exactly anyway.
    I have a JFrame with a windowAdapter that handles my closing tasks, it works just fine currently as long as the close button on the frame is clicked. I had to add a check in the program that should close the window if its true.
    So I have code that checks a condition and if its true it shuts down all threads and trys to exit gracefully through the windowClosing event. As other posts have said I can use the jframe.dispose followed by System.exit and it works fine seeing that in the code thoughmakes me think its a kludge since dispose doesn't trigger the windowClosing event. I would like to somehow simulate clicking the close button on the frame when I need to exit because of an error but I'm not sure how to programatically do that.
    My current window close operation is set to DO_NOTHING_ON_CLOSE so that the windowClosing event can handle the closing, I tried setting the operation to EXIT_ON_CLOSE then disposing the frame when the error occured but that didn't trigger the windowsClosing event either.
    Any ideas?
    Thanks,
    Eudaemon.

    okay... So do something like this:
    private void init() { // or whatever...
       frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
             quit();
       quitmenuitem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent.) {
             quit();
    public void quit() {
       // do whatever cleanup or checks or whatever...
       frame.dispose();
    }

  • JFrame decorations problem

    Greetings:
    I've found searching at the net, that it is suggested "that the L&F (rather than the system) decorate all windows. This must be invoked before creating the JFrame. Native look and feels will ignore this hint". The command that does this is the static JFrame.setDefaultLookAndFeelDecorated(true); .
    If I install Metal L&F as default, and then go to Full Screen and change the L&F, it partially WORKS (the Metal L&Fdecorations are set like in an InternalFrame); but if I set the System L&F as default and change the L&F, the new L&F does not recive its propper decorations but rather the system ones.
    Could anyone help me in this issue?
    Thanks a lot in advanced.
    Javier
    // java scope
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import com.sun.java.swing.plaf.motif.*;
    public abstract class LandFTest {
       public static void main(String[] args) {
          try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } //UIManager.getCrossPlatformLookAndFeelClassName()
          catch (Exception e) { }
          SwingUtilities.invokeLater(new Runnable() {
             public void run() { JFrame.setDefaultLookAndFeelDecorated(true);
                                 MainWin myWindow = new MainWin(" Look & Feel Test");
                                 myWindow.pack();
                                 myWindow.setVisible(true); }
    class MainWin extends JFrame {
          public Box mainLayout = new MainComponent(this);
             public JScrollPane mainLayoutScrollPane = new JScrollPane(mainLayout);
          public Boolean FullScrnOn=false;
       public MainWin (String mainWin_title) {
          Container framePanel = this.getContentPane();
          this.setTitle(mainWin_title);
          this.setLocationRelativeTo(null);
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          framePanel.add(mainLayoutScrollPane,BorderLayout.CENTER);
    class MainComponent extends Box {
          Box controls_Box = Box.createHorizontalBox();
          Box lfControl_Box = Box.createVerticalBox();
          JRadioButton landfRadioButton1 = new JRadioButton(" Metal skin (Java)"),
                       landfRadioButton2 = new JRadioButton(" System skin",true),
                       landfRadioButton3 = new JRadioButton(" Motif skin");
          ButtonGroup lanfFButtonGroup = new ButtonGroup();
          JButton fullScrnButton = new JButton("Full Screen On");
    public MainComponent(MainWin refFrame){
          super(BoxLayout.Y_AXIS);
          this.initMainCompopent(refFrame);
          this.setMainComponent();
       public void initMainCompopent (MainWin refFrame) {
           LookAndFeel_ActionListener landFListener = new LookAndFeel_ActionListener(lanfFButtonGroup, refFrame);
           FullScreen_ActionListener fullSCrnListener = new FullScreen_ActionListener(refFrame);
          lfControl_Box.setBorder(BorderFactory.createTitledBorder(" Look & Feel Control "));
          landfRadioButton1.setActionCommand("Java");
             landfRadioButton1.addActionListener(landFListener);
             lanfFButtonGroup.add(landfRadioButton1);
          landfRadioButton2.setActionCommand("System");
             landfRadioButton2.addActionListener(landFListener);
             lanfFButtonGroup.add(landfRadioButton2);
          landfRadioButton3.setActionCommand("Motif");
             landfRadioButton3.addActionListener(landFListener);
             lanfFButtonGroup.add(landfRadioButton3);
          fullScrnButton.addActionListener(fullSCrnListener);
          fullScrnButton.setAlignmentX(Component.CENTER_ALIGNMENT);
       public void setMainComponent () {
          controls_Box.add(Box.createHorizontalGlue());
          controls_Box.add(lfControl_Box);
             lfControl_Box.add(landfRadioButton1);
             lfControl_Box.add(landfRadioButton2);
             lfControl_Box.add(landfRadioButton3);
          controls_Box.add(Box.createHorizontalGlue());
          this.add(Box.createVerticalGlue());
          this.add(controls_Box);
          this.add(Box.createVerticalGlue());
          this.add(fullScrnButton);
          this.add(Box.createVerticalGlue());
    class LookAndFeel_ActionListener implements ActionListener {
          private ButtonGroup eventButtonGroup;
          private MainWin eventFrame;
       public LookAndFeel_ActionListener (ButtonGroup buttonGroup, MainWin eventFrame) {
          this.eventButtonGroup = buttonGroup;
          this.eventFrame = eventFrame;
       public void actionPerformed(ActionEvent event) {
          String command = eventButtonGroup.getSelection().getActionCommand();
          GraphicsDevice scrnDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
          eventFrame.dispose();
          if (command.equals("System")) {
            try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
            catch (Exception e) { }
          else { if (command.equals("Java")) {
                   try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());}
                   catch (Exception e) { }
                 else { if (command.equals("Motif")) {
                          try { UIManager.setLookAndFeel(new MotifLookAndFeel()); }
                          catch (Exception e) { }
                        else { }
          SwingUtilities.updateComponentTreeUI(eventFrame);
          if (eventFrame.FullScrnOn){ try {scrnDevice.setFullScreenWindow(eventFrame); }
                                      finally {  }
          } else { JFrame.setDefaultLookAndFeelDecorated(true); }
          eventFrame.setVisible(true);
          //eventFrame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
    class FullScreen_ActionListener implements ActionListener {
          private MainWin eventFrame;
       public FullScreen_ActionListener (MainWin eventFrame) {
          this.eventFrame = eventFrame;
       public void actionPerformed(ActionEvent event) {
             MainComponent mainFrameLayout = (MainComponent)eventFrame.mainLayout;
             GraphicsDevice scrnDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
          if (!eventFrame.FullScrnOn){
             if (scrnDevice.isFullScreenSupported()) {
                // Enter full-screen mode with an undecorated JFrame
                eventFrame.dispose();
                eventFrame.setUndecorated(true);
                eventFrame.setResizable(false);
                mainFrameLayout.fullScrnButton.setText("Full Screen Off");
                eventFrame.FullScrnOn=!eventFrame.FullScrnOn;
                try {scrnDevice.setFullScreenWindow(eventFrame); }
                finally {  }
             } else { JOptionPane.showMessageDialog(eventFrame, "Full Screen mode is not allowed \nby the system in this moment.",
                                                                " Full Screen Info", JOptionPane.INFORMATION_MESSAGE); }
          else { // Return to Windowed mode mode with JFrame decorations
                  eventFrame.dispose();
                  eventFrame.setUndecorated(false);
                  eventFrame.setResizable(true);
                  mainFrameLayout.fullScrnButton.setText("Full Screen On");
                  eventFrame.FullScrnOn=!eventFrame.FullScrnOn;
                  try { scrnDevice.setFullScreenWindow(null); }
                  finally { }
          eventFrame.setVisible(true);
    }

    Greetings:
    Thanks a lot for your kind answer:
    After reading your reply, I've done some studies directly in the API in how does the L&F's are managed. Of what I've understood (please correct me if I'm wrong), the things are as follow:
    i) System and Motif Look and Feel's does NOT support decorations, so that's why you should let the JVM do the decoration work (that is, use myFrame.setUndecorated(false); ).
    In this case, it does not matter if you use JFrame.setDefaultLookAndFeelDecorated(true); before the creation of the frame: it won't show decorations if you don't use the command mentioned.
    ii) Metal (Java) Look and Feel DOES support decorations; that's why, if you use JFrame.setDefaultLookAndFeelDecorated(true); before the creation of the frame, you'll see it's own decorations. In this case, you should use myFrame.setUndecorated(true); so the L&F does all the decorating work, avoiding the weird sitiuation of showing double decorating settings (L&F's and native system's).
    iii) So, the real problem here would be if you want to have, as a user choice, that the System/Motif L&F's will be available together with the Java's (Metal). I've made the next variation to my code, so that this will be possible:
    class LookAndFeel_ActionListener implements ActionListener {
    private ButtonGroup eventButtonGroup;
    private MainWin eventFrame;
    public LookAndFeel_ActionListener (ButtonGroup buttonGroup, MainWin eventFrame) {
    this.eventButtonGroup = buttonGroup;
    this.eventFrame = eventFrame;
    public void actionPerformed(ActionEvent event) {
    String command = eventButtonGroup.getSelection().getActionCommand();
    GraphicsDevice scrnDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    eventFrame.dispose();
    if (command.equals("System")) {
    try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    eventFrame.setUndecorated(false); // make sure the native system controls decorations as this L&F doesnt's manage one's.
    catch (Exception e) { }
    else { if (command.equals("Java")) {
    try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); }
    catch (Exception e) { }
    eventFrame.setUndecorated(true); // make sure the L&F controls decorations as it actually has one's.
    else { if (command.equals("Motif")) {
    try { UIManager.setLookAndFeel(new MotifLookAndFeel()); }
    catch (Exception e) { }
    eventFrame.setUndecorated(false); // make sure the native system controls decorations as this L&F doesnt's manage one's.
    else { }
    SwingUtilities.updateComponentTreeUI(eventFrame);
    if (eventFrame.FullScrnOn){ try {scrnDevice.setFullScreenWindow(eventFrame); }
    finally { }
    } else { }
    eventFrame.setVisible(true);
    }iv) The issue here is that, as I needed to use myFrame.dispose() (the JVM says that it is not possible to use setUndecorated(false); in a displayable frame), I recieve and error:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Buffers have not been created
    at sun.awt.windows.WComponentPeer.getBackBuffer(WComponentPeer.java:846)
    at java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:3815)
    at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:3800)
    at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:3915)
    at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:3897)
    at java.awt.Component$FlipBufferStrategy.getDrawGraphics(Component.java:3889)
    at javax.swing.BufferStrategyPaintManager.prepare(BufferStrategyPaintManager.java:508)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:264)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1220)
    at javax.swing.JComponent.paint(JComponent.java:1015)
    and, yet, the Metal (Java's) L&F keeps without decorations. Is it there a way to solve this without having to create a new instance of the frame and use JFrame.setDefaultLookAndFeelDecorated(true); before the creation of such new instance of the frame for the Metal's (Java) L&F?
    Thanks again for al lthe help: this was, definetelly, something not obvious to figure out (or to find a hint on the net).
    Regards,

  • Unexplained JFrame loading problems

    I keep getting a problem whenever i try and use a window that i extended off of JFrame. By its self the frame works perfectly, but when i actually try to use it for the purpose intended, the winow pops up, and is in the right spot, but nothing i put in it is visible. Any ideas?
    The class in question is called "PassFrame". It runs into the problem on lines 239-240 where i try to use it.
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Secrecy {
         private int[] code;
         private int[] cipher;
         private Dice dice;
         private GUI gui;
         private PassFrame passframe;
         public static final char SPACE = ' ';
         //if encrypting, crypt is true, decrypting, crypt is false
         //crypt is set to true by default
         boolean crypt = true;
         public Secrecy() {
              code = new int[93];
              cipher = new int[93];
              dice = new Dice(93);
         void setUp() {
              for(int i = 0; i < code.length; i++) {
                   code[i] = 32+i;
                   code[i] = -1;
         public void setSeed(char[] c) {
              dice.setSeed(seedSetter(c));
         public int seedSetter(char[] pass) {
              int i = 0;
              for(int j = 0; j < pass.length; j++) {
                   i += (int)pass[j];
              return i;
         static void frameWait(JFrame frame) {
              while(frame.isVisible()) {
                   nap(100);
         static void fileChooserWait(JFileChooser chooser) {
              while(chooser.isVisible()) {
                   nap(100);
         void generateCipher() {
              for(int i = 0; i < cipher.length; i++) {
                   int go = dice.roll();
                   if(cipher[go] == -1) {
                        cipher[go] = code;
         //takes input string and converts it using substitution of a different char sequence, then returns converted string
         String encrypt(String s) {
              String now = "";
              for(int i = 0; i < s.length(); i++) {
                   char c = s.charAt(i);
                   now += findMatch(c, crypt);
              return now;
         //looks through one char array to find a match of input char'c', returns corresponding char in different char array
         public int findMatch(char c, boolean b) {
              if(b) {
                   for(int j = 0; j < code.length; j++) {
                        if((int)c == code[j]) {
                             return cipher[j];
              }else{
                   for(int j = 0; j < cipher.length; j++) {
                        if((int)c == cipher[j]) {
                             return code[j];
              return (int)SPACE;          
         void runEncryption(JTextArea area, char[] password) {
              setSeed(password);
              String now = encrypt(area.getText());
              area.setText(now);
         static void nap(int ms) {
              try{
                   Thread.currentThread().sleep(ms);
              }catch (InterruptedException ivt) {
         static void center(JFrame frame) {
              Toolkit tk = Toolkit.getDefaultToolkit();
              Dimension d = tk.getScreenSize();
              frame.setLocation((d.width/2) - (frame.getWidth()/2), (d.height/2) - (frame.getHeight()/2));
         void runChooser() {
              ChooserFrame chooser = new ChooserFrame();
              fileChooserWait(chooser);
         void runPass(JTextArea area) {
              passframe.show();
              while(passframe.isVisible()) {
                   nap(100);
              char[] c = passframe.getPassword();
              setSeed(c);
              generateCipher();
              encrypt(area.getText());
         void run() {
              gui = new GUI(this);
              gui.show();
         public static void main(String[] args) {
              Secrecy sec = new Secrecy();
              sec.run();
    class PassFrame extends JFrame {
         private int length = 16;
         private char[] password;
         private JLabel label = new JLabel("Please enter your Password");
         private JPasswordField jpf = new JPasswordField(length);
         private JPanel panel = new JPanel();
         public PassFrame() {
              setTitle("Password");
              setResizable(false);
              panel.add(label);
              jpf.addKeyListener(new KeyAdapter() {
                   public void keyPressed(KeyEvent kvt) {
                        int code = kvt.getKeyCode();
                        if(code == KeyEvent.VK_ENTER) {
                             password = jpf.getPassword();
                             hide();
              addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent evt) {
                        WindowCloser wc = new WindowCloser();
              panel.add(jpf);
              getContentPane().add(panel);
              pack();
              Secrecy.center(this);
         public char[] getPassword() {
              return password;
         void run() {
              Secrecy.center(this);
              show();
              Secrecy.frameWait(this);
              if(password != null) {
                   System.out.println(password);
         public static void main(String[] args) {
              PassFrame pf = new PassFrame();
              pf.run();
    class ChooserFrame extends JFileChooser {
         public ChooserFrame() {
    class GUI extends JFrame {
         private JButton encrypt;
         private JButton open;
         private JTextArea area;
         private JPanel panel;
         private JScrollPane pane;
         private Secrecy prog;
         public GUI(Secrecy sec) {
              addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent evt) {
                        WindowCloser wc = new WindowCloser();
              prog = sec;
              encrypt = new JButton("Encrypt");
              open = new JButton("Open");
              area = new JTextArea();
              panel = new JPanel();
              setup();
              Secrecy.center(this);
         public void setup() {
              setupButtons();
              setupText();
              pack();
         void setupButtons() {
              encrypt.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent evt) {
                        PassFrame pf = new PassFrame();
                        pf.show();
                        Secrecy.frameWait(pf);     
                        System.out.println("Trying to run Encryption sequence");                    
                        prog.runEncryption(area, pf.getPassword());
              open.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent evt) {
                        prog.runChooser();
              getContentPane().add(encrypt, BorderLayout.EAST);
              getContentPane().add(open, BorderLayout.SOUTH);
         void setupText() {
              area = new JTextArea(10, 55);
              area.setLineWrap(true);
              area.setWrapStyleWord(true);
              pane = new JScrollPane(area);
              getContentPane().add(pane, BorderLayout.CENTER);
    class Dice {
         // random num generator
         static Random r;
         int sides;
         private static long seedLockVal;
         public Dice( int si ) {
              sides = si;
         public int roll() {
              return d( sides ) + 1;
         static {
              setSeedLock();
         public static int d( int what ) {
              if ( what <= 1 ) return 0;
              int val = r.nextInt() / 64;
              int v = (int)( val % what );
              if ( v < 0 ) v *= -1;
              return v;
         public static long setSeedLock() {     
              seedLockVal = System.currentTimeMillis();
              r = new Random( seedLockVal );
              return seedLockVal;
         public static void setSeed( long val ) {
              seedLockVal = val;
              r = new Random( val );
    class WindowCloser extends JFrame {
         JButton ok;
         JButton cancel;
         JLabel check;
         JLabel warning;
         JPanel panel;
         WindowCloser() {
              setResizable(false);
              check = new JLabel("Are you sure you wish to exit? You will lose all data.");
              warning = new JLabel("Closing this window will exit the program.");
              ok = new JButton("OK");
              cancel = new JButton("Cancel");
              ok.addActionListener( new ActionListener() {
                   public void actionPerformed(ActionEvent evt) {
                        System.exit(0);
              cancel.addActionListener( new ActionListener() {
                   public void actionPerformed(ActionEvent evt) {
                        hide();
              addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent evt) {
                        System.exit(0);
              panel = new JPanel();
              panel.add(check);
              panel.add(warning);
              panel.add(ok);
              panel.add(cancel);
              getContentPane().add(panel);
              pack();
              Secrecy.center(this);
              show();
         public static void main(String[] args) {
              WindowCloser wc = new WindowCloser();

    Oh shit...
    Please post concise pieces of code where problems occur.
    Just a few remarks:
         static void fileChooserWait(JFileChooser chooser) {
              while(chooser.isVisible()) {
                   nap(100);
         }If you use JFileChooser#showXXXDialog, the method will block until the dialog has been disposed of (closed).
         WindowCloser() {
              setResizable(false);
    check = new JLabel("Are you sure you wish to exit?
    t? You will lose all data.");
    warning = new JLabel("Closing this window will exit
    it the program.");
              ok = new JButton("OK");
              cancel = new JButton("Cancel");Have a look at the static methods of the javax.swing.JOptionPane class. They're pretty useful.
    These remarks may not help you much, but I for one am not going to read all of this.
    Oh, and please acknowledge the [url http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips and post your code between [code[b]][[b]code] blocks.

  • Jframe dispose, all

    Hi there all,
    I have a problem with my application which i developing. I couldnt find any solution. Can anybody help me?
    I ll shortly explain my problem. I have a main jframe in which there are two buttons.
    One button opens a new child jframe whenever i click it. And the other must close all childs jframes.But i couldnt find a way to close all opening childs jframes. One demostration i want to show you..
    public class NewJFrame extends javax.swing.JFrame {
        /** Creates new form NewJFrame */
        public NewJFrame() {
            initComponents();
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
        new ChildFrame(jTextField1.getText());
        * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration
    this is main andpublic class ChildFrame extends javax.swing.JFrame {
    /** Creates new form ChildFrame */
    public ChildFrame(String title) {
    setTitle(title);
    initComponents();
    setVisible(true);
    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 400, Short.MAX_VALUE)
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 300, Short.MAX_VALUE)
    pack();
    }// </editor-fold>
    // Variables declaration - do not modify
    // End of variables declaration
    this is child.
    i give each opening window a title,
    how can i close (dispose) all opened windows with a close all buttons? Is anybody know a way to solve it?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Oh my... that didn't make much sense (at least to me).
    Did you want to close all the windows and exit the virtual machine (that is, close the java.exe process) ? Normally, when all the frames close the virtual machine may exit on it's own accorde, but it's not a given. You can shutdown your aplication (which closes all the frames) with a System.exit(0) . Setting
    myMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);has the same effect.
    Otherwise, if you were intending to dispose all the frames, but not exit the virtual machine, then you can simply utilize the static method Frame.getFrames() . There are two similar (but not exactly the same) methods in the Window class. I think this is what was meant when you were told to read the API. You can add a window listener to the main frame. When it's closed you retreive the frames with these static methods and simply call dispose on them.

  • JFrame applications problem

    Hi, i'm just starting out writing swing applications and i'm having a few problems.
    Don't bother answering this if you think i'm too stupid because i don't want to waste your time.
    Whenever i write a swing application, it compiles but when i try to run it i get: <b>Exception in thread "main" java.lang.NoSuchMethodError: main</b>
    Heres my code:
    <code>
    import java.awt.*;
    import javax.swing.*;
    public class swingframe extends JFrame
         private JButton but;
         public swingframe()
                   Container c=getContentPane();
                   c.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
                   c.setBackground(Color.black);
                   but = new JButton("Wyld Stallyns Rule!!!");
                   but.setBackground(Color.green);
                   c.add(but);
    </code>
    Sorry again if i'm wasting your time.
    <b>Thanks</b>

    import java.awt.*;
    import javax.swing.*;
    public class SwingFrame extends JFrame{
       private JButton but;
       public SwingFrame(){
          Container c=getContentPane();
          c.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
          c.setBackground(Color.black);
          but = new JButton("Wyld Stallyns Rule!!!");
          but.setBackground(Color.green);
          c.add(but);
          this.setSize(200, 200);
          this.setDefaultCloseOperation( EXIT_ON_CLOSE );
       public static void main(String []args){
          new SwingFrame().show();
    }

  • JFrame minimizing problem

    I am writing an application where i have many JFrames on the screen at the same time. The problem is that when i click on a menu item in one of the screens the rest of them get minimized. Is there any way of preventing this without using JInternalFrames.

    can you please supply source code, I may not be able to help but other's can.

  • JFrame setResizable problem

    hi everybody,
    i'm using j2sdk1.4.2_03 and i want a JFrame all time maximized, i used:
    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    frame.setResizable(false);
    but if i double click on the upper bar the window is still resizable, do anybody of you know where is my problem?
    thanks

    http://developer.java.sun.com/developer/bugParade/bugs/4465093.html

  • JFrame refesh problem

    I was building a huge app... And at one point, a problem appeared. Spent hours on it. Then I cut it down to the most simple element.
    I have an app, this is all it does: creates a frame and changes the color of the background. Yet for some reason, I need to resize for the background color to be applied.
    JFrame APP_FRAME = new JFrame();          
    APP_FRAME.getContentPane().setBackground( Color.black );
    APP_FRAME.setBackground( Color.black );
    APP_FRAME.setVisible( true );
    APP_FRAME.setSize( 645, 512 );
    That's all it is! The black background doesn't apply unless I resize! It works when I set the background color directly on the frame rather than the ContentPane... But later on, I add tons of things in my ContentPane, and I still have the bug of having to resize to get an initial display.
    I would appreciate any help, I can't believe I'm stuck on such a detail!
    Thanks in advance,
    JN

    Still nothing...
    I created a whole new program:
    import java.awt.Color;
    import javax.swing.JFrame;
    JFrame frm = new JFrame();
    frm.setVisible(true);
    frm.getContentPane().setBackground(Color.blue);
    frm.pack();
    frm.setSize(640,480);
    as simple as it gets.. My windows appears in 640X480 but with a gray background. Why doesn't it start off in blue! :-(

  • JFrame movability problem...

    Hi All.
    How to make the JFrame immovable.
    that is ,the frame should not be dragged or resized by the user.
    Kindly help me out to sove this problem.
    I tried frame.setResizable(false);
    but it does not worked out.
    Regards,
    Sdivyya

    javax.swing.JWindow.....

  • JFrame Repaint problem

    I am new to Java Swing and I have a problem.
    I have a GUI program which extends JFrame and contains some business logic. In the middle of executing the business logic, I need to display a chart and I used another JFrame to display the chart. The chart did display but once the control return back to the original process, the chart Jframe turn blank. How can I get the display of the chart until user closes it?
    public class ChangeMetric extend JFrame implements ActionListener {
    public void actionPerformed(ActionEvent event) {
    JFrame jf = new JFrame();
    JPanel jp = new JPanel();
    jp.setSize(500,500);
    jf.getContentPane().add(jp);
    jf.setVisible(true);
    JOptionPane.showConfirmDialog(null, "Report has been saved to : " + path + "\n " +
                   "Do you want another report?", "Completed", JOptionPane.YES_NO_OPTION);
    //Jf turn blank after the JoptionPane displayed.
    Thanks in advance.
    Alex

    I need to display a chart and I used another JFrame to display the chart.An application should only have a single JFrame. Use a JDialog instead. You create it the same way you do a JFrame, the only difference is you specify the frame as the owner.
    Jf turn blank after the JoptionPane displayed.Then you must be doing something really wierd in your code. There is no reason for the option pane to have any effect on your dialog.
    And when you post your SSCCE, don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.

  • JFrame setUndecorated problem

    I set my JFrame undecorated like this:
    jframe.removeNotify();
    jframe.setUndecorated(true);
    jframe.setVisible(true);This works great, only my textFields get unselectable when i use removeNotify(). When i don't use removeNotify, my frame doesn't get undecorated. When i click on a button, the textfields are selectable again.
    How can i solve dis problem?

    oeps. it's the textfields are selectable again when an other frame is opened and the focus had gone back to the frame with the textFields.
    Probably it has something to do with a focus.... ???

  • JFrame.setSize() problem

    I'm facing a strange problem. I have a class derived from JFrame which is the main application frame window. Problem is I'm not able to set its size arbitrarily e.g. in following call to setSize(...) even if I replace 700 with 100 OR 1000 or whatever, the frame is always displayed of some fixed size . I can't find out why .. any clues ?
    JGuessFrame() {
    this.setSize(700, 700);   
    // Center the window on the screen
    setLocationRelativeTo(null);
    setDefaultLookAndFeelDecorated(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    try this:
    JGuessFrame() {
    this.setSize(700, 700);
    // Center the window on the screen
    setLocationRelativeTo(null);
    setDefaultLookAndFeelDecorated(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack(); // maybe this will help
    // if not then try this
    this.repaint();

  • JFrame restoration problem.

    Hello everyone!!
    My problem is:
    I have a jframe inside of that there's a jpanel.
    the jpanel mouse listener calls a method in a different class that uses the graphic reference of the jpanel to draw. once I minimize and maximize the jframe the drawing disappear. once I click on the area where drawing used to be it appears again.
    I have an inner window listener class in the jframe which calls the same method in the same instance of the other class but nothing happens until I click on the area on the mouse listener calls the method!!
    please help.

    Go through ths tutorial:
    The Java&#8482; Tutorials: [Performing Custom Painting|http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html]
    After that, if you still have a question, post a SSCCE that clearly demonstrates the problem.
    db

  • Modal dialog to a JFrame creates problem in windows XP

    I have a problem with JDialog(modal) when it is set to a JFrame.
    It works when the focus is on the dialog.
    If some other application is selected from the taskbar of the windows XP and selecting back the JFrame, the dialog is not poping up on to the frame but it is hided. The dialog can be accessed by selecting Alt+Tab.
    Any solution for this.Pl help.

    hi!
    you need to create a Short, Self Contained, Compilable and Executable, Example Program that demonstrates the incorrect behaviour, so that any one can easily understand where you are doing a mistake.
    And don't forget to use the Code Formatting Tags so the code retains its original formatting.
    :)

Maybe you are looking for