JFRAME and Window?????????


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: JFRAME and Window?????????

  1. #1
    Join Date
    Feb 2005
    Posts
    10

    JFRAME and Window?????????

    Hello,

    I created JFrame with JMenu and its items. But I dont know how to add a Window into JFrame when one of the JMenu items is selected and I want to create a new window for all other menu items. Below is the sample code.

    PHP Code:
    public class MainFrame extends javax.swing.JFrame {
        private 
    JMenuBar menuBar;

        private 
    JMenu fileMenu;
            private 
    JMenuItem connectItem;
        private 
    JMenuItem exitItem;
        private 
    Connection connection;

       
        public 
    MainFrame() {

            
    super"Application" );

            
    menuBar = new JMenuBar();

            
    fileMenu = new JMenu();
            
    connectItem = new JMenuItem();
            
    exitItem = new JMenuItem();

            
    fileMenu.setMnemonic('F');
            
    fileMenu.setText("File");

            
    connectItem.setMnemonic('L');
            
    connectItem.setText("Connect");
            
    fileMenu.add(connectItem);
            
    connectItem.addActionListener(

        new 
    ActionListener() // anonymous inner class
        
    {
            public 
    void actionPerformedActionEvent event )
            {

        
                           
                }
    //end of action performed
            
    }//end of inner class
            
    ); // end of addAction Lister

            
    exitItem.setMnemonic('E');
            
    exitItem.setText("Exit");
            
    exitItem.addActionListener(
            new 
    ActionListener() // anonymous inner class
                    
    {
            public 
    void actionPerformedActionEvent event )
            {

                 
    System.exit( );
            }
    //end of action performed
                    
    }//end of inner class
            
    ); // end of addAction Lister

            
    fileMenu.add(exitItem);
            
    menuBar.add(fileMenu);

            
    setJMenuBar(menuBar);// set menu bar for this application

     
    }//enf of Public MainFrame


    }//end of class Main Frame 

    thank you for your help in advance

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    A solution with panels...

    When you say you want to add a new window into a JFrame, then that has
    to be a panel, and that is what this solution uses.
    I also plucked out the inner anonymous listener classes and made the
    frame an action listener; why people want make a new inner anonymous
    class for every little responsive thingie in a GUI beats me....

    In this setup the panels are stored in a hashtable for reuse, e.g. when the
    user selects 'connect' then the previous connect panel (if any) will show.

    Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.sql.*;
    import java.util.*;
    
    class CenterPanel extends JPanel {
      private JLabel lbl=new JLabel();
      private JTextField tf=new JTextField(22);
      public CenterPanel(String choice) {
        lbl.setText(choice);
        add(lbl);
        add(tf);
      }
      public String getLabelText() {
        return this.lbl.getText();
      }
      public boolean equals (Object ob) {
        if (! (ob instanceof CenterPanel) ) return false;
        CenterPanel p=(CenterPanel)ob;
        return lbl.getText().equals(p.getLabelText());
      }
    
    }
    
    public class MainFrame
        extends javax.swing.JFrame  implements ActionListener  {
      private JMenuBar menuBar;
    
      private JMenu fileMenu;
      private JMenuItem connectItem;
      private JMenuItem testItem;
      private JMenuItem exitItem;
      private Connection connection;
      private Hashtable panelHt=new Hashtable();
    
      public MainFrame() {
    
        super("Application");
    
        getContentPane().setLayout(new BorderLayout());
    
        menuBar = new JMenuBar();
    
        fileMenu = new JMenu();
        connectItem = new JMenuItem();
        exitItem = new JMenuItem();
        testItem = new JMenuItem();
    
        fileMenu.setMnemonic('F');
        fileMenu.setText("File");
    
        connectItem.setMnemonic('L');
        connectItem.setText("Connect");
    
        testItem.setMnemonic('T');
        testItem.setText("Test");
    
    
        exitItem.setMnemonic('E');
        exitItem.setText("Exit");
    
        // anonymous inner listener classes sux....
        connectItem.addActionListener(this);
        testItem.addActionListener(this);
        exitItem.addActionListener(this);
    
        fileMenu.add(connectItem);
        fileMenu.add(testItem);
        fileMenu.add(exitItem);
    
        menuBar.add(fileMenu);
    
        setJMenuBar(menuBar); // set menu bar for this application
    
      } //enf of Public MainFrame
      /**
       * Handle the actions
       * @param e
       */
      public void actionPerformed(ActionEvent e) {
       if (e.getActionCommand().equals("Exit")) {
          System.exit(0);
       } else {
         setCenterPanel(e.getActionCommand());
       }
     }
    
      /**
       * Panel swapping
       * @param s
       */
      private void setCenterPanel (String s) {
        // first remove any old centerpanel
        Component [] cmp=getContentPane().getComponents();
        for (int i=0; i<cmp.length; i++) {
          if (cmp[i] instanceof CenterPanel) {
            getContentPane().remove(cmp[i]);
            break;
          }
        }
        // then check if we have made the panel already, lookup in hashtable.
        CenterPanel aPanel=(CenterPanel)panelHt.get(s);
        if (aPanel==null) {
          aPanel=new CenterPanel(s); // nope, make a new one
          panelHt.put(aPanel.getLabelText(),aPanel); // store in hashtable
        }
        getContentPane().add(aPanel, BorderLayout.CENTER); // show it
    
        validate();
        repaint();
      }
      public static void main (String [] args) {
        MainFrame mf=new MainFrame();
        mf.setBounds(20,20,400,300);
        mf.setVisible(true);
      }
    } //end of class Main Frame          }//end of class Main Frame
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center