4 Replies - 1029 Views - Last Post: 12 December 2011 - 04:53 PM Rate Topic: -----

#1 Logik22  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 164
  • Joined: 12-December 11

'Random Movie Picker' - GUI - Control placement

Posted 12 December 2011 - 12:04 PM

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?

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);
			
			
			
		
			
			
		}
		
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: 'Random Movie Picker' - GUI - Control placement

#2 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: 'Random Movie Picker' - GUI - Control placement

Posted 12 December 2011 - 12:23 PM

You want to move the setVisible() method to the part of the code AFTER you add everything into the frame.

Also, if you are new to Java, why did you jump straight into Swing? You should start by building console applications. If you are not going to do anything to the GUI object you created in the main method, just use
new GUI(); instead of GUI test = new GUI();

Was This Post Helpful? 0
  • +
  • -

#3 Logik22  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 164
  • Joined: 12-December 11

Re: 'Random Movie Picker' - GUI - Control placement

Posted 12 December 2011 - 02:43 PM

View PostThe_Programmer-, on 12 December 2011 - 12:23 PM, said:

You want to move the setVisible() method to the part of the code AFTER you add everything into the frame.

Also, if you are new to Java, why did you jump straight into Swing? You should start by building console applications. If you are not going to do anything to the GUI object you created in the main method, just use
new GUI(); instead of GUI test = new GUI();



Thanks for the tip with 'setVisible()', that fixed my one issue.

The reason I skipped console applications is because I wanted to make a program that I would frequently use. I took a class on Java in school and enjoyed it so I'd like to brush up on it. I'm not completely new but I don't know enough to stand on my own two feet (obviously). In my class, I struggled with the GUI (mainly placement of components) so I definitely wanted to incorporate that into this project so I could get better.

Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#4 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: 'Random Movie Picker' - GUI - Control placement

Posted 12 December 2011 - 04:30 PM

On your other issue, I'm sorry I can not help you. I have never used GridLayout before. Wait until pbl gets on here, he is the master of Swing(I think?).
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: 'Random Movie Picker' - GUI - Control placement

Posted 12 December 2011 - 04:53 PM

With gridlayout they all get equal amount of space (they all share the space equally).

Try and mix some of the layout managers :) Make some JPanels with different layout managers.
Try fooling around with:
FlowLayout, BorderLayout, Gridlayout, and BoxLayout (A little more advanced).

I believe you can solve it using only GridLayout and BorderLayout if you want to. I know that pbl has used BoxLayout to make a layout looking like yours before, so that is also a possibility.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1