Results 1 to 8 of 8
  1. #1
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default Newbi. What way is the best?

    God morning.

    Learning diffrent ways to type my code as time go. Need an advice on witch exampel is the best. And why it is the best.

    Exampel 1.
    Java Code:
    public JMenuBar createMenu() {
    	// Create the menu system
    	JMenuBar aMenuBar = new JMenuBar();
    	JMenu aMenu = new JMenu("Arkiv");
    	JMenuItem aMenuItem = new JMenuItem("Close");
    	aMenuBar.add(aMenu);
    	aMenu.add(aMenuItem);
    	return aMenuBar;
    }// end menu
    
    public static void gui() {
    	// Create gui AND thing in it
    	JTextField aTextField = new JTextField();
    	JLabel aLabel = new JLabel("Text1: ");
    	JFrame aFrame = new JFrame("myGui 1");
    	aFrame.setSize(200, 200);
    	aFrame.setVisible(true);
    	aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	JPanel aPanel = new JPanel(new BorderLayout());
            aLabel.setLabelFor(aTextField);
    	aPanel.add(aLabel, BorderLayout.WEST);
    	aPanel.add(aTextField, BorderLayout.CENTER);
    	aFrame.add(aPanel, BorderLayout.NORTH);
    	mygui gui = new mygui();
    	aFrame.setJMenuBar(gui.createMenu());
    	aFrame.pack();
    }// end gui
    	
    	public static void main(String[] args) {
    		gui(); // runs gui.
    	
    		
     }
    }
    Exampel 2.
    Java Code:
    private JMenuBar createMenu() {
    	// Create the menu system
    	JMenuBar aMenuBar = new JMenuBar();
    	JMenu aMenu = new JMenu("Arkiv");
    	JMenuItem aMenuItem = new JMenuItem("Close");
    	aMenuBar.add(aMenu);
    	aMenu.add(aMenuItem);
    	return aMenuBar;
    }// end menu
    
    private JPanel createThings() {
    	// Things to be on the frame
    	JTextField aTextField = new JTextField();
    	JLabel aLabel = new JLabel("Text2: ");
    	JPanel aPanel = new JPanel(new BorderLayout());
    	aLabel.setLabelFor(aTextField);
    	aPanel.add(aLabel, BorderLayout.WEST);
    	aPanel.add(aTextField, BorderLayout.CENTER);
    	return aPanel;
    } // end things
    
    private static void gui() {
    	// create and show the gui
    	JFrame aFrame = new JFrame("myGui 2");
    	aFrame.setSize(200, 200);
    	aFrame.setVisible(true);
    	aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	mygui frame = new mygui();
    	aFrame.add(frame.createThings(), BorderLayout.NORTH);
    	mygui gui = new mygui();
    	aFrame.setJMenuBar(gui.createMenu());
    	aFrame.pack();
    } // end gui
    
    public static void main(String[] args) {
    	gui();
    }
    }
    Thankfull for any advice that can make me better. / Ocean.
    Last edited by ocean; 10-01-2009 at 05:59 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    The only differences I could see from your two snippets is that in the second one, the Panel is created in a separate method. That helps if you have to create that panel many times, in which case you don't have to type up all that code. Just call the method. Since the panels returned will always look exactly the same, it would be useful only if the panel returned is in fact reusable.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    I dunno.
    Personally I like to break things down like that.
    It may not make any functional difference, but I find it easier to read a makeThisPanel(), than attempt to find the bits that go towards making thisPanel in a single method. Tends to be the way I write code as well, starting with something like:

    Java Code:
    public void buildPanel()
    {
        // make top search panel
    
        // make center result panel
    
        // make bottom button panel
    }
    and then actually do methods representing those comments.

    What I will say, though, is the gui() call should be in a SwingUtilities.invokeLater() call.

  4. #4
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Yes thas is the only diffrence, And i made them private. wich is a bad ide if i need to reuse them? made this exampel very simply just to show my point. But lets say im making a really big program. does it make any diffrence in memory use or speed?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Making them private's not a problem.
    I wouldn't have a generic toolkit for panels anyway.
    They tend to be local to whatever screen I'm building.

  6. #6
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    To make SwingUtilities.invokeLater() work i had to make gui runnable and add return statement to gui(). Since invokeLater are new to me i looked at sun doc. i can´t figure out why i should use it. Any help explaining it would be greatful.

    EDIT: 2009-10-01 kl 12:00

    After some more reading i found out how you mean. and made

    Java Code:
    public static void main(String[] args) {
    	SwingUtilities.invokeLater(new Runnable() {
    	    public void run() {
    	    gui();
    Is this the correct way to run gui()?
    Last edited by ocean; 10-01-2009 at 11:43 AM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Yep, that's the way you do it.
    It makes sure the gui is built by the Event Dispatch Thread (the thread that deals with all the gui stuff, or should anyway). Good practice, basically.

  8. #8
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Thank you Tolls for pointing me in the rigth direction. Now I can move on with other tasks.

Similar Threads

  1. newbi. ActionEvent. Close on click
    By ocean in forum New To Java
    Replies: 3
    Last Post: 09-18-2009, 09:41 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
  •