...

View Full Version : 'Random Movie Picker' - GUI



Logik22
12-12-2011, 08:03 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);






}

}

}

Fou-Lu
12-12-2011, 08:43 PM
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.
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.

Logik22
12-12-2011, 10:27 PM
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.
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?

Logik22
12-12-2011, 10:45 PM
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?



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.
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!

Fou-Lu
12-12-2011, 10:45 PM
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/tutorial/uiswing/layout/index.html

Logik22
12-13-2011, 04:30 PM
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/tutorial/uiswing/layout/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/tutorial/uiswing/layout/visual.html

Fou-Lu
12-13-2011, 04:49 PM
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/tutorial/uiswing/layout/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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum