1

I am developing an application which needs to pop up new JFrame B with different components, when I click on a button on JFrame A. How do I achieve that?

I don't want to use the tabs.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
padman
  • 491
  • 1
  • 3
  • 14
  • 3
    Have you tried anything to solve the issue? – Mechkov Nov 18 '11 at 16:32
  • 5
    See also [`CardPanel`](http://stackoverflow.com/questions/5654926/implementing-back-forward-buttons-in-swing/5655843#5655843). – trashgod Nov 18 '11 at 16:33
  • 5
    `please solve my prob..` -- Sorry, that's not really what we're here for...We can get you started in the right direction though! – BenCole Nov 18 '11 at 16:35

4 Answers4

6

Use a JDialog , problem solved!

See this java tutorial for more help : How to Make Dialogs

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
  • But in JDialog,we can't add the Jtables,textfields...etc – padman Nov 18 '11 at 16:40
  • 1
    NO, We can add, the jdialog inherits from container class,as the javadoc says *A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT components.*, and note that the jtextfield and jtables are inherited from AWT components. – COD3BOY Nov 18 '11 at 16:55
  • 2
    I & most others can add pretty much any GUI component(s) we want to a `JDialog` or `JOptionPane` (or a `JApplet` or a `JWindow` or a tab in a `JTabbedPane` ..). Just put all the components in a single `JPanel` and add that to the final top-level component. – Andrew Thompson Nov 18 '11 at 16:57
5

I'm not sure why no one has suggested CardLayout yet, but this is likely your best solution. The Swing tutorials have a good section on this: How to use CardLayout

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

In a nutshell (a simple solution), you register a listener with the JButton and then have the listener perform the tasks you want it to perform:

setVisible(true) for one frame.
setVisible(false) for the other one.

Regards!

Mechkov
  • 4,294
  • 1
  • 17
  • 25
  • 1
    EW. What a disgusting resolution! – mre Nov 18 '11 at 16:38
  • @mre A solution is a solution friend. As it said a SIMPLE solution, maybe i should have said a DISGUSTING solution? So, if it is a valid solution, why the downvote? – Mechkov Nov 18 '11 at 16:40
  • @mre: amen! who wants windows popping in and out? – Hovercraft Full Of Eels Nov 18 '11 at 16:41
  • 1
    @Mechkov: it was my down-vote, not his. Your solution is a user's nightmare, that's why. – Hovercraft Full Of Eels Nov 18 '11 at 16:42
  • A solution is solution. So no need for the downvote as it will solve the problem. it is up to the topic creator to decide what works for him/her. Again, starting a chain reaction here fellas... – Mechkov Nov 18 '11 at 16:42
  • @Hovercraft Full Of Eels As i said a solution is a solution, whether it is ugly or "pretty" So no call for a downvote since this is "a valid solution" – Mechkov Nov 18 '11 at 16:43
  • +1 since -1 is too harsh. The question is nebulous and this answer is not "disgusting". – Ingo Kegel Nov 18 '11 at 16:44
  • 1
    I disagree. When there are much better solutions available, why suggest a bad one? – Hovercraft Full Of Eels Nov 18 '11 at 16:45
  • @IngoKegel, This solution is most certainly disgusting. It's promoting bad practice. And as far as usability goes, well...this solution pretty much throws that right out the window. – mre Nov 18 '11 at 16:46
  • Bad practice to have modal dialogs? Really? and you're downvoting stuff on that basis? – Ingo Kegel Nov 18 '11 at 16:49
  • I dont think you understand what "(a simple solution)" means? Means one of many that solves an issue but it is up to you to decide whether you want to implement it or not..It is a valid solution, so if you like it - implement it, if not use something else. Just because you drive your own car and someone else is being driven, does not mean they dont get to their destinations. – Mechkov Nov 18 '11 at 16:50
  • @mre I see you have added your downvote as well. OK, guys - you take our your anger on the people who spend time helping others - I'm out of here. – Ingo Kegel Nov 18 '11 at 16:51
  • If proposing this approach (and I **don't** recommend it) please also point out the frames will probably need to be changed to use `setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)`. – Andrew Thompson Nov 18 '11 at 17:12
0

One way to approach this would be to create another jFrame and then add a listener onto your button like so:

jFrameNew.setVisible(true);

This way you have a whole new frame to work with. If you want to just have a pop-up message you can also try using the jDialog frames.

Depending on which IDE you are using...for example Netbeans has a gui that makes designing interfaces slightly easier, so you can test out the different frames.

Max
  • 5,799
  • 3
  • 20
  • 27
  • 1
    Don't even suggest secondary JFrames as that's not what their for. If a program needs new windows they should be dependent on the main window or dialogs. Better even still to use to use CardLayout though. – Hovercraft Full Of Eels Nov 18 '11 at 16:39
  • @HovercraftFullOfEels As Mechkov is arguing for his solution (since ours are rather similar) I don't see why you downvoted this. It solves the problem, and I also mention dialogues. I read the authors question, "needs to pop up new JFrame-B" as he literally wants a new frame. – Max Nov 18 '11 at 16:45
  • 2
    Is it better to give the OP what he "wants" or the correct solution for his problem? – Hovercraft Full Of Eels Nov 18 '11 at 16:46
  • It's unclear what exactly the OP "wants". He said as long as it doesn't involve tabs. I merely gave him an option. How do you know exactly what layout he wants? – Max Nov 18 '11 at 16:47
  • @HovercraftFullOfEels This is a downvote based on your personal design preference which I'm going to revert with a +1. Unjust downvotes are demoralizing. – Ingo Kegel Nov 18 '11 at 16:47
  • @IngoKegel, All his downvotes are justified, whereas your upvotes are misleading and wrong. – mre Nov 18 '11 at 16:48
  • @Ingo: No, my downvote is for the original poster to know not to use this solution but to use better solutions such as either CardLayout or JDialogs. – Hovercraft Full Of Eels Nov 18 '11 at 16:49
  • @HovercraftFullOfEels But there is nothing WRONG with my solution. It is a solution. If, in theory your solution is better, it should receive more upvotes, no? – Max Nov 18 '11 at 16:51
  • @HovercraftFullOfEels I'm not saying these answers are good, but I strongly feel that they should not be downvoted. Upvote the good stuff. – Ingo Kegel Nov 18 '11 at 16:52
  • @IngoKegel: Point taken. Post edited to allow removal of down-vote. To remove edit later. – Hovercraft Full Of Eels Nov 18 '11 at 16:58
  • 2
    If proposing this approach of multiple frames (and I **don't** recommend it) please also point out the frames will probably need to be changed to use `setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)`. – Andrew Thompson Nov 18 '11 at 17:12
  • 1
    And still this post to be correct should mention that it is better to use JDialogs than to use multiple JFrames. – Hovercraft Full Of Eels Nov 18 '11 at 17:24