JFrame.setVisible()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaya3
    New Member
    • Aug 2007
    • 184

    JFrame.setVisible()

    Hi,
    I need to make the frame invisible on action event in swings.
    I have the following code:


    public class sample_pgm extends JFrame{
    public void sample()
    {
    JFrame jf = new JFrame("Sample" );
    JPanel panel = new JPanel();
    Container c = jf.getContentPa ne();
    JButton subscribe_butto n = new JButton("submit ");
    panel.add(subsc ribe_button);
    subscribe_butto n.addActionList ener(new MyButtonListene r(this));
    String[] fields ={"Name","Depar t"};
    String[][] data ={{"Name","Depa rt"},{"asdsdf", "assdf"}};
    JTable table = new JTable(data, fields);
    JScrollPane scrollPane = new JScrollPane(tab le);
    scrollPane.setP referredSize(ne w Dimension(500, 150));
    panel.add(scrol lPane);
    c.add(panel);
    jf.setDefaultCl oseOperation(JF rame.EXIT_ON_CL OSE);
    jf.pack();
    jf.setVisible(t rue);
    }
    public static void main(String args[])
    {
    sample_pgm s =new sample_pgm();
    s.sample();
    }
    private class MyButtonListene r implements ActionListener
    {
    private JFrame parentComponent ;
    MyButtonListene r(JFrame parentComponent )
    {
    this.parentComp onent=parentCom ponent;
    }
    public void actionPerformed (ActionEvent e) {
    parentComponent .setVisible(fal se);
    }
    }
    }

    when i tried above code.. Frame is still visible.. How can i correct taht.. help pl..


    -Thanks & Regards,
    Hamsa
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by gaya3
    Hi,
    I need to make the frame invisible on action event in swings.
    I have the following code:

    [CODE=java]
    public class sample_pgm extends JFrame{
    public void sample()
    {
    JFrame jf = new JFrame("Sample" );
    JPanel panel = new JPanel();
    Container c = jf.getContentPa ne();
    JButton subscribe_butto n = new JButton("submit ");
    panel.add(subsc ribe_button);
    subscribe_butto n.addActionList ener(new MyButtonListene r(this));
    String[] fields ={"Name","Depar t"};
    String[][] data ={{"Name","Depa rt"},{"asdsdf", "assdf"}};
    JTable table = new JTable(data, fields);
    JScrollPane scrollPane = new JScrollPane(tab le);
    scrollPane.setP referredSize(ne w Dimension(500, 150));
    panel.add(scrol lPane);
    c.add(panel);
    jf.setDefaultCl oseOperation(JF rame.EXIT_ON_CL OSE);
    jf.pack();
    jf.setVisible(t rue);
    }
    public static void main(String args[])
    {
    sample_pgm s =new sample_pgm();
    s.sample();
    }
    private class MyButtonListene r implements ActionListener
    {
    private JFrame parentComponent ;
    MyButtonListene r(JFrame parentComponent )
    {
    this.parentComp onent=parentCom ponent;
    }
    public void actionPerformed (ActionEvent e) {
    parentComponent .setVisible(fal se);
    }
    }
    }
    [/CODE]
    when i tried above code.. Frame is still visible.. How can i correct taht.. help pl..


    -Thanks & Regards,
    Hamsa
    First of all, please use the CODE tags. As you see above, it makes it so much easier to read the code.
    Now, your ButtonListener looks a bit weird - the JFrame you define at the top of it has the same name as the JFrame given in the constructor. Therefore, in the constructor the second one is used, so the first one shouldn't even be properly declared at any point - I think. Maybe, you should change the name of one of them?
    I must admit however, that I've not worked with Swing much and not at all lately, so I might be wrong about this.

    Greetings,
    Nepomuk

    Comment

    • gaya3
      New Member
      • Aug 2007
      • 184

      #3
      Originally posted by nepomuk
      First of all, please use the CODE tags. As you see above, it makes it so much easier to read the code.
      Now, your ButtonListener looks a bit weird - the JFrame you define at the top of it has the same name as the JFrame given in the constructor. Therefore, in the constructor the second one is used, so the first one shouldn't even be properly declared at any point - I think. Maybe, you should change the name of one of them?
      I must admit however, that I've not worked with Swing much and not at all lately, so I might be wrong about this.

      Greetings,
      Nepomuk

      hi...
      I couldnt get you.. can you explain me once again..
      Thanks in Advance.

      -Thanks & Regards,
      Hamsa

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by gaya3
        hi...
        I couldnt get you.. can you explain me once again..
        Thanks in Advance.

        -Thanks & Regards,
        Hamsa
        OK, first of all: CODE tags. When posting any code in this forum, type
        Code:
         before your code and when finished writing your code, use [/CODE ] (delete that last space).
        
        Arg, I just realized, that my comment on your ButtonListener was wrong - forget what I said.
        
        Anyway, I would swap these here around:
        [CODE=java]
        JButton subscribe_button = new JButton("submit");
        panel.add(subscribe_button);
        subscribe_button.addActionListener(new MyButtonListener(this));
        and not add the button to the panel, until it's complete (= until you've added the Listener), but I don't know if that will have any effect. I remember having to do something similar a while ago and I think my solution was something like that, but I really don't remember.

        Sorry for confusing you,
        Nepomuk

        Comment

        • hsn
          New Member
          • Sep 2007
          • 237

          #5
          hi gaya3
          your code is not bad i just did some changes to it and it worked

          here is your original code and i will help you through it.
          Code:
          publicclasssample_pgm extendsJFrame{
             publicvoidsample()
              {
                JFramejf = newJFrame("Sample");
                JPanelpanel = newJPanel();
                Containerc = jf.getContentPane();
                JButtonsubscribe_button = newJButton("submit");
                panel.add(subscribe_button);
                subscribe_button.addActionListener(newMyButtonListener(this));
                String[]fields ={"Name","Depart"};
                String[][]data ={{"Name","Depart"},{"asdsdf","assdf"}};
                JTabletable = newJTable(data, fields);
                          JScrollPanescrollPane = newJScrollPane(table);
                         scrollPane.setPreferredSize(newDimension(500, 150));
                         panel.add(scrollPane);
              c.add(panel);
                          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
              jf.pack();
              jf.setVisible(true);
              }
            publicstaticvoidmain(Stringargs[])
          {
             sample_pgm s =newsample_pgm();
             s.sample();
          }
          privateclassMyButtonListener implementsActionListener
             {
                privateJFrame parentComponent;
                MyButtonListener(JFrameparentComponent)
                {
                   this.parentComponent=parentComponent;
                }
                publicvoidactionPerformed(ActionEvente){
                   parentComponent.setVisible(false);
                }
          }
          }
          the way i did it was by using ActionListener

          ok.
          1) in the begining of the class you have to implement ActionListener next to the line where you extend JFrame.
          2) i see it much better to create all the objects in the class outside the functions (for designing UI). so do that it will help you.
          3) after you implement ActionListener you have to create a function called actionPreformed .
          Code:
          public void actionPerformed(ActionEvent arg0) {	
          }
          .
          4) actionPreformed will be called when a button is pressed. in this program there is only one button so we don't have to define which button is called in the function. in the function include the name of the frame "jf as you did provide" and do the setVisible function and it will work

          i really hope that i helped you

          GOOD LUCK
          hsn

          Comment

          • gaya3
            New Member
            • Aug 2007
            • 184

            #6
            Thanks for all reply..
            Its working fine..

            -Thnaks & Regards,
            Hamsa

            Comment

            • hsn
              New Member
              • Sep 2007
              • 237

              #7
              welcome gaya3

              Good Luck

              hsn

              Comment

              Working...