469,366 Members | 2,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

JFrame.setVisible()

184 100+
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.getContentPane();
JButton subscribe_button = new JButton("submit");
panel.add(subscribe_button);
subscribe_button.addActionListener(new MyButtonListener(this));
String[] fields ={"Name","Depart"};
String[][] data ={{"Name","Depart"},{"asdsdf","assdf"}};
JTable table = new JTable(data, fields);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(500, 150));
panel.add(scrollPane);
c.add(panel);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String args[])
{
sample_pgm s =new sample_pgm();
s.sample();
}
private class MyButtonListener implements ActionListener
{
private JFrame parentComponent;
MyButtonListener(JFrame parentComponent)
{
this.parentComponent=parentComponent;
}
public void actionPerformed(ActionEvent e) {
parentComponent.setVisible(false);
}
}
}

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


-Thanks & Regards,
Hamsa
Apr 8 '08 #1
6 13153
Nepomuk
3,112 Expert 2GB
Hi,
I need to make the frame invisible on action event in swings.
I have the following code:

Expand|Select|Wrap|Line Numbers
  1. public class sample_pgm extends JFrame{
  2.    public void sample()
  3.     {
  4.       JFrame jf = new JFrame("Sample");
  5.       JPanel panel = new JPanel();
  6.       Container c = jf.getContentPane();
  7.       JButton subscribe_button = new JButton("submit");
  8.       panel.add(subscribe_button);
  9.       subscribe_button.addActionListener(new MyButtonListener(this));
  10.       String[] fields ={"Name","Depart"};
  11.       String[][] data ={{"Name","Depart"},{"asdsdf","assdf"}};
  12.       JTable table = new JTable(data, fields);
  13.                 JScrollPane scrollPane = new JScrollPane(table);
  14.                scrollPane.setPreferredSize(new Dimension(500, 150));
  15.                panel.add(scrollPane);
  16.     c.add(panel);
  17.                 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.     jf.pack();
  19.     jf.setVisible(true);
  20.     }
  21.   public static void main(String args[])
  22. {
  23.    sample_pgm s =new sample_pgm();
  24.    s.sample();
  25. }
  26. private class MyButtonListener implements ActionListener
  27.    {
  28.       private JFrame parentComponent;
  29.       MyButtonListener(JFrame parentComponent)
  30.       {
  31.          this.parentComponent=parentComponent;
  32.       }
  33.       public void actionPerformed(ActionEvent e) {
  34.          parentComponent.setVisible(false);
  35.       }
  36. }
  37. }
  38.  
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
Apr 8 '08 #2
gaya3
184 100+
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
Apr 8 '08 #3
Nepomuk
3,112 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. JButton subscribe_button = new JButton("submit");
  2. panel.add(subscribe_button);
  3. subscribe_button.addActionListener(new MyButtonListener(this));
  4.  
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
Apr 8 '08 #4
hsn
237 100+
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.
Expand|Select|Wrap|Line Numbers
  1. publicclasssample_pgm extendsJFrame{
  2.    publicvoidsample()
  3.     {
  4.       JFramejf = newJFrame("Sample");
  5.       JPanelpanel = newJPanel();
  6.       Containerc = jf.getContentPane();
  7.       JButtonsubscribe_button = newJButton("submit");
  8.       panel.add(subscribe_button);
  9.       subscribe_button.addActionListener(newMyButtonListener(this));
  10.       String[]fields ={"Name","Depart"};
  11.       String[][]data ={{"Name","Depart"},{"asdsdf","assdf"}};
  12.       JTabletable = newJTable(data, fields);
  13.                 JScrollPanescrollPane = newJScrollPane(table);
  14.                scrollPane.setPreferredSize(newDimension(500, 150));
  15.                panel.add(scrollPane);
  16.     c.add(panel);
  17.                 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  18.     jf.pack();
  19.     jf.setVisible(true);
  20.     }
  21.   publicstaticvoidmain(Stringargs[])
  22. {
  23.    sample_pgm s =newsample_pgm();
  24.    s.sample();
  25. }
  26. privateclassMyButtonListener implementsActionListener
  27.    {
  28.       privateJFrame parentComponent;
  29.       MyButtonListener(JFrameparentComponent)
  30.       {
  31.          this.parentComponent=parentComponent;
  32.       }
  33.       publicvoidactionPerformed(ActionEvente){
  34.          parentComponent.setVisible(false);
  35.       }
  36. }
  37. }
  38.  
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.
Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed(ActionEvent arg0) {    
  2. }
.
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
Apr 8 '08 #5
gaya3
184 100+
Thanks for all reply..
Its working fine..

-Thnaks & Regards,
Hamsa
Apr 11 '08 #6
hsn
237 100+
welcome gaya3

Good Luck

hsn
Apr 11 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Oliver Hirschi | last post: by
7 posts views Thread by Caution | last post: by
Shinzon
5 posts views Thread by Shinzon | last post: by
Shinzon
5 posts views Thread by Shinzon | last post: by
3 posts views Thread by coffeetime | last post: by
3 posts views Thread by coffeetime | last post: by
2 posts views Thread by zahit | last post: by
3 posts views Thread by notaCons | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.