Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Swing app gui optimization

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Swing app gui optimization

    Initially I had wrongly designed a swing app gui which used static methods in the main method.

    Then I realised I could've done it better. I've designed a rough outline of the app which might be correct per se, but maybe not optimized. I also decided to make the app thread-safe.

    Here's an outline:

    public class MyApp {
    	JMenuBar myMenuBar = null;
    	JToolBar myToolBar = null;
    	JMenuItem myMenuItem1 = null;
    	JMenuItem myMenuItem2 = null;
    	JPanel northPanel = null;
     
    	public JPanel createNorthPanel() {
    		northPanel = new JPanel();		
    		northPanel.add(createMenuBar());
    		northPanel.add(createToolBar());
    		return northPanel;
    	}
     
    	// similarly, create 'west', 'center', 'east' and 'south' panels
     
    	public JMenuItem createMenuItem(String text, char mnemonic, String keyStrokeRepresentation) {
    		JMenuItem newMenuItem = new JMenuItem();
    		// set respective properties...		
    		return newMenuItem;
    	}		
     
    	public JMenuBar createMenuBar() {
    		myMenuBar = new JMenuBar();		
    		myMenuBar.add(myMenuItem1 = createMenuItem("MyMenuItem1", 'M', "control M"));	
    		myMenuItem1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent actionEvent) {
    				// take some action here...
    			}
    		}	
     
    		// similarly, create other menu items		
    		return myMenuBar;
        }
     
    	public static void main(String args[]) {
    		// implementing thread safety...
    		Runnable runner = new Runnable() {
    			public void run() {
    				JFrame mainFrame = new JFrame();				
    				MyApp appInstance = new MyApp();				
    				mainFrame.add(appInstance.createNorthPanel(), BorderLayout.NORTH);
     
    				// similarly, create 'west', 'center', east' and 'south' panels...
    				mainFrame.add(appInstance.createWestPanel(), BorderLayout.WEST);
    				mainFrame.add(appInstance.createCenterPanel(), BorderLayout.CENTER);				
    				mainFrame.add(appInstance.createEastPanel(), BorderLayout.EAST);				
    				mainFrame.add(appInstance.createSouthPanel(), BorderLayout.SOUTH);					
    				mainFrame.setVisible(true);
    				mainFrame.setSize(400, 300);
    			}
    		};		
    		EventQueue.invokeLater(runner);
    	}
    }

    Is everything all right with the code, or can it be better optimized? Plz overlook the length of the code. It's rather long, I admit. Thanks.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Swing app gui optimization

    I have two comments:
    1) It is always a good idea to specify what you want to happen to the program when the little X on the top right corner of the JFrame is pressed. For whatever reason, JFrame does not handle this directly as you would want with your main frame because, if you do not, when the X is pressed the program actually still runs. Specifically, you want to use the JFrame.setDefaultCloseOperation() method to indicate what you want the program to do in the event the JFrame is closed. Since this is your main frame, I would assume you want the program to stop executing. If so, this is the statement you would use:
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    2) You do not use the JMenu you created. If you do want to use that, you should use the JFrame.setJMenuBar() method.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Replies: 2
    Last Post: November 25th, 2010, 01:59 PM
  2. Swing
    By waboke in forum AWT / Java Swing
    Replies: 2
    Last Post: November 3rd, 2010, 08:55 AM
  3. Text in Swing
    By whoismrsaxon in forum AWT / Java Swing
    Replies: 4
    Last Post: March 26th, 2010, 07:22 AM
  4. Swing Timers
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 5
    Last Post: November 10th, 2009, 09:10 PM
  5. java swing help
    By JM_4ever in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2009, 06:42 AM