100+
P: 184
|
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
| |
Share this Question
Expert 2.5K+
P: 3,112
|
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
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
| | 100+
P: 184
|
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
| | Expert 2.5K+
P: 3,112
|
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: -
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
| | 100+
P: 237
|
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. -
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. - 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
| | 100+
P: 184
|
Thanks for all reply..
Its working fine..
-Thnaks & Regards,
Hamsa
| | 100+
P: 237
|
welcome gaya3
Good Luck
hsn
| | | | Question stats - viewed: 12635
- replies: 6
- date asked: Apr 8 '08
|