tyweed420
asked on
My swing gui the size of buttons and textfields are stretched too big?
I'm designing a gui and i'm having trouble trying to get everything to a decent size. I'd like some rows to have a set size so buttons don't stretch to insane sizes. I'v tried setprefferedsize() and setmaximumsize on these with no luck have a look at the snap shot of my gui. see how huge those top buttons are. Please help!
http://www.southbaybeagles.com/craigslist/example.jpg
i''ll attach my code
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import com.sun.media.sound.Toolki t;
public class movieFrontGui extends JFrame implements ActionListener
{
JFrame frame;
JPanel panelGenre;
JPanel panelQuickJump;
JPanel panelMoviesArea;
JPanel panelToolbar;
JPanel bottomPanel;
List list;
JTable table;
JTextField search_textfield;
//======Buttons=========== ==
JButton button_search;
JButton button_addMovie;
JButton button_deleteMovie;
JButton button_editMovie;
JButton button_all;
JButton[] quickButtons = new JButton[26];
JButton button_a;
JButton button_b;
JButton button_c;
JButton button_d;
JButton button_e;
JButton button_f;
JButton button_g;
JButton button_h;
JButton button_i;
JButton button_j;
JButton button_k;
JButton button_l;
JButton button_m;
JButton button_n;
JButton button_o;
JButton button_p;
JButton button_q;
JButton button_r;
JButton button_s;
JButton button_t;
JButton button_u;
JButton button_v;
JButton button_w;
JButton button_x;
JButton button_y;
JButton button_z;
JMenu file_menu;
JMenuItem file_save;
JMenuItem file_saveAs;
JMenuItem file_exit;
JMenu view_menu;
JMenu help_menu;
JMenuBar menuBar;
movieFrontGui()
{
createGui();
}
void createGui()
{
frame = new JFrame("Movie Warehouse");
frame.setSize(1280,1024); //<============need to dynamicly set size!
createPanelQuickJump();
createPanelToolBar();
createMenuBar();
createpanelGenre();
creatpanelMoviesArea();
//merge movies area and genre into one JPanel
bottomPanel = new JPanel();
bottomPanel.add(panelGenre );
bottomPanel.add(panelMovie sArea);
frame.setLayout(new GridLayout(3,1));
//frame.getContentPane().a dd(menuBar );
frame.getContentPane().add (panelTool bar);
frame.getContentPane().add (panelQuic kJump);
frame.getContentPane().add (bottomPan el);
frame.setLocationRelativeT o( null ); // Center the frame on the screen.
frame.setVisible( true );
frame.pack();
this.setDefaultCloseOperat ion(EXIT_O N_CLOSE);
// Make the frame visible to the user. // Make the frame visible to the user.
/*
panelQuickJump;
panelMoviesArea;
panelToolbar;
*/
}
void createMenuBar()
{
//====File menu================
file_menu = new JMenu("File");
file_menu.add(file_save = new JMenuItem("Save"));
file_menu.add(file_saveAs = new JMenuItem("Save As"));
file_menu.add(file_exit = new JMenuItem("Exit"));
//====View Menu================
view_menu = new JMenu("View");
//=Help Menu=================
help_menu = new JMenu("Help");
menuBar = new JMenuBar();
menuBar.add(file_menu);
menuBar.add(view_menu);
menuBar.add(help_menu);
frame.setJMenuBar(menuBar) ;
}
void createPanelQuickJump()
{
panelQuickJump = new JPanel();
panelQuickJump.setLayout(n ew GridLayout(1,26));
createQuickButtons();
}
void creatpanelMoviesArea()
{
panelMoviesArea = new JPanel();
table = new JTable(20,4);
panelMoviesArea.add(table) ;
}
void createpanelGenre()
{
list = new List();
list.add("All");
list.add("Action");
list.add("Adventure");
list.add("Animation");
list.add("Comedy");
list.add("Documentary");
list.add("Drama");
list.add("Horror");
list.add("Sci-Fi");
list.add("Suspense");
list.add("Tv");
panelGenre = new JPanel();
panelGenre.add(list);
}
void createPanelToolBar()
{
panelToolbar = new JPanel();
panelToolbar.setLayout(new GridLayout(1,6));
search_textfield = new JTextField();
JLabel labelSearch = new JLabel("Search");
panelToolbar.add(labelSear ch);
panelToolbar.add(search_te xtfield);
panelToolbar.add(button_se arch);
panelToolbar.add(button_ad dMovie);
panelToolbar.add(button_de leteMovie) ;
panelToolbar.add(button_ed itMovie);
}
void createQuickButtons()
{
button_all = new JButton("all");
button_all.addActionListen er(this);
panelQuickJump.add(button_ all);
//alphabet buttons
for(int i=0; i < 26; i++)
{
char string= 'a';
quickButtons[i] = new JButton("a");
quickButtons[i].addActionL istener(th is);
panelQuickJump.add(quickBu ttons[i]);
}
//other buttons
button_search = new JButton("Search");
button_search.addActionLis tener(this );
button_addMovie = new JButton("Add Movie");
button_addMovie.addActionL istener(th is);
button_deleteMovie = new JButton("Delete Movie");
button_deleteMovie.addActi onListener (this);
button_editMovie = new JButton("Edit Movie");
button_editMovie.addAction Listener(t his);
}
/**
Performs the specific actions once a listener is invoked
*/
public void actionPerformed(ActionEven t actionevent)
{
}
public static void main(String args[])
{
movieFrontGui dogfiler = new movieFrontGui();
}
}
http://www.southbaybeagles.com/craigslist/example.jpg
i''ll attach my code
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import com.sun.media.sound.Toolki
public class movieFrontGui extends JFrame implements ActionListener
{
JFrame frame;
JPanel panelGenre;
JPanel panelQuickJump;
JPanel panelMoviesArea;
JPanel panelToolbar;
JPanel bottomPanel;
List list;
JTable table;
JTextField search_textfield;
//======Buttons===========
JButton button_search;
JButton button_addMovie;
JButton button_deleteMovie;
JButton button_editMovie;
JButton button_all;
JButton[] quickButtons = new JButton[26];
JButton button_a;
JButton button_b;
JButton button_c;
JButton button_d;
JButton button_e;
JButton button_f;
JButton button_g;
JButton button_h;
JButton button_i;
JButton button_j;
JButton button_k;
JButton button_l;
JButton button_m;
JButton button_n;
JButton button_o;
JButton button_p;
JButton button_q;
JButton button_r;
JButton button_s;
JButton button_t;
JButton button_u;
JButton button_v;
JButton button_w;
JButton button_x;
JButton button_y;
JButton button_z;
JMenu file_menu;
JMenuItem file_save;
JMenuItem file_saveAs;
JMenuItem file_exit;
JMenu view_menu;
JMenu help_menu;
JMenuBar menuBar;
movieFrontGui()
{
createGui();
}
void createGui()
{
frame = new JFrame("Movie Warehouse");
frame.setSize(1280,1024); //<============need to dynamicly set size!
createPanelQuickJump();
createPanelToolBar();
createMenuBar();
createpanelGenre();
creatpanelMoviesArea();
//merge movies area and genre into one JPanel
bottomPanel = new JPanel();
bottomPanel.add(panelGenre
bottomPanel.add(panelMovie
frame.setLayout(new GridLayout(3,1));
//frame.getContentPane().a
frame.getContentPane().add
frame.getContentPane().add
frame.getContentPane().add
frame.setLocationRelativeT
frame.setVisible( true );
frame.pack();
this.setDefaultCloseOperat
// Make the frame visible to the user. // Make the frame visible to the user.
/*
panelQuickJump;
panelMoviesArea;
panelToolbar;
*/
}
void createMenuBar()
{
//====File menu================
file_menu = new JMenu("File");
file_menu.add(file_save = new JMenuItem("Save"));
file_menu.add(file_saveAs = new JMenuItem("Save As"));
file_menu.add(file_exit = new JMenuItem("Exit"));
//====View Menu================
view_menu = new JMenu("View");
//=Help Menu=================
help_menu = new JMenu("Help");
menuBar = new JMenuBar();
menuBar.add(file_menu);
menuBar.add(view_menu);
menuBar.add(help_menu);
frame.setJMenuBar(menuBar)
}
void createPanelQuickJump()
{
panelQuickJump = new JPanel();
panelQuickJump.setLayout(n
createQuickButtons();
}
void creatpanelMoviesArea()
{
panelMoviesArea = new JPanel();
table = new JTable(20,4);
panelMoviesArea.add(table)
}
void createpanelGenre()
{
list = new List();
list.add("All");
list.add("Action");
list.add("Adventure");
list.add("Animation");
list.add("Comedy");
list.add("Documentary");
list.add("Drama");
list.add("Horror");
list.add("Sci-Fi");
list.add("Suspense");
list.add("Tv");
panelGenre = new JPanel();
panelGenre.add(list);
}
void createPanelToolBar()
{
panelToolbar = new JPanel();
panelToolbar.setLayout(new
search_textfield = new JTextField();
JLabel labelSearch = new JLabel("Search");
panelToolbar.add(labelSear
panelToolbar.add(search_te
panelToolbar.add(button_se
panelToolbar.add(button_ad
panelToolbar.add(button_de
panelToolbar.add(button_ed
}
void createQuickButtons()
{
button_all = new JButton("all");
button_all.addActionListen
panelQuickJump.add(button_
//alphabet buttons
for(int i=0; i < 26; i++)
{
char string= 'a';
quickButtons[i] = new JButton("a");
quickButtons[i].addActionL
panelQuickJump.add(quickBu
}
//other buttons
button_search = new JButton("Search");
button_search.addActionLis
button_addMovie = new JButton("Add Movie");
button_addMovie.addActionL
button_deleteMovie = new JButton("Delete Movie");
button_deleteMovie.addActi
button_editMovie = new JButton("Edit Movie");
button_editMovie.addAction
}
/**
Performs the specific actions once a listener is invoked
*/
public void actionPerformed(ActionEven
{
}
public static void main(String args[])
{
movieFrontGui dogfiler = new movieFrontGui();
}
}
Set a FlowLayout on each button panel
> panelQuickJump.setLayout(n ew GridLayout(1,26));
GridLayout ignores the preferred size, try commenting that line out.
GridLayout ignores the preferred size, try commenting that line out.
same with your other button panels
>>try commenting that line out.
i.e. use a FlowLayout (the default)
i.e. use a FlowLayout (the default)
I would suggest combination of BorderLayout and GridBagLayout for your UI to get a better control!
try this out,
import java.awt.*;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import com.sun.media.sound.Toolki t;
public class TestLayout extends JFrame implements ActionListener
{
JFrame frame;
JPanel panelGenre;
JPanel panelQuickJump;
JPanel panelMoviesArea;
JPanel panelToolbar;
JPanel bottomPanel;
List list;
JTable table;
JTextField search_textfield;
//======Buttons=========== ==
JButton button_search;
JButton button_addMovie;
JButton button_deleteMovie;
JButton button_editMovie;
JButton button_all;
JButton[] quickButtons = new JButton[26];
JButton button_a;
JButton button_b;
JButton button_c;
JButton button_d;
JButton button_e;
JButton button_f;
JButton button_g;
JButton button_h;
JButton button_i;
JButton button_j;
JButton button_k;
JButton button_l;
JButton button_m;
JButton button_n;
JButton button_o;
JButton button_p;
JButton button_q;
JButton button_r;
JButton button_s;
JButton button_t;
JButton button_u;
JButton button_v;
JButton button_w;
JButton button_x;
JButton button_y;
JButton button_z;
JMenu file_menu;
JMenuItem file_save;
JMenuItem file_saveAs;
JMenuItem file_exit;
JMenu view_menu;
JMenu help_menu;
JMenuBar menuBar;
TestLayout()
{
createGui();
}
void createGui()
{
frame = new JFrame("Movie Warehouse");
frame.setSize( 800, 600 ); //<============need to dynamicly set size!
createPanelQuickJump();
createPanelToolBar();
createMenuBar();
createpanelGenre();
creatpanelMoviesArea();
//merge movies area and genre into one JPanel
bottomPanel = new JPanel();
bottomPanel.add(panelGenre );
bottomPanel.add(panelMovie sArea);
// frame.getContentPane().set Layout(new GridLayout(3,1));
JPanel pnlButton = new JPanel( new GridBagLayout() ) ;
pnlButton.add( panelToolbar, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON TAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
pnlButton.add( panelQuickJump, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON TAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
// pnlButton.setPreferredSize ( new Dimension( 600, 150 ) );
//frame.getContentPane().a dd(menuBar );
frame.getContentPane().add (pnlButton , BorderLayout.NORTH);
// frame.getContentPane().add (panelQuic kJump);
frame.getContentPane().add (bottomPan el);
frame.setLocationRelativeT o( null ); // Center the frame on the screen.
frame.setVisible( true );
frame.pack();
this.setDefaultCloseOperat ion(EXIT_O N_CLOSE);
// Make the frame visible to the user. // Make the frame visible to the user.
/*
panelQuickJump;
panelMoviesArea;
panelToolbar;
*/
}
void createMenuBar()
{
//====File menu================
file_menu = new JMenu("File");
file_menu.add(file_save = new JMenuItem("Save"));
file_menu.add(file_saveAs = new JMenuItem("Save As"));
file_menu.add(file_exit = new JMenuItem("Exit"));
//====View Menu================
view_menu = new JMenu("View");
//=Help Menu=================
help_menu = new JMenu("Help");
menuBar = new JMenuBar();
menuBar.add(file_menu);
menuBar.add(view_menu);
menuBar.add(help_menu);
frame.setJMenuBar(menuBar) ;
}
void createPanelQuickJump()
{
panelQuickJump = new JPanel();
panelQuickJump.setLayout( new GridBagLayout() );
createQuickButtons();
}
void creatpanelMoviesArea()
{
panelMoviesArea = new JPanel();
table = new JTable(20,4);
panelMoviesArea.add(table) ;
}
void createpanelGenre()
{
list = new List();
list.add("All");
list.add("Action");
list.add("Adventure");
list.add("Animation");
list.add("Comedy");
list.add("Documentary");
list.add("Drama");
list.add("Horror");
list.add("Sci-Fi");
list.add("Suspense");
list.add("Tv");
panelGenre = new JPanel();
panelGenre.add(list);
}
void createPanelToolBar()
{
panelToolbar = new JPanel();
panelToolbar.setLayout( new GridBagLayout() );
search_textfield = new JTextField( 10 );
JLabel labelSearch = new JLabel("Search");
panelToolbar.add(labelSear ch, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(search_te xtfield, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_se arch, new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_ad dMovie, new GridBagConstraints( 3, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_de leteMovie, new GridBagConstraints( 4, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_ed itMovie, new GridBagConstraints( 5, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
}
void createQuickButtons()
{
button_all = new JButton("all");
button_all.addActionListen er(this);
panelQuickJump.add(button_ all, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
//alphabet buttons
for(int i=0; i < 26; i++)
{
char string= 'a';
quickButtons[i] = new JButton("a");
quickButtons[i].addActionL istener(th is);
panelQuickJump.add(quickBu ttons[i], new GridBagConstraints( GridBagConstraints.RELATIV E, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
}
//other buttons
button_search = new JButton("Search");
button_search.addActionLis tener(this );
button_addMovie = new JButton("Add Movie");
button_addMovie.addActionL istener(th is);
button_deleteMovie = new JButton("Delete Movie");
button_deleteMovie.addActi onListener (this);
button_editMovie = new JButton("Edit Movie");
button_editMovie.addAction Listener(t his);
}
/**
Performs the specific actions once a listener is invoked
*/
public void actionPerformed(ActionEven t actionevent)
{
}
public static void main(String args[])
{
TestLayout dogfiler = new TestLayout();
}
}
import java.awt.*;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import com.sun.media.sound.Toolki
public class TestLayout extends JFrame implements ActionListener
{
JFrame frame;
JPanel panelGenre;
JPanel panelQuickJump;
JPanel panelMoviesArea;
JPanel panelToolbar;
JPanel bottomPanel;
List list;
JTable table;
JTextField search_textfield;
//======Buttons===========
JButton button_search;
JButton button_addMovie;
JButton button_deleteMovie;
JButton button_editMovie;
JButton button_all;
JButton[] quickButtons = new JButton[26];
JButton button_a;
JButton button_b;
JButton button_c;
JButton button_d;
JButton button_e;
JButton button_f;
JButton button_g;
JButton button_h;
JButton button_i;
JButton button_j;
JButton button_k;
JButton button_l;
JButton button_m;
JButton button_n;
JButton button_o;
JButton button_p;
JButton button_q;
JButton button_r;
JButton button_s;
JButton button_t;
JButton button_u;
JButton button_v;
JButton button_w;
JButton button_x;
JButton button_y;
JButton button_z;
JMenu file_menu;
JMenuItem file_save;
JMenuItem file_saveAs;
JMenuItem file_exit;
JMenu view_menu;
JMenu help_menu;
JMenuBar menuBar;
TestLayout()
{
createGui();
}
void createGui()
{
frame = new JFrame("Movie Warehouse");
frame.setSize( 800, 600 ); //<============need to dynamicly set size!
createPanelQuickJump();
createPanelToolBar();
createMenuBar();
createpanelGenre();
creatpanelMoviesArea();
//merge movies area and genre into one JPanel
bottomPanel = new JPanel();
bottomPanel.add(panelGenre
bottomPanel.add(panelMovie
// frame.getContentPane().set
JPanel pnlButton = new JPanel( new GridBagLayout() ) ;
pnlButton.add( panelToolbar, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
pnlButton.add( panelQuickJump, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
// pnlButton.setPreferredSize
//frame.getContentPane().a
frame.getContentPane().add
// frame.getContentPane().add
frame.getContentPane().add
frame.setLocationRelativeT
frame.setVisible( true );
frame.pack();
this.setDefaultCloseOperat
// Make the frame visible to the user. // Make the frame visible to the user.
/*
panelQuickJump;
panelMoviesArea;
panelToolbar;
*/
}
void createMenuBar()
{
//====File menu================
file_menu = new JMenu("File");
file_menu.add(file_save = new JMenuItem("Save"));
file_menu.add(file_saveAs = new JMenuItem("Save As"));
file_menu.add(file_exit = new JMenuItem("Exit"));
//====View Menu================
view_menu = new JMenu("View");
//=Help Menu=================
help_menu = new JMenu("Help");
menuBar = new JMenuBar();
menuBar.add(file_menu);
menuBar.add(view_menu);
menuBar.add(help_menu);
frame.setJMenuBar(menuBar)
}
void createPanelQuickJump()
{
panelQuickJump = new JPanel();
panelQuickJump.setLayout( new GridBagLayout() );
createQuickButtons();
}
void creatpanelMoviesArea()
{
panelMoviesArea = new JPanel();
table = new JTable(20,4);
panelMoviesArea.add(table)
}
void createpanelGenre()
{
list = new List();
list.add("All");
list.add("Action");
list.add("Adventure");
list.add("Animation");
list.add("Comedy");
list.add("Documentary");
list.add("Drama");
list.add("Horror");
list.add("Sci-Fi");
list.add("Suspense");
list.add("Tv");
panelGenre = new JPanel();
panelGenre.add(list);
}
void createPanelToolBar()
{
panelToolbar = new JPanel();
panelToolbar.setLayout( new GridBagLayout() );
search_textfield = new JTextField( 10 );
JLabel labelSearch = new JLabel("Search");
panelToolbar.add(labelSear
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(search_te
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_se
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_ad
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_de
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_ed
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
}
void createQuickButtons()
{
button_all = new JButton("all");
button_all.addActionListen
panelQuickJump.add(button_
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
//alphabet buttons
for(int i=0; i < 26; i++)
{
char string= 'a';
quickButtons[i] = new JButton("a");
quickButtons[i].addActionL
panelQuickJump.add(quickBu
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
}
//other buttons
button_search = new JButton("Search");
button_search.addActionLis
button_addMovie = new JButton("Add Movie");
button_addMovie.addActionL
button_deleteMovie = new JButton("Delete Movie");
button_deleteMovie.addActi
button_editMovie = new JButton("Edit Movie");
button_editMovie.addAction
}
/**
Performs the specific actions once a listener is invoked
*/
public void actionPerformed(ActionEven
{
}
public static void main(String args[])
{
TestLayout dogfiler = new TestLayout();
}
}
ASKER
The flowlayout worked but what about my jlist it stays super small i'd like it long so you can see everything on the list. I eagerly await your responses. Oh i tried setpreferredsize and setminimumsize neither did anything?
use GridBagLayout to setpreferredsize and setminimumsize to work!
and see if this is what u needed,
import java.awt.*;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import javax.swing.*;
public class TestLayout extends JFrame implements ActionListener
{
JFrame frame;
JPanel panelGenre;
JPanel panelQuickJump;
JPanel panelMoviesArea;
JPanel panelToolbar;
JPanel bottomPanel;
List list;
JTable table;
JTextField search_textfield;
//======Buttons=========== ==
JButton button_search;
JButton button_addMovie;
JButton button_deleteMovie;
JButton button_editMovie;
JButton button_all;
JButton[] quickButtons = new JButton[26];
JButton button_a;
JButton button_b;
JButton button_c;
JButton button_d;
JButton button_e;
JButton button_f;
JButton button_g;
JButton button_h;
JButton button_i;
JButton button_j;
JButton button_k;
JButton button_l;
JButton button_m;
JButton button_n;
JButton button_o;
JButton button_p;
JButton button_q;
JButton button_r;
JButton button_s;
JButton button_t;
JButton button_u;
JButton button_v;
JButton button_w;
JButton button_x;
JButton button_y;
JButton button_z;
JMenu file_menu;
JMenuItem file_save;
JMenuItem file_saveAs;
JMenuItem file_exit;
JMenu view_menu;
JMenu help_menu;
JMenuBar menuBar;
TestLayout()
{
createGui();
}
void createGui()
{
frame = new JFrame("Movie Warehouse");
frame.setSize( 800, 600 ); //<============need to dynamicly set size!
createPanelQuickJump();
createPanelToolBar();
createMenuBar();
createpanelGenre();
creatpanelMoviesArea();
//merge movies area and genre into one JPanel
bottomPanel = new JPanel();
// bottomPanel.add(panelGenre );
bottomPanel.add(panelMovie sArea);
// frame.getContentPane().set Layout(new GridLayout(3,1));
JPanel pnlButton = new JPanel( new GridBagLayout() ) ;
pnlButton.add( panelToolbar, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON TAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
pnlButton.add( panelQuickJump, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON TAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
// pnlButton.setPreferredSize ( new Dimension( 600, 150 ) );
//frame.getContentPane().a dd(menuBar );
frame.getContentPane().add (pnlButton , BorderLayout.NORTH);
// frame.getContentPane().add (panelQuic kJump);
frame.getContentPane().add ( new JScrollPane( table, JScrollPane.VERTICAL_SCROL LBAR_AS_NE EDED,
JScrollPane.HORIZONTAL_SCR OLLBAR_AS_ NEEDED ) );
frame.getContentPane().add ( new JScrollPane( list, JScrollPane.VERTICAL_SCROL LBAR_AS_NE EDED,
JScrollPane.HORIZONTAL_SCR OLLBAR_AS_ NEEDED ), BorderLayout.WEST );
frame.setLocationRelativeT o( null ); // Center the frame on the screen.
frame.setVisible( true );
frame.pack();
this.setDefaultCloseOperat ion(EXIT_O N_CLOSE);
// Make the frame visible to the user. // Make the frame visible to the user.
/*
panelQuickJump;
panelMoviesArea;
panelToolbar;
*/
}
void createMenuBar()
{
//====File menu================
file_menu = new JMenu("File");
file_menu.add(file_save = new JMenuItem("Save"));
file_menu.add(file_saveAs = new JMenuItem("Save As"));
file_menu.add(file_exit = new JMenuItem("Exit"));
//====View Menu================
view_menu = new JMenu("View");
//=Help Menu=================
help_menu = new JMenu("Help");
menuBar = new JMenuBar();
menuBar.add(file_menu);
menuBar.add(view_menu);
menuBar.add(help_menu);
frame.setJMenuBar(menuBar) ;
}
void createPanelQuickJump()
{
panelQuickJump = new JPanel();
panelQuickJump.setLayout( new GridBagLayout() );
createQuickButtons();
}
void creatpanelMoviesArea()
{
panelMoviesArea = new JPanel();
table = new JTable(20,4);
panelMoviesArea.add(table) ;
}
void createpanelGenre()
{
list = new List();
list.add("All");
list.add("Action");
list.add("Adventure");
list.add("Animation");
list.add("Comedy");
list.add("Documentary");
list.add("Drama");
list.add("Horror");
list.add("Sci-Fi");
list.add("Suspense");
list.add("Tv");
panelGenre = new JPanel();
panelGenre.add(list);
}
void createPanelToolBar()
{
panelToolbar = new JPanel();
panelToolbar.setLayout( new GridBagLayout() );
search_textfield = new JTextField( 10 );
JLabel labelSearch = new JLabel("Search");
panelToolbar.add(labelSear ch, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(search_te xtfield, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_se arch, new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_ad dMovie, new GridBagConstraints( 3, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_de leteMovie, new GridBagConstraints( 4, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_ed itMovie, new GridBagConstraints( 5, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
}
void createQuickButtons()
{
button_all = new JButton("all");
button_all.addActionListen er(this);
panelQuickJump.add(button_ all, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
//alphabet buttons
for(int i=0; i < 26; i++)
{
char string= 'a';
quickButtons[i] = new JButton("a");
quickButtons[i].addActionL istener(th is);
panelQuickJump.add(quickBu ttons[i], new GridBagConstraints( GridBagConstraints.RELATIV E, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
}
//other buttons
button_search = new JButton("Search");
button_search.addActionLis tener(this );
button_addMovie = new JButton("Add Movie");
button_addMovie.addActionL istener(th is);
button_deleteMovie = new JButton("Delete Movie");
button_deleteMovie.addActi onListener (this);
button_editMovie = new JButton("Edit Movie");
button_editMovie.addAction Listener(t his);
}
/**
Performs the specific actions once a listener is invoked
*/
public void actionPerformed(ActionEven t actionevent)
{
}
public static void main(String args[])
{
TestLayout dogfiler = new TestLayout();
}
}
import java.awt.*;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import javax.swing.*;
public class TestLayout extends JFrame implements ActionListener
{
JFrame frame;
JPanel panelGenre;
JPanel panelQuickJump;
JPanel panelMoviesArea;
JPanel panelToolbar;
JPanel bottomPanel;
List list;
JTable table;
JTextField search_textfield;
//======Buttons===========
JButton button_search;
JButton button_addMovie;
JButton button_deleteMovie;
JButton button_editMovie;
JButton button_all;
JButton[] quickButtons = new JButton[26];
JButton button_a;
JButton button_b;
JButton button_c;
JButton button_d;
JButton button_e;
JButton button_f;
JButton button_g;
JButton button_h;
JButton button_i;
JButton button_j;
JButton button_k;
JButton button_l;
JButton button_m;
JButton button_n;
JButton button_o;
JButton button_p;
JButton button_q;
JButton button_r;
JButton button_s;
JButton button_t;
JButton button_u;
JButton button_v;
JButton button_w;
JButton button_x;
JButton button_y;
JButton button_z;
JMenu file_menu;
JMenuItem file_save;
JMenuItem file_saveAs;
JMenuItem file_exit;
JMenu view_menu;
JMenu help_menu;
JMenuBar menuBar;
TestLayout()
{
createGui();
}
void createGui()
{
frame = new JFrame("Movie Warehouse");
frame.setSize( 800, 600 ); //<============need to dynamicly set size!
createPanelQuickJump();
createPanelToolBar();
createMenuBar();
createpanelGenre();
creatpanelMoviesArea();
//merge movies area and genre into one JPanel
bottomPanel = new JPanel();
// bottomPanel.add(panelGenre
bottomPanel.add(panelMovie
// frame.getContentPane().set
JPanel pnlButton = new JPanel( new GridBagLayout() ) ;
pnlButton.add( panelToolbar, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
pnlButton.add( panelQuickJump, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZON
// pnlButton.setPreferredSize
//frame.getContentPane().a
frame.getContentPane().add
// frame.getContentPane().add
frame.getContentPane().add
JScrollPane.HORIZONTAL_SCR
frame.getContentPane().add
JScrollPane.HORIZONTAL_SCR
frame.setLocationRelativeT
frame.setVisible( true );
frame.pack();
this.setDefaultCloseOperat
// Make the frame visible to the user. // Make the frame visible to the user.
/*
panelQuickJump;
panelMoviesArea;
panelToolbar;
*/
}
void createMenuBar()
{
//====File menu================
file_menu = new JMenu("File");
file_menu.add(file_save = new JMenuItem("Save"));
file_menu.add(file_saveAs = new JMenuItem("Save As"));
file_menu.add(file_exit = new JMenuItem("Exit"));
//====View Menu================
view_menu = new JMenu("View");
//=Help Menu=================
help_menu = new JMenu("Help");
menuBar = new JMenuBar();
menuBar.add(file_menu);
menuBar.add(view_menu);
menuBar.add(help_menu);
frame.setJMenuBar(menuBar)
}
void createPanelQuickJump()
{
panelQuickJump = new JPanel();
panelQuickJump.setLayout( new GridBagLayout() );
createQuickButtons();
}
void creatpanelMoviesArea()
{
panelMoviesArea = new JPanel();
table = new JTable(20,4);
panelMoviesArea.add(table)
}
void createpanelGenre()
{
list = new List();
list.add("All");
list.add("Action");
list.add("Adventure");
list.add("Animation");
list.add("Comedy");
list.add("Documentary");
list.add("Drama");
list.add("Horror");
list.add("Sci-Fi");
list.add("Suspense");
list.add("Tv");
panelGenre = new JPanel();
panelGenre.add(list);
}
void createPanelToolBar()
{
panelToolbar = new JPanel();
panelToolbar.setLayout( new GridBagLayout() );
search_textfield = new JTextField( 10 );
JLabel labelSearch = new JLabel("Search");
panelToolbar.add(labelSear
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(search_te
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_se
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_ad
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_de
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
panelToolbar.add(button_ed
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
}
void createQuickButtons()
{
button_all = new JButton("all");
button_all.addActionListen
panelQuickJump.add(button_
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
//alphabet buttons
for(int i=0; i < 26; i++)
{
char string= 'a';
quickButtons[i] = new JButton("a");
quickButtons[i].addActionL
panelQuickJump.add(quickBu
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ));
}
//other buttons
button_search = new JButton("Search");
button_search.addActionLis
button_addMovie = new JButton("Add Movie");
button_addMovie.addActionL
button_deleteMovie = new JButton("Delete Movie");
button_deleteMovie.addActi
button_editMovie = new JButton("Edit Movie");
button_editMovie.addAction
}
/**
Performs the specific actions once a listener is invoked
*/
public void actionPerformed(ActionEven
{
}
public static void main(String args[])
{
TestLayout dogfiler = new TestLayout();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for the code and actually laying it out!
looks great!
looks great!