Java Swing GUI Newbie


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Java Swing GUI Newbie

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    2

    Java Swing GUI Newbie

    Hi

    Ive just been assigned my last 16 week project for university but have stumbled across major slowdown with the GUI element of things. Iam using java swing to create a frame and a menubar (done) but I need to be able to have 2 internal frames, one holding a calendar class, the other holding a 'form'. However for the life of me I cant get an internal frame to work, what am I doing wrong? please help! It would be an amazing start for someone just to modify my code so that it will add an internal frame.

    Btw Iam running BlueJ and hence it needs to run inside this program. Iam new to java, approx 4 weeks since I started so please be gentle although at heart Iam a hardcore php'er so I understand about variables, arrays, lists etc


    Thanks
    Mike

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.JInternalFrame;
    
    
    /**
     * Disco Organiser GUI 1.
     * 
     * @author Michael Allam 
     * @version 1
     */
    public class DiscoOrganiser
    {
    
        //Fields
        private JFrame window;
        private JFrame windowb;
        private String version="v1.0";
        private JButton searchbtn = new JButton("Search");
    
        /**
         * Constructor for class DiscoOrganiser
         */
        public DiscoOrganiser()
        {
            drawFrame();
        }
    
    
        //Draw Frame
        public void drawFrame()
        {
                 
            //Created the frame
            window = new JFrame("MyDiscoOrganiser "+version);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //JDesktopPane desktop = new JDesktopPane();
            
            //Setup Container
            Container contentPane = window.getContentPane();
            contentPane.setLayout(new BorderLayout());
            contentPane.setBackground(Color.white);
            contentPane.setSize(640,480);       //Dont work??
            
            //Center frame
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            window.setLocation(d.width/2 - window.getWidth()/2, d.height/2 - window.getHeight()/2);
            
            //Make menubar
            createMenuBar(window);
    
            //Create Internal Frame For Calendar!
            //-----------------------------------
            // HELP!
            
            //Set frame visible
            window.pack();        
            window.setVisible(true);
        }  
        
        
        
        //Make Menu Bar
        public void createMenuBar(JFrame frame)
        {  
            
            //Create Menu
            JMenuBar menubar = new JMenuBar();
            frame.setJMenuBar(menubar);
            JMenu menu;
            JMenuItem item;
       
     
            //Create The File Menu
            menu = new JMenu("File");
            menubar.add(menu);
            item = new JMenuItem("Setup");
            menu.add(item);
            menu.addSeparator();
            item = new JMenuItem("Import Data");
            menu.add(item);
            item = new JMenuItem("Export Data");
            menu.add(item);
            menu.addSeparator();
            item = new JMenuItem("Import Template");
            menu.add(item);
            item = new JMenuItem("Export Template");
            menu.add(item);
            menu.addSeparator();
            item = new JMenuItem("Quit");
            item.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { quit(); }});
            menu.add(item);
            
          
            //Create The View Menu
            menu = new JMenu("View");
            menubar.add(menu);
            item = new JMenuItem("Statistics");
            menu.add(item);
            menu.addSeparator();
            item = new JMenuItem("Calendar");
            menu.add(item);
            item = new JMenuItem("Clients");
            menu.add(item);
            item = new JMenuItem("Venues");
            menu.add(item);
            item = new JMenuItem("Employees");
            menu.add(item);
            item = new JMenuItem("Music Database");
            menu.add(item);
            menu.addSeparator();
            item = new JMenuItem("Contracts In XX Year");
            menu.add(item);
            item = new JMenuItem("Contracts In XX Month");
            menu.add(item);
            item = new JMenuItem("Contracts In Date Range");
            menu.add(item);
            
            
            //Create The Tools Menu
            menu = new JMenu("Tools");
            menubar.add(menu);
            item = new JMenuItem("Add Booking");
            menu.add(item);      
            item = new JMenuItem("Edit Booking");
            menu.add(item);  
            item = new JMenuItem("Delete Booking");
            menu.add(item);  
            menu.addSeparator();
            item = new JMenuItem("Invoices");
            menu.add(item);  
            item = new JMenuItem("Statements");
            menu.add(item);  
            item = new JMenuItem("Contracts");
            menu.add(item);  
            item = new JMenuItem("Surveys");
            menu.add(item);  
            item = new JMenuItem("Misc Letters");
            menu.add(item);  
            menu.addSeparator();
            item = new JMenuItem("Finances");
            menu.add(item);  
            menu.addSeparator();
            item = new JMenuItem("Music Database");
            menu.add(item);  
            
            
            //Create The Search Menu
            menu = new JMenu("Search");
            menubar.add(menu);
            item = new JMenuItem("By FirstName");
            menu.add(item);
            item = new JMenuItem("By LastName");
            menu.add(item);
            item = new JMenuItem("By Organisation");
            menu.add(item);        
            item = new JMenuItem("By Phone Number");
            menu.add(item); 
            item = new JMenuItem("By Address");
            menu.add(item); 
            item = new JMenuItem("By Venue");
            menu.add(item); 
            item = new JMenuItem("By Set Date");
            menu.add(item); 
            item = new JMenuItem("By Event Type");
            menu.add(item); 
            
            
            //Create The Setup Menu
            menu = new JMenu("Setup");
            menubar.add(menu);
            item = new JMenuItem("Company Details");
            menu.add(item);
            item = new JMenuItem("Employees");
            menu.add(item);
            item = new JMenuItem("Availability");
            menu.add(item);
            item = new JMenuItem("Templates");
            menu.add(item);
            item = new JMenuItem("Database");
            menu.add(item);
            
            
            //Glue To The Right
            menubar.add(Box.createHorizontalGlue());
            
            //Create The Help Menu
            menu = new JMenu("Help");
            menubar.add(menu);
            item = new JMenuItem("Help");
            menu.add(item);
            item = new JMenuItem("Online Help");
            menu.add(item);
            menu.addSeparator();
            item = new JMenuItem("About");
            item.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { showAbout(); }});
            menu.add(item);
                           
        }
       
        
        
        
        
        // ----------------------------
        // -- MENU CONTROL FUNCTIONS --
        // ----------------------------
        
        //Quit Application
        //Allows File->Quit to close application
        private void quit()
        {
            System.exit(0);
        }
        
        //Product About Page
        //Allows buttons/links to show product about box
        private void showAbout()
        {
            JOptionPane.showMessageDialog(window,"MyDiscoOrganiser "+version+" by Michael Allam\n", "About MyDiscoOrganiser", JOptionPane.INFORMATION_MESSAGE);
        }
    }

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

    Check this out

    I have fixed/added to your code a wee bit...

    This is my way of doing it; I make one JInternalFrame extension so all
    my internal frames looks/behaves basically the same. Then I implement my
    internal 'frames' as JPanels that I stuff into the center of my JInternalFrame extension.

    Notice that I pass the mainFrames pointer to these panels to facilitate
    invokation of methods/classes in the mainFrame from the panels.

    Also, the way to hook up menuitems, and responding to them is a bit
    simpler here.

    If you need to have it all in one file (?) you can just copy the class source
    into the java file that contains the static void main method (stack them
    above each other),
    just remember that only the class containing this main must be declared public.

    Good luck
    Attached Files Attached Files
    Last edited by sjalle; 03-07-2005 at 05:53 AM.
    eschew obfuscation

  3. #3
    Join Date
    Mar 2005
    Posts
    2
    Hi

    Thanks, Its only in one file at the moment as tbh I have no idea what Iam doing but rather a hack and slash attempt to just get something working at least. Going to have a play with the code you just sent me to see if I can get any further, many thanks for an amazingly quick reply too.

    That new function for handling the internal frames has solved all my problems thanks, Ive left it as 2 class's as I was using 2 class's originally anyway so might as well leave it as it is.

    Cheers
    Mike
    Last edited by Vapourmike; 03-07-2005 at 09:24 AM.

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