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!