81528

Multiple instance of ONE JFrame

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:

<strong>FrameToOpen.java</strong>

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

<strong>OtherClass.java</strong>

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.

<strong>'Actual' Classes</strong>

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... }

Answer1:

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.

Recommend

  • How to bring jinternalframe to front from all opened jinternalframes?
  • Adding text to a label from another class - Simple Logic Issue
  • Wrapping Line, Right Align,Auto Resize Row Height In JTable
  • Keyboard not inputing anything java
  • Adding additional shapes in a tetris project. loop logic assistance
  • How to set the color of JButtons in an array in java?
  • Dimple.js - Add data labels to each bar of the bar chart
  • Override/Rewrite a javascript library function
  • How to open form2 on the second monitor?
  • Java GUI Problems
  • Display some part of text in bold and some part in italic in SWT label
  • How to use Swing Timer ActionListener
  • How to make the JDialog invisible when the JProgressBar reaches 100%?
  • STM_SETIMAGE doesn't invalidate/repaint the static control
  • How to move move a rectangle in JFrame using KeyListener?
  • How to create a dollar amount input field in an HTML form?
  • GridBagLayout padding
  • ValidationResult Returned From IValidatableObject.Validate Is Not Localized
  • iOS Validation failed -> Bundle is invalid (Xcode 6.2, iOS 8.2)
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • What is sun.awt.windows.WToolkit?
  • setSelected() with JRadioButton r[]=new JRadioButton[3] not working [duplicate]
  • Java making confirming exit
  • opencv display image without x server
  • How do I shift the decimal place in Python?
  • Application level floating views with navigation in Android
  • Row to Column conversion in Talend
  • jQuery Orbit - How to make a Random Slideshow?
  • Bad request using file_get_contents for PUT request in PHP
  • JFileChooser in front of fullscreen Swing application
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Validaiting emails with Net.Mail MailAddress
  • Can I make an Android app that runs a web view in Chrome 39?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • JTable with a ScrollPane misbehaving
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • How can i traverse a binary tree from right to left in java?