473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 13817
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

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

Similar topics

4
by: Fereshteh Shojaei | last post by:
Hi, I wonder if somebody could help me. I would like to move the cursor from one JTextField to another one when I press <CR>. Regards, Fereshteh
0
by: Oliver Hirschi | last post by:
Hi, First of all sorry for my german posting before! Here is my question in english: By starting my application I construct a JFrame. By clicking onto a JButton on this JFrame, it construct...
7
by: Caution | last post by:
Ok.. bare with me, not sure how to word this. I am writing a program that stores airplane data. My problem right now is trying to figure out how to get the navigation to work. My program loads...
5
Shinzon
by: Shinzon | last post by:
ok so far I have got: JFrame frame = new JFrame( "Matt's DVD's" ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setSize( 420, 170 ); // set frame size ...
5
Shinzon
by: Shinzon | last post by:
Ok so I have been working on this for a moment now and wondering how to add multiple jlabels to a jframe. the code looks like this: JLabel jl = new JLabel("DVD ID #= " + dvd); //Displays DVD...
3
by: coffeetime | last post by:
Hi, I just decided to learn java, so I'm quite a noob. I use the book learning java third edition. have JDK 1.6 program in notepad. I have made the simple HelloJava example, and now i wanted to...
3
by: coffeetime | last post by:
Hi, I just decided to learn java, so I'm quite a noob. I use the book learning java third edition. have JDK 1.6 program in notepad. I have made the simple HelloJava example, and now i wanted to...
2
by: zahit | last post by:
hi guys, my line doesn't appear on the frame. what's the problem? import java.util.*; import java.io.*; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; public class...
3
by: notaCons | last post by:
hello im quite newbies in Java Programing so any one can help me. i will be plz.... what am i trying here is try convert The JFrame to JApplet, but when i change the JFrame to JApplet 4 Error come...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.