Close JFrame in Netbeans

Hello,
I have a java application in NetBeans. I have been using Netbean absolut layout. I didn´t create JFrame class variable at all, instead using:Ok is a button
ok.setFont(new java.awt.Font("Arial", 0, 10));
ok.setToolTipText("ok");
ok.setLabel("ok");
ok.setMargin(new java.awt.Insets(2, 2, 2, 2));
getContentPane().add(ok, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 150, 60, 20));
ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okPerformed(evt);
I don´t want to use System.exit(0); because it will shut down whole system. In order to use dispose method, I created a JFrame class variable in beginning of the class, but it doesn´t work.
JFrame frame = new JFrame();
public void okPerformed (java.awt.event.ActionEvent evt){
this.frame.dispose();
Can anyone help me?
Thanks

hello,
Thanks for replying my message so fast.
I tried, but it still doesn´t work. I am using netbeans absolute Layout. It doesn´t really have frame. Here is my code
public class tt extends JFrame{
JFrame frame = new JFrame();
JTextField text1= new JTextField(20);
JButton ok = new JButton();
public tt throws Exception
setSize(400,300);
setTitle("tt");
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
text1.setFont(new java.awt.Font("Arial",0,12));
text1.setBounds(5, 8, 5, 5);
text1.setEditable(true);
getContentPane().add(text1, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 30, 100, 20));
ok.setFont(new java.awt.Font("Arial", 0, 10));
ok.setToolTipText("ok");
ok.setLabel("ok");
ok.setMargin(new java.awt.Insets(2, 2, 2, 2));
getContentPane().add(ok, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 150, 60, 20));
ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okPerformed(evt);
setVisible(true);
public void okPerformed (java.awt.event.ActionEvent evt){
setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
Thanks

Similar Messages

  • Set name of JFrame in Netbeans

    Hi,
    I am using the GUI builder in netbeans to create my application.
    This means that all the components are created in InitComponents() and I cannot access these components to change them.
    What I need to be able to do is to give my JFrame a name so that I can set properties on the Frame.
    Ultimately what I want to be able to do is to change the IconImage on top left.( I do not want to use the properties window to do this)
    I want to be able to say something like frame.iconImage("icon.gif");
    So how do I set the name of the JFrame in Netbeans?
    Thanks in Advance.

    I'm assuming that your class extends JFrame, so you can just call this.<whichever method you want> from any method or constructor of your class.

  • Close JFrame by press JButton

    i wonder how to close JFrame...
    i know that DeafultCloseOperation... to this for me by pressing X...
    but how can i close the JFrame by pressing JButton...
    what do i have to do?
    to create new Event to do this or what?
    thx
    cya

    You can just call dispose();
                 btnClose.addActionListener(
                            new ActionListener()
                                   // pass query to tthe frame
                                 public void actionPerformed( ActionEvent event )
                                     dispose();
                                 } // end actionPerformed
                             }  // end ActionListener inner class         
                          ); // end call to addActionListener 

  • Cannot set the initial size of nested JFrame in NetBeans 5.5.

    For some background; I'm trying to design a small configurable, multi-market clock for Traders of foreign currency. Several international market times will be displayed and each will change color as that market opens and closes. The user will be able to select specific foreign markets and configure several options such as color and size.
    I was trying to use a JFrame for the initial window and an undecorated JFrame to hold the clocks and their labels. In the GUI builder for the initial JFrame, I dragged a JFrame and dropped it outside the original to add an (other Component). My problem is that I can't set the prefereedSize or the minimumSize properties for the nested JFrame. When I run the program, a button sets the second JFrame visible but at (0,0) size which when undecorated is invisible, and when decorated shows only the Min/Max/Close buttons. Am I just going about this all wrong or is this a bug
    I am running Java Version 1.5.0 (build 1.5.0_08-b03)
    Rick

    I have tried to use several different components but I have not been able to get them to appear as a component, undocked from the main window.
    Another idea that I tried was to have the whole program run from the same frame and then just set the window as undecorated and hide the menu bar. Unfortunately decorated state cannot be changed after it is displayable or packed. Besides that, how do you then make it moveable when there is no title bar? So many questions? Any other suggestions?

  • Close JFrame

    hi all,
    i have a JFrame. on that frame i put a JTabbedPane. on this pane i add a JPanel. and on this panel there is a JButton "exit". when this button is clicked, a JDialog opens that asks the user if he really wants to quit. when the user clicks "yes", i would like the frame to close, without using the System.exit() command (since the frame is opened from another JFrame, and that one isn't supposed to shut down).
    does any of you know how to do that? thanks a lot people!

    thanks woodi,
    i know that i could use these methods, but the problem is i cannot tell the program which frame to close; since i'm using:
    JFrame --> JTabbedPane --> JPanel --> JButton --> JDialog
    how to tell the JDialog that i want to distroy the JFrame?

  • Question on not to close JFrame when close button is clicked

    Hi All,
    Good Morning
    I have frame created like:
    JFrame frame=new JFrame();When we give
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);The above statement closes the frame but i want the same frame to be hidden in the statusbar.So can any one help me in providing any built in method if there or else how to do it??
    Thanks in advance.
    regards,
    Viswanadh

    but i want the same frame to be hidden in the statusbar.Thats what the "minimize" button is for. Don't mix the functionality of the two buttons.

  • To close JFrame over a JDialog

    Hi,
    I am facing a slight problem. Say I have a code like under
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class abc extends JDialog implements ActionListener
         JPanel jp1 = new JPanel();
         JButton ok1 = new JButton("OK");
         public abc()
              this.getContentPane().setLayout(new BorderLayout());
              this.getContentPane().add(jp1,BorderLayout.CENTER);
              jp1.add(ok1);
              ok1.addActionListener(this);
              ok1.setActionCommand("ok1");
         public void actionPerformed(ActionEvent e)
              if (e.getActionCommand().equals("ok"))
                   System.out.println("Inside action performed method");
              else if (e.getActionCommand().equals("ok1"))
              JFrame j1 = new JFrame();
              JPanel jp2 = new JPanel();
              JButton btn1 = new JButton("OK");
              j1.getContentPane().add(jp2);
              jp2.add(btn1);     
              btn1.addActionListener(this);
              btn1.setActionCommand("ok");
              j1.show();
              j1.pack();
              j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    public static void main(String args[])
              abc ABC = new abc();
              ABC.show();
              ABC.pack();
    Now here in this code the line
    j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    closes the application itself. What I want is only the JFrame closes while the main Dialog stills stays in context.
    Can someone help with me how I can achieve the same.
    Regards.
    Anand

    There is a dialog it has a OK button. When I click the OK button a frame opens with another OK1 button in it.
    Now if I click the "x" sign of the frame it should be disposed without doing anything. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)But if I click the OK1 button then also the frame should be disposed but
    after doing something other processing corresponding to the OK1 button.
    OK1.addActionListener( new ActionListener() {
        public void actionPerformed(ActionEvent e) {
              // do processing
             frame.setVisible(false);
             // add this if you want to remove the frame from memory and recreate it when it needs to be opened
             frame.dispose();
    })>
    And within all this the parent(JDialog) and the child(JFrame) should be modal.A JFrame unfortunately cannot be modal, so you shd use a JDialog instead.
    ICE

  • Algo and close JFrame question

    Just a question. Basically, this is just my main method:
        public static void main(String[] args) {
            //Launch window ==============================================
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    game.launchFrame();
            //============================================================
        }It launches the frame of my game, which is a domino game. There's a bunch of code there (done), and at the end of a game, it asks whether the user wants to play another game, or quit the program. Quitting the program means just using System.exit(0).
    But what about if the user wants to play another game? I was thinking to just close the frame and launch another one. So I put "isEnd = true" if the user wants another game, and modified the main method:
            while (isEnd == false){
            //Launch window ==============================================
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    game.launchFrame();
            //============================================================
            }But it's totally flawed. It repeatedly tries to launch the same frame, so it's messy as hell.
    What do you guys suggest so the program can start a new game?

    so...
    we have the frame -> "DominoFrame.java"
    we have the GUI builder -> "MyDominoGUIBuilder.java"
    we have the class for storing domino -> "DominoStorage.java"
    and the Domino classes -> "DominoSet.java" , "Domino.java", etc...
    when you launch the app in DominoFrame, MyDominoGUIBuilder uses a new DominoStorage
    to initialize and manage the Domino game...
    when you wanna start a new game, you can "destroy" the DominoStorage and creating
    a new one and call the MyDominoGUIBuilder to create a new game, etc...

  • Netbeans and Multiple jFrames

    Hey,
    Does anyone know how to make an application have multiple windows/jFrames with Netbeans 5.5's GUI builder?
    I created another jFrame in my application by right-clicking on my source package in the Projects window and selecting New... --> jFrame Form...
    How do you make the new jFrame form I created become visible from the first jFrame form I have?
    Once I have that new form visible, how do I make it do stuff to the first jFrame form?

    Here's a little example:
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    class Frame1 extends JFrame {
         private JButton button;
         public Frame1() {
              initComponents();
         private void initComponents() {
              button = new JButton("New Frame");
              button.setSize(100, 20);
              button.setLocation(5, 5);
              this.getContentPane().setLayout(null);
              this.getContentPane().add(button);
              this.pack();
              this.setSize(400, 400);
              this.setLocationRelativeTo(null);
              this.setDefaultCloseOperation(EXIT_ON_CLOSE);
              this.setTitle("Demo");
              button.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent evt) {
                        new Frame2().setVisible(true);
         public static void main(String[] argv) {
              new Frame1().setVisible(true);
    class Frame2 extends JFrame {
         private JMenuBar jmb;
         private JMenu menu;
         private JMenuItem exitItem;
         public Frame2() {
              initComponents();
         private void initComponents() {
              jmb = new JMenuBar();
              menu = new JMenu("File");
              menu.setMnemonic((int)'F');
              exitItem = new JMenuItem("Exit");
              exitItem.setMnemonic((int)'X');
              jmb.add(menu);
              menu.add(exitItem);
              this.setJMenuBar(jmb);
              this.pack();
              this.setSize(100, 100);
              this.setTitle("Demo 2");
              this.setDefaultCloseOperation(EXIT_ON_CLOSE);
              this.setResizable(false);
              this.setLocationRelativeTo(null);
              exitItem.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent evt) {
                        System.exit(0);
    }

  • Questions about close a JFrame

    1) Creat a new JFrame frm1 to show a login page
    2) when user entered username & password, and clicked "OK" button, the program will check it's authority. when passed, I want to open a new JFrame and close the login frame.
    but I couldn't close the login page, and i found nothing on close JFrame on JFrame's API, except
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    maybe the usage about JFrame totally wrong. please give me some help. thank you!

    Hy
    On clicking the Login Button, u should validate the authorization.
    Eg:- If authorisation success then 1 , else 0.
    so if 1 means.
    this.dispose();
    newFrame.setVisible(true);
    if authorization 0 means,
    show some message using JOptionPane.showMessageDialog(...);
    and let the cursor be in the first field.

  • How to setExtendedState for NetBeans IDE JFrame

    Hello.
    I seem to have a problem with using the visual editor to set extended state for my jframe in NetBeans IDE.
    I would like to make max out the screen, is there a way or a work around in this case?
    Thank you very much.

    I would like to make max out the screen, is there a
    way or a work around in this case?Did you mean maximizing the JFrame ? Then this will help
    setExtendedState(JFrame.MAXIMIZED_BOTH);Good luck! Happy New Year

  • Inserting an image to Jframe and save it ,  in SQL database

    Hai guys,
    I just wanna know about the inserting image in to a Jframe (in netBeans) it will be a grate help if you tell me the way to call a image to Jframe through a Button click and save the image in MySQl database.
    Is anyone has a idea about this task, please tell me
    Thanks,

    Image class should work to get the data, then add it to a Canvas object, then you can save the bytes from the Image into an SQL statement through JDBC.
    The implementation details will vary based on which approch and database you select.

  • Problem with opening new frame in netbeans

    Hi...
    I have created a jframe in netbeans when i try to open this frame now i got a warning form loaded with errors errors occurred in loading the form data.....
    How to solve this problem?

    How to solve this problem?Learn Java.
    [http://catb.org/~esr/faqs/smart-questions.html]
    db

  • JFrame opening multiple times - strange bug

    Hi there
    I'm having this problem whereby a window (a JFrame) is opened multiple times - it works as follows:
    - I've got one JFrame which is opened via a button in my main JFrame (I'll call it JFrame 1)
    - A second one is opened from within the first in a similar way (JFrame 2)
    - The first time one opens JFrame 1 one can open and close JFrame 2 fine as many times as one wants
    - But when one closes JFrame 1 (this with JFrame 2 closed) and then reopens it (JFrame 1) and then clicks the button to open JFrame 2, JFrame 2 then opens twice - except one of them contains no components while the other contains everything it is meant to
    - The same thing happens as many times as I close JFrame 1 - except that when it is closed again JFrame 2 opens 3 times, then 4 and so on
    This is what I have for JFrame 1 (the actionlistener in the main window just calls showDebateRound(int roundID):
    public void showDebateRound(int roundID) {
            JFrame roundWindow=new JFrame("Round Draw");
            roundWindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            showRoundTitle.setFont(new java.awt.Font("Dialog", 1, 18));
            showRoundText.setColumns(20);
            showRoundText.setEditable(false);
            showRoundText.setRows(5);
            showRoundScroller.setViewportView(showRoundText);
            //showRoundPrint.setEnabled(false);
            //work out the title
            showRoundTitle.setText("Round: "+getRoundTitle(roundID));
            //work out the draw output
            showRoundTexts = "Motion: "+round[roundID].getMotion()+"\n\n";
            for (int i=0;i<debateCount;i++) {
                if (debate.getRoundID()==roundID) {
    showRoundTexts=showRoundTexts+"Prop: "+team[debate[i].getTeamOneID()].getTeamName()+"\nOpp: "+team[debate[i].getTeamTwoID()].getTeamName()+"\n\n";
    showRoundText.setText(showRoundTexts);
    //layout
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(roundWindow.getContentPane());
    roundWindow.getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(showRoundScroller, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(showRoundTitle)
    .addComponent(showRoundDrawLabel))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
    .addComponent(showRoundPrint)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(ShowRoundEnterResults)))
    .addContainerGap())
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(showRoundTitle)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(showRoundDrawLabel))
    .addGroup(layout.createSequentialGroup()
    .addGap(31, 31, 31)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(ShowRoundEnterResults)
    .addComponent(showRoundPrint))))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(showRoundScroller, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
    .addContainerGap())
    roundWindow.pack();
    roundWindow.setVisible(true);
    //debate round only actionlisteners
    ShowRoundEnterResults.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    enterResults();
    }JFrame 2 is constructed in the same way as JFrame 1
    Does anyone have any idea why this could be happening? I can post more code if need be. Thanks.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    hiding the form is probably more efficient than creating a new one each timeI rather think OP's both hiding the form and creating a new one each time.
    I would really like to know why it is not workingYeah, so would I.But where's the code that brings up the second JFrame? the actionListener.actionPerformed of the button that opens it?
    Better still, post a SSCCE limited to the minimum code that shows the problem.
    {color:#0000ff}http://homepage1.nifty.com/algafield/sscce.html{color}
    db

  • Work with images in netBeans Help needed !

    Hai Guys,
    I have problem in inserting image in JFrame(In netBeans). Can any one tell me the way to insert an image to JFrame and save that image in my SQL database?
    Guys this is a very important task to me so please help me !
    Thanks for watching / replying

    If you are using net beans use the image view if it and insert the picture of the customer in to explicitly in to the column you want to put in and check the code generated behind.
    So you come to how to insert the picture in to the pane of the Jframe.
    Inseting the particular image in to database
    do as he said "BLOB" as your datatype and store the image.
    I think in your Jframe you might be taking the input from the user and posting it to show the user what he has entered and then saving it into the database.
    so directly put the input data first in to the required column where you are showing user the data what he entered and then on submit/save button call action performed method to save the data in the database

Maybe you are looking for

  • Type of the phone dialed/received in phone log of ...

    Hi. I have new N95 and there is somthing which annoying me very much. If I have two different numbers assigned to one contact i.e. mobile and home, they have different icons in phonebook but only mobile phone icon is shown in call log, so I cannot de

  • Using Skype Mobile while traveling in Europe

    I have a Verizon Samsung Fascinate with Skype installed.   I have bought credits. I will soon be traveling to Belgium and France.  I have been told by Verizon that my phone will not work in Europe.  However, when my husband talked to a representative

  • Itunes library .itl file is locked when trying to upgrade to IOS5

    Itunes library .itl file is locked when trying to download IOS5 upgrade. Can someone help?

  • STSUPLD.DLL not recognizing as an add-on; upload multiple documents disabled

    On my client system, Windows XP, Office 2007 and IE8 is installed. It is a 32 bit system. On the Sharepoint site, "upload multiple document" feature is disabled. After searching for the cause, I came to know about the STSUPLD add-on that needs to be

  • LightBox is opening in a new window!

    I have gone though so many other old discussion on this subject and have tried everything i have read about but nothing seemes to be working If anybody could have at look at my code below it would be so helpful maybe i have missed some think I would