Link to home
Start Free TrialLog in
Avatar of tyweed420
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.ActionListener;

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.Toolkit;

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(panelMoviesArea);
            
            frame.setLayout(new GridLayout(3,1));
            //frame.getContentPane().add(menuBar);
            frame.getContentPane().add(panelToolbar);
            frame.getContentPane().add(panelQuickJump);
            frame.getContentPane().add(bottomPanel);
        frame.setLocationRelativeTo( null );         // Center the frame on the screen.
        frame.setVisible( true );  
        frame.pack();
        this.setDefaultCloseOperation(EXIT_ON_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 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(labelSearch);
            panelToolbar.add(search_textfield);
            panelToolbar.add(button_search);
            panelToolbar.add(button_addMovie);
            panelToolbar.add(button_deleteMovie);
            panelToolbar.add(button_editMovie);
      }
      
      void createQuickButtons()
      {
            button_all = new JButton("all");
            button_all.addActionListener(this);
            panelQuickJump.add(button_all);
            //alphabet buttons
            for(int i=0; i < 26; i++)
            {
                  char string= 'a';
                  quickButtons[i] = new JButton("a");
                  quickButtons[i].addActionListener(this);
                  panelQuickJump.add(quickButtons[i]);
            }
            
            //other buttons
            button_search = new JButton("Search");
            button_search.addActionListener(this);
          button_addMovie = new JButton("Add Movie");
          button_addMovie.addActionListener(this);
          button_deleteMovie = new JButton("Delete Movie");
          button_deleteMovie.addActionListener(this);
          button_editMovie = new JButton("Edit Movie");
          button_editMovie.addActionListener(this);
      }

      /**
    Performs the specific actions once a listener is invoked
    */
    public void actionPerformed(ActionEvent actionevent)
    {
          
    }
      
      
      
    public static void main(String args[])
    {
        movieFrontGui dogfiler = new movieFrontGui();
     
    }
      
      
      
      
      
}

            
            
            
            
            
            
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Set a FlowLayout on each button panel
>           panelQuickJump.setLayout(new GridLayout(1,26));

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 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.ActionListener;

    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.Toolkit;

    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(panelMoviesArea);

//              frame.getContentPane().setLayout(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.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
             pnlButton.add( panelQuickJump, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
                 GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
//             pnlButton.setPreferredSize( new Dimension( 600, 150 ) );
              //frame.getContentPane().add(menuBar);
              frame.getContentPane().add(pnlButton, BorderLayout.NORTH);
//              frame.getContentPane().add(panelQuickJump);
              frame.getContentPane().add(bottomPanel);
            frame.setLocationRelativeTo( null );         // Center the frame on the screen.
            frame.setVisible( true );
            frame.pack();
            this.setDefaultCloseOperation(EXIT_ON_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(labelSearch, 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_textfield, 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_search, 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_addMovie, 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_deleteMovie, 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_editMovie, 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.addActionListener(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].addActionListener(this);
                   panelQuickJump.add(quickButtons[i], new GridBagConstraints( GridBagConstraints.RELATIVE, 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.addActionListener(this);
             button_addMovie = new JButton("Add Movie");
             button_addMovie.addActionListener(this);
             button_deleteMovie = new JButton("Delete Movie");
             button_deleteMovie.addActionListener(this);
             button_editMovie = new JButton("Edit Movie");
             button_editMovie.addActionListener(this);
         }

         /**
        Performs the specific actions once a listener is invoked
        */
        public void actionPerformed(ActionEvent actionevent)
        {

        }



        public static void main(String args[])
        {
            TestLayout dogfiler = new TestLayout();

        }





    }
Avatar of tyweed420
tyweed420

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.ActionListener;

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(panelMoviesArea);

//              frame.getContentPane().setLayout(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.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
             pnlButton.add( panelQuickJump, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0,
                 GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) ) ;
//             pnlButton.setPreferredSize( new Dimension( 600, 150 ) );
              //frame.getContentPane().add(menuBar);
             frame.getContentPane().add(pnlButton, BorderLayout.NORTH);
//              frame.getContentPane().add(panelQuickJump);
              frame.getContentPane().add( new JScrollPane( table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ) );
             frame.getContentPane().add( new JScrollPane( list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ), BorderLayout.WEST );
            frame.setLocationRelativeTo( null );         // Center the frame on the screen.
            frame.setVisible( true );
            frame.pack();
            this.setDefaultCloseOperation(EXIT_ON_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(labelSearch, 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_textfield, 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_search, 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_addMovie, 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_deleteMovie, 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_editMovie, 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.addActionListener(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].addActionListener(this);
                   panelQuickJump.add(quickButtons[i], new GridBagConstraints( GridBagConstraints.RELATIVE, 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.addActionListener(this);
             button_addMovie = new JButton("Add Movie");
             button_addMovie.addActionListener(this);
             button_deleteMovie = new JButton("Delete Movie");
             button_deleteMovie.addActionListener(this);
             button_editMovie = new JButton("Edit Movie");
             button_editMovie.addActionListener(this);
         }

         /**
        Performs the specific actions once a listener is invoked
        */
        public void actionPerformed(ActionEvent actionevent)
        {

        }



        public static void main(String args[])
        {
            TestLayout dogfiler = new TestLayout();

        }





    }
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the code and actually laying it out!  

looks great!