-1

I have 2 frames, the first frame has the nothing more and a button, which leads to another frame which will have all the components, like tabs which have more components.

The code I am using is:

 button_1.addActionListener(new ActionListener()
     {

      public void actionPerformed(ActionEvent e) {

         JFrame  Frame_2 = new JFrame();

         Frame_1.setVisible(false);
         Frame_2.setVisible(true);
    }
 });  

this is creating a new separate frame , but i want to create new JFrame over existing JFrame

update

@VinceEmigh +1

Thanks for the detail custom solution. It shows that someone is really willing to help, I am a self learner , started just 3 months ago so your code is bit difficult to understand, but the idea of using cardlayout did the work and i came up with a solution.

JFrame guiFrame = new JFrame();
CardLayout cards;
JPanel cardPane;

    JButton B_1 = new JButton("Next Card");
    B_1.setActionCommand("Next Card");
    B_1.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {
            cards.next(cardPane);
        }
    });

cards = new CardLayout();
cardPane = new JPanel();
cardPane.setLayout(cards);
cards.show(cardPane, "Main");

JPanel Card_1 = new JPanel();
JLabel background_1 = new JLabel(new ImageIcon("C:\\Users\\ME\\Desktop\\Back1.jpg"));
Card_1.add(background_1);
Card_1.add(B_1);

JPanel Card_2 = new JPanel();
JLabel background_2 = new JLabel(new ImageIcon("C:\\Users\\ME\\Desktop\\Back2.jpg"));
Card_2.add(background_2);

cardPane.add(Card_1, "Main");
cardPane.add(Card_2, "Sub");
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Farrukh khalid
  • 93
  • 1
  • 2
  • 9

1 Answers1

1

You shouldnt use 2 frames. You should use 1 frame, then switch between panels in the frame using CardLayout. Unless you're referring to nesting a frame within a frame, creating 2 different frames for 1 applicarion is typically bad practice, and should be avoided if possible.

Set your frames layout to CardLayout, add 2 panels to your frame. One panel contains the button, the other has the components.

When your button event triggers throuhh an actionlistener, switch out the panels using the cardlayout you put for the frames layout.

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class App extends JFrame {
    private CardLayout cl = new CardLayout();
    private JPanel firstPanel = new FirstPanel();
    private JPanel secondPanel = new SecondPanel();

    public App() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setLayout(cl);

        add(firstPanel, "first");
        add(secondPanel, "second");

        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void switchPanel(String name) {
        cl.show(getContentPane(), name);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                App app = new App();
            }
        });
    }

    class FirstPanel extends JPanel implements ActionListener {
        private JButton button = new JButton("Button");

        public FirstPanel() {
            button.addActionListener(this);
            add(button);
        }

        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == button) {
                switchPanel("second");
            }
        }
    }

    class SecondPanel extends JPanel { }
}   
Vince
  • 14,470
  • 7
  • 39
  • 84
  • 1
    +1 for suggesting `CardLayout`. -1, for posting a custom solution instead of linking to the section from the Swing tutorial on [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) which contains explanations and a better structured working example. Creating a class with all static variables and methods is not a good design. – camickr Apr 30 '14 at 20:34
  • 1
    @camickr This is a very simple example of CardLayout. Oracle's examples contain other aspects not needed in this situation, and as someone who used Oracle's trails as a complete beginner, I understand how sometimes irrelevant context could make the code examples harder to understand. Although, a link to Oracle's official guide should have been included in the answer, this answer is targeted towards his exact situation. As for static, for answers, maybe I shouldn't use static. But in this case, they belong to the class because I'm not instantiating App. – Vince Apr 30 '14 at 20:58
  • 1
    Point the OP is the right direction. If the OP has trouble understanding the tutorial then the OP can ask a question. Why show what you think is a better CardLayout example if you don't show a better way to create the class. If you think the OP won't understand the tutorial then the OP probably won't understand why you are using static variables and methods and will just blindly copy your code thinking the code is necessary. – camickr Apr 30 '14 at 23:54
  • @camickr The tutorial contains other irrelevant Swing components. The point was to show him how to use CardLayout, not how to write a flexible application that utilizes CardLayout. If you're worried about him copying and pasting, what makes you think he wouldn't copy Oracle's code, thinking everything is necessary, such as the ItemListener? If he can create get this far in programming, Im sure he has came across `You cannot use a non-static reference in a static field` error, which is really the only reason why I made them static. I only brought up instantiating to show the design still works – Vince May 01 '14 at 00:03
  • 1
    @VinceEmigh I support camickr on this, right idea with a bad example which is only going to teach the OP bad habits and cause more problems in the future which is going to lead to more pointless questions. `static` is not a solution to the problem. Instead of creating the UI in the `main` method, create an instance of `App` and do it there, problem solved... – MadProgrammer May 01 '14 at 00:37
  • @MadProgrammer Thanks for reminding me. You're right, I used static to solve a problem without even realizing. I felt that in context to the application, making them static would make sense in the long-run anyways. But both of you are right, the OP would see it as a solution to the static problem soley, even if the design made sense. Sorry for arguing camickr, I wasn't realizing the real problem with me using static for the example – Vince May 01 '14 at 00:47
  • @MadProgrammer Edited, but unless I put the ActionListener and JButton inside the App class, I need some way of accessing `switchPanel(String);` in App. As much as I want to remove static, it's either use static or use nested classes. EDIT: I changed the example to nested classes – Vince May 01 '14 at 01:13
  • @VinceEmigh Pass a reference of `App` to the other panels. Personally, I prefer to have `App` manage the navigation itself as the other panels shouldn't now or care...IMHO – MadProgrammer May 01 '14 at 01:15