Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    'Random Movie Picker' - GUI

    I'm new to Java so I decided to create a very basic program. I'm creating a Random Movie Picker. Basically you browse to a directory, the program will select a file at random, and then output the name.

    I ran into 2 small problems with the GUI (Grid layout)

    1) When I run the program the content pane is blank. It will only display if I click the frame (as if I'm resizing the window).


    2) The logic of grid layout seems very basic and it reflects in the presentation of the program. Is there a way I can manipulate the size of the components in the grid? As it stands now the controls are stretched to fill the grid and it is not very functional (especially with long directory names). Is there a way I can manipulate the size of individual controls or is there another way I should be making the GUI?

    Code:
    import javax.swing.JFrame;
    import javax.swing.SwingConstants;
    import java.awt.*;
    import javax.swing.*;
    
    public class RMP {
    	
    
    	public static class GUI extends JFrame
    	{
    	
    		public static void main(String args[])
    		{
    			GUI create = new GUI();
    			
    		}
    
    
    
    		//Variables
    		private static final int Width = 500;
    		private static final int Height = 200;
    		private JTextField txtDirectory, txtRandom;
    		private JLabel lblDirectory, lblRandom;
    		private JButton btnBrowse, btnPick;
    		
    		
    		public GUI()
    		{
    			//Customize JFrame
    			setTitle("Random Movie Picker");
    			setSize(Width, Height);
    			setVisible(true);
    			setDefaultCloseOperation(EXIT_ON_CLOSE);
    			
    			//Create Pane
    			Container pane = getContentPane();
    			
    			//Set Layout
    			pane.setLayout(new GridLayout (2, 3));
    			
    			
    			//Text Fields
    			txtDirectory = new JTextField(255);
    			txtRandom = new JTextField(255);
    			
    			//Labels
    			lblDirectory = new JLabel("Directory: ", SwingConstants.LEFT);
    			lblRandom = new JLabel("Random Movie: ", SwingConstants.LEFT);
    			
    			
    			//Buttons
    			btnBrowse = new JButton("Browse...");
    			btnPick = new JButton("Pick My Movie!");
    			
    			//Add components to Content Pane
    			pane.add(lblDirectory);
    			pane.add(txtDirectory);
    			pane.add(btnBrowse);
    			pane.add(lblRandom);
    			pane.add(txtRandom);
    			pane.add(btnPick);
    			
    			
    			
    		
    			
    			
    		}
    		
    	}
    
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    1. That is a repaint issue. Make sure you issue a repaint after updating the affected component. Your code here does not demonstrate what happens when a button is triggered.
    2. No (assuming you mean individually). The gridlayout is designed to create equal size divisions. If you want to use different sizes, check into using the gridbaglayout. Its more complicated to use, but allows staggered components.

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    1. That is a repaint issue. Make sure you issue a repaint after updating the affected component. Your code here does not demonstrate what happens when a button is triggered.
    2. No (assuming you mean individually). The gridlayout is designed to create equal size divisions. If you want to use different sizes, check into using the gridbaglayout. Its more complicated to use, but allows staggered components.


    1) I'm focusing on the GUI at this point. Once that is situated I will begin working on the event handlers.

    2) My biggest problem is that the text boxes aren't the correct size/shape. I want the text box to be 1 line and long enough to hold about 50-100 characters. The buttons are a bit too big for my liking but they'll still be functional so it's not that big of a deal. Knowing that i'm a beginner, do you recommend I redo the code to implement a GridBagLayout?

  • #4
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logik22 View Post
    1) I'm focusing on the GUI at this point. Once that is situated I will begin working on the event handlers.

    2) My biggest problem is that the text boxes aren't the correct size/shape. I want the text box to be 1 line and long enough to hold about 50-100 characters. The buttons are a bit too big for my liking but they'll still be functional so it's not that big of a deal. Knowing that i'm a beginner, do you recommend I redo the code to implement a GridBagLayout?
    Quote Originally Posted by Fou-Lu View Post
    1. That is a repaint issue. Make sure you issue a repaint after updating the affected component. Your code here does not demonstrate what happens when a button is triggered.
    2. No (assuming you mean individually). The gridlayout is designed to create equal size divisions. If you want to use different sizes, check into using the gridbaglayout. Its more complicated to use, but allows staggered components.

    1) I put 'setVisible()' and that resolved my first issue of the components not showing up. Thanks!

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Oh I gotcha. Try adding a pack() after adding your other components, or move the setvisible after the adding of the components. Both should work; I'm thinking the pack will end up larger on this though since it will try to accommodate the full length of the TextArea's, which needs to spread to the size of all buttons. SetVisible will fudge the text areas. I assumed you mean that the app launches with no painted components right?

    As for the change, yeah I do. There are several you can look into though (and I think Java 6 or 7 have added some more too?). Look here for some overviews of the different ones available, and decide which you think best suits your needs: http://docs.oracle.com/javase/tutori...out/index.html

  • #6
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Oh I gotcha. Try adding a pack() after adding your other components, or move the setvisible after the adding of the components. Both should work; I'm thinking the pack will end up larger on this though since it will try to accommodate the full length of the TextArea's, which needs to spread to the size of all buttons. SetVisible will fudge the text areas. I assumed you mean that the app launches with no painted components right?

    As for the change, yeah I do. There are several you can look into though (and I think Java 6 or 7 have added some more too?). Look here for some overviews of the different ones available, and decide which you think best suits your needs: http://docs.oracle.com/javase/tutori...out/index.html


    What do you mean by 'SetVisible will fudge the text areas.'? You are correct in assuming the app launches without any blank canvas. Moving SetVisible() to the end of the GUI section helped.


    Thank you for the resource describing the different layout managers. This section was especially helpful:
    http://docs.oracle.com/javase/tutori...ut/visual.html

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by Logik22 View Post
    What do you mean by 'SetVisible will fudge the text areas.'? You are correct in assuming the app launches without any blank canvas. Moving SetVisible() to the end of the GUI section helped.


    Thank you for the resource describing the different layout managers. This section was especially helpful:
    http://docs.oracle.com/javase/tutori...ut/visual.html
    You've specified a size for the frame itself, but the textarea's are set for 255 characters. Just using setvisible should take the size of the frame itself, so the GridLayout should resize to match the frame size and not that of the components. Using a pack() will try to resize based on component size, so with a 255 character length it will resize each grid column to 'fit' a 255 character input within them (even though its a button and label).

    The overview is great for sure, gives you an idea of what best suits your purpose. SpringForm may be more along the lines you want, which is nice since its easier than GridBag if you can use it.


  •  

    Posting Permissions

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