Search code examples
javaswingjtablejpaneljbutton

Add button on top of JTable


How to add a Back button on top of JTable ? I tried but no luck.

public class viewMovie extends JPanel{

    static JFrame frame = new JFrame("View Movie");
    JTable table;

     public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                try {
                    createAndShowGui();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            });
        }

     static void createAndShowGui() throws Exception  {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new viewMovie());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

     public viewMovie() throws Exception
     {
         String sql="Select * from movie";
            DatabaseConnection db = new DatabaseConnection();
            Connection  conn =db.getConnection();
            PreparedStatement  ps = conn.prepareStatement(sql);
             ResultSet rs = ps.executeQuery();
             ResultSetMetaData rsmt= rs.getMetaData();
             int c= rsmt.getColumnCount();
             Vector column= new Vector(c);
             for(int i=1;i<=c;i++)
             {
                 column.add(rsmt.getColumnName(i));
             }
             Vector data = new Vector();
             Vector row=new Vector();
             while(rs.next())
             {
                 row=new Vector(c);
                 for(int i=1;i<=c;i++)
                 {
                     row.add(rs.getString(i));
                 }
                 data.add(row);
             }

             JButton back= new JButton("Back");
             JPanel topPanel = new JPanel(new GridLayout(1, 0, 3, 3));
                     topPanel.add(back);

                 JPanel panel= new JPanel();
                 table=new JTable(data,column);
             JScrollPane jsp = new JScrollPane(table);
                 panel.setLayout(new BorderLayout());
                 panel.add(jsp,BorderLayout.CENTER);
                 frame.setContentPane(panel);
                 frame.setVisible(true);

     } 

}

This is the output I get.

enter image description here


Solution

  • You're forgetting one line of code, the line that adds the topPanel to the panel JPanel:

    panel.add(topPanel, BorderLayout.PAGE_START);
    

    Side note: for future questions, you will want to make your code compilable and runnable by us, meaning get rid of unnecessary dependencies, such as database. For your code above, the database stuff could be replaced by:

    JPanel panel = new JPanel();
    Integer[][] data = { { 1, 2, 3 }, { 4, 5, 6 } };
    String[] column = { "A", "B", "C" };
    
    table = new JTable(data, column);
    

    But actually since it is just a simple layout question, even the JTable is not necessary.