Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Adding JButtons to Frame

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Adding JButtons to Frame

    Hey guys, I'm pretty good with Java, except for the GUI. I've started getting myself familiar with it, and well it was going well until I came to a large brick wall. You see I added this JTable onto my frame, and got the scroll and everything working fine. I need to add about 3 buttons now, but it just isn't working out. The GridBagLayout isn't working as it should.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
     
    public class BRA extends JFrame
    {
        public static int column=0, row = 0, instances; //integers for column, row, and instance numbers
        public static  String[][] data = new String [15][4]; //array which contains the database
     
        static JTable table;
     
        private JScrollPane scroll;
        static String columnNames[] = {"Title", "Batman", "Director", "Year"};
        static JPanel panel = new JPanel ();
     
        public static void main (String [] args) throws IOException
        {
            reader();   //calls the reader method, reads data in from text file
     
            JFrame frame = new JFrame("The Batman Movie Database (BMDb)");  //frame setup
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            frame.setSize (550,600);
            frame.setLocationRelativeTo(null); // here's the part where i center the jframe on screen
            frame.setVisible(true);              
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JPanel panel = new JPanel(new GridBagLayout());                 //panel setup
            frame.getContentPane().add(panel, BorderLayout.CENTER);
     
            GridBagConstraints d = new GridBagConstraints();
     
            table = new JTable(data, columnNames); 
            //table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
            d.gridx = 0;
            d.gridy = 0;
            panel.add(table, d); 
     
            JScrollPane scrollPane = new JScrollPane (table);
            table.setPreferredScrollableViewportSize(new Dimension(500,160));
            table.setFillsViewportHeight(true);
            panel.add(scrollPane);       
     
            JButton add = new JButton ("ADD");
            d.gridx = 56;
            d.gridy = 150;
            panel.add(add, d);
        }
     
        public static void reader()throws IOException   //method to read in data from the text file
        {
            //buffered reader to allow for data to be read from the data file
            BufferedReader input = new BufferedReader (new FileReader ("V2.txt"));
            String line = input.readLine(); //string that contains each successuve line of the file
            while(line !=null)              //loop that reads a new line until it is a blank
            {
                if(line.equals("")){break;}
                data[row][column] = line;   //fills that position of the array with the line
                column++;                   //increments the column number
                if(column == 4)
                {
                    column = 0;             //resets the column number when it reaches 4
                    row++;                  //and inrements the row number
                }
                line = input.readLine();//reads another line from the file
            }
            input.close();                  //closes the buffered reader
        }
     
    }

    That's my code so far and the file used is attached as well. If you can please help me position 3 buttons in the bottom left corner under the JTable, it would be most appreciated. Thanks!
    Attached Files Attached Files


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Adding JButtons to Frame

    I ran your code and got a frame with an 'Add' button in the middle, and a tiny square of something at its top left. The tiny square might be a JTable, but it's too small to tell. I suggest you either post the correct code, or fix the current problems before trying to add more buttons.

    Incidentally, was there any particular reason you chose GridBagLayout over something simple like BorderLayout?

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding JButtons to Frame

    Quote Originally Posted by dlorde View Post
    I ran your code and got a frame with an 'Add' button in the middle, and a tiny square of something at its top left. The tiny square might be a JTable, but it's too small to tell. I suggest you either post the correct code, or fix the current problems before trying to add more buttons.

    Incidentally, was there any particular reason you chose GridBagLayout over something simple like BorderLayout?
    Yeah, that tiny square is a JTable (it you were to maximize the screen you will see it). That's what I've been having troubles with, how would you move that one button, so my JTable and buttons still fit. And I've been told that GridBayLayout gives you more versatility in moving buttons and such

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Adding JButtons to Frame

    You need to add frame.pack() after adding the last component to it, so they get laid out properly. Ideally this would be before you set the frame visible, which is usually the last thing to do.

    I find GridBagLayout much too fiddly - it's OK for GUI generators, but I prefer simplicity. If I was doing it, I'd use a BorderLayout for the frame, put the JTable in the CENTER area, and put the buttons in the SOUTH area. If you want the buttons spaced out in some way, use a panel with its own layout for the SOUTH area and put the buttons in that.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding JButtons to Frame

    Alright, that sounds pretty good, thanks for the idea. Would you mind giving me a sample code, just so I could wrap my head around it a bit more?

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Adding JButtons to Frame

    Sure, here's the BorderLayout tutorial, complete with sample code. Also, check out this thread, where I show nested BorderLayouts.

    You really should be able to find this stuff yourself; once you hear a class suggested, first look in the API docs to see if there's helpful description there, then look in the Java Tutorials to see if there's a tutorial for it. If you still can't find enough detail, Google for it.
    Last edited by dlorde; June 12th, 2011 at 11:03 AM.

Similar Threads

  1. Directing JButtons to functions?
    By scopolamine in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 11:22 AM
  2. Adding a frame/textarea to an application
    By zincc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2011, 04:43 PM
  3. Starting and Stopping a loop with JButtons
    By Endevor in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 8th, 2010, 03:37 PM
  4. help with JFrame, drawing, and JButtons
    By Khoatic in forum AWT / Java Swing
    Replies: 1
    Last Post: November 19th, 2010, 12:34 AM
  5. JTables with JButtons, I'm overlooking something
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2010, 11:30 AM