473,219 Members | 2,016 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,219 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 13800
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.