2

In Java, I have 2 classes. One contains a JFrame. On startup, that class is called. The JFrame shows.

But in the other class, when I press a button on it's own frame, it opens a new instance of that class which should make another frame. But it just focuses on the old frame already opened...

Source:

FrameToOpen.java

public FrameToOpen() {
    JFrame frame = new JFrame();
    // Just the most simple settings to make it appear...
    frame.setSize(400, 200);
    frame.setVisible(true);
}

OtherClass.java

public OtherClass() {
    JFrame frame = new JFrame();
    JPanel window = new JPanel();
    JButton openFrame = new JButton("Open Frame);
    // Again, just the most simple settings to make it appear with components...
    frame.setSize(400, 200);
    frame.setVisible(true);
    frame.add(window);
    window.setLayout(null);

    window.add(openFrame);
    openFrame.setBounds(5, 5, 100, 30);

    openFrame.addActionListener(this);

    frame.repaint();
    frame.validate();
}

public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();
    if (o == openFrame) {
        // THIS HERE MAKES NEW INSTANCE OF FRAMETOOPEN
        new FrameToOpen();
    }
}

So, when I press this button, it doesn't open a new frame, but just focuses on old one.

Please help.

'Actual' Classes

ServerGUI.java

    if (o == openAdmin) {
        int port;
        try {
            port = Integer.parseInt(portNumber.getText().trim());
        } catch(Exception er) {
            appendEvent("Invalid Port Number.");
            return;
        }

        // FrameToOpen.java. Opening a new instance of that class...
        new ClientGUI("localhost", port, true);
    }

ClientGUI.java

static JFrame frame = new JFrame("Chat Client");
Dimension d = new Dimension(600, 600);
JMenuBar menu = new JMenuBar();

public ClientGUI(String host, int port, boolean isHost) {

        this.isHost = isHost;

        frame.setSize(d);
        frame.setMinimumSize(d);
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setJMenuBar(menu);
        frame.setVisible(true);


        // Everything else in the class is my buttons, lists, editor panes,
        // and socket handling...

}
Sk4llsRPG
  • 259
  • 4
  • 13
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. – Andrew Thompson Aug 04 '14 at 10:37
  • I actually read that article JUST before posting this, and I actually couldn't find anything helpful... All I want to do is be able to have two instances of one class at once in the form of a JFrame. (Having two of it open at once) – Sk4llsRPG Aug 04 '14 at 10:39
  • Why frame is defined inside the constructor? How do you mantain the reference to that JFrame? – inigoD Aug 04 '14 at 10:41
  • Can not reproduce. Button creates a new frame every time just fine. This obviously is not your actual code (dummy names and syntax errors). Show us your real code, or a minimal portion of it to reproduce your problem. – tobias_k Aug 04 '14 at 10:41
  • The problem (which isn't explained very clearly) seems to be *"but just focuses on old one."*. That **would not be a problem** if the 2nd 'frame' was in fact a `JDialog` with the actual `JFrame` specified as the owner. – Andrew Thompson Aug 04 '14 at 10:42
  • No, this is my actual code. I actually use frame and window. Will upload actual code. And actually just moved JFrame frame; inside to not need to include whole class... – Sk4llsRPG Aug 04 '14 at 10:43
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). – Andrew Thompson Aug 04 '14 at 10:44
  • ..or perhaps this actually needs a `CardLayout`. The question seems inconsistent and confusing as to what actual effect is required. – Andrew Thompson Aug 04 '14 at 10:46
  • One frame; two instances, for [example](http://stackoverflow.com/a/3245805/230513). – trashgod Aug 04 '14 at 10:47
  • @trashgod, how is that relevant sorry...? – Sk4llsRPG Aug 04 '14 at 10:49
  • @tobias_k ActionListener... I added an actionlistener to the button. It equals true when the button is pressed. Also I added the 'true' class... – Sk4llsRPG Aug 04 '14 at 10:50
  • @Sk4llsRPG Yes, but are you 100% positively sure that the body of the if-clause is executed? Did you put some print statement in there to see? Because in your example code, it will _not_ be executed, because the member variable `openFrame` is _not_ the same button you attach the action listener to. – tobias_k Aug 04 '14 at 10:52
  • Well I don't know whats wrong with that but the button executes fine. It just checks the action listener and converts it to an Object. The second that button is pressed it equals true, so then the if statement kicks in. That is actually the class with the `openFrame` as that part is 100% not edited. – Sk4llsRPG Aug 04 '14 at 10:55

1 Answers1

4

You defined your frame variable as being static:

static JFrame frame = new JFrame("Chat Client");

so it is created only once for the class, no matter how many instances are created. Remove the static modifier if you want to hava it as an instance field.

user85421
  • 28,957
  • 10
  • 64
  • 87