Results 1 to 6 of 6
  1. #1
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default How do you make another window appear

    Ok, here is the situation. I am working on my final project for my java programing class at school and I have run into a bit of a problem. I need to know how to bring up a new "window" when I click something on the main GUI window. For example, I have a menu item that says help, and I have a class in the same package as the main GUI window called help.java. Basically, this help class is a window all in inself in that it extends JFrame and has a main method from which it can open. I've added an action listener to
    my help menu item, what should I do in the action performed method to make this help window pop up?

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    create the JFrame, pack it (if using layout managers -- which you should be doing), and finally call setVisible(true).

    Though most often a second window should be a JDialog rather than a new JFrame.

  3. #3
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    So say I'm trying to display the help class window, should I delete the main method from there and basically put it in the action performed method in my main GUI window?

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    There's no need to delete the main method, but rather than ask, you're far better of trying and seeing what happens. You won't break anything. ;)

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Here is an example:

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class OpenANewWindow {
    	public static void main(String[] args) {
    		MainWindow frame = new MainWindow();
    		frame.showGUI();
    	}
    }
    
    class MainWindow extends JFrame {
    	JButton button;
    	JPanel panel;
    	NewWindow newWindow = new NewWindow();
    	
    	public MainWindow() {
    		panel = new JPanel();
    		newWindow = new NewWindow();
    		button = new JButton("New Window");
    		
    		button.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				newWindow.setVisible(true);
    			}
    		});
    		
    		panel.add(button);
    		
    		getContentPane().add(panel);
    	}
    	
    	public void showGUI() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		setLocation(100,100);
    		setVisible(true);
    	}
    }
    
    class NewWindow extends JFrame {
    	public NewWindow() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(200,200);
    		setLocation(300,300);
    		setVisible(false);
    	}
    }
    But, my advice is to use JOptionPane instead of NewWindow.

  6. #6
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Version with JOPtionPane, and class Help.java

    class: OpenANewWindow2.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JMenuBar;
    
    public class OpenANewWindow2 {
    	public static void main(String[] args) {
    		MainWindow frame = new MainWindow();
    		frame.showGUI();
    	}
    }
    
    class MainWindow extends JFrame {
    	JButton button;
    	JPanel panel;
    	JOptionPane help;
    	Help helpPanel = new Help();
    	
    	JMenuBar menuBar;
    	JMenu helpMenu;
    	JMenuItem helpItem;
    	
    	public MainWindow() {
    		panel = new JPanel();
    		
    		menuBar = new JMenuBar();
    		helpMenu = new JMenu("Help");
    		helpItem = new JMenuItem("Help");
    		helpMenu.add(helpItem);
    		menuBar.add(helpMenu);
    		add(menuBar, BorderLayout.NORTH);
    		
    		
    		helpItem.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				int result = help.showOptionDialog(help.getRootFrame(), helpPanel, "Help", JOptionPane.OK_CANCEL_OPTION,
    						JOptionPane.QUESTION_MESSAGE, null, null, null);
    			}
    		});
    		
    		getContentPane().add(panel);
    	}
    	
    	public void showGUI() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		setLocation(100,100);
    		setVisible(true);
    	}
    }


    class: Help.java

    Java Code:
    import java.awt.BorderLayout;
    
    import javax.swing.JTextField;
    import javax.swing.JTextArea;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    public class Help extends JPanel {
    	JTextField textField;
    	JTextArea textArea;
    	
    	public Help() {
    		textField = new JTextField(20);
    		textArea = new JTextArea(20,6);
    		setLayout(new BorderLayout());
    		add(textArea, BorderLayout.SOUTH);
    		add(textField, BorderLayout.NORTH);
    	}
    }

Similar Threads

  1. Replies: 4
    Last Post: 01-10-2010, 07:48 AM
  2. change url in parent window from child window
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-09-2009, 12:17 PM
  3. how can i move one frame window to another window
    By santhosh_el in forum AWT / Swing
    Replies: 8
    Last Post: 06-10-2009, 03:36 PM
  4. how to make window active and textfield focused
    By whwillisiv in forum New To Java
    Replies: 0
    Last Post: 04-28-2009, 04:06 AM
  5. Replies: 5
    Last Post: 08-26-2008, 03:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •