简体   繁体   中英

How can I place a JTable inside a JScrollPane?

Here is my code:

    String col[] = {"No","Name","Capacity"};
    final DefaultTableModel tablemodel = new DefaultTableModel(col,0);      

    Container contentPane = this.getContentPane();
    JTable table = new JTable(tablemodel);
    table.setBounds(414, 39, 294, 281);
    contentPane.add(table);
    contentPane.add(new JScrollPane(table));

    JButton btnCreateTheMovie = new JButton("Create the movie theater");        
    btnCreateTheMovie.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            movietheater.addMovieTheater(addtheaterID.getText(), Integer.parseInt(addtheater_capacity.getText()));
            try {
                write_movie_theater = new PrintWriter(new FileOutputStream(new File("d:/movietheater.txt"),true));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            };  

                int x = movietheater.movietheater_array.size()-1;
                i++;
                String ID = movietheater.movietheater_array.get(x).getID();
                int capacity = movietheater.movietheater_array.get(x).getCapacity();

                Object[] data = {i,ID,capacity};
                tablemodel.addRow(data);                    
        }
    });
    btnCreateTheMovie.setBounds(75, 192, 166, 23);
    contentPane.add(btnCreateTheMovie,BorderLayout.NORTH);

I ran this code, but the JTtable is not visible. How can I add JScrollpane to JList? How can i see the jtable headers?

How can i fix it?

JFrame by default uses BorderLayout that adds only single component in each segment (north, south, center, west, east). Please have a look at How to Use BorderLayout .

Remember:

  • Don't use setBounds() method just hand over it to Layout Manager to set the size and position of the components that what it is made for.
  • Use ActionListener instead of MouseListener for JButton
  • Avoid null layout at all that uses absolute positioning and you must specify the size and position of every component within that container. One drawback of absolute positioning is that it does not adjust well when the top-level container is resized. It also does not adjust well to differences between users and systems, such as different font sizes and locales.

Try it again after making some changes:

  • no need to add the JTable in the contentPane because it's already added in JScrollPane
  • simply add the JScrollPane in the center of the JFrame
  • add the JButton either in north or south as per your need.

Sample code:

String col[] = { "No", "Name", "Capacity" };
DefaultTableModel tablemodel = new DefaultTableModel(col, 0);

Container contentPane = this.getContentPane();
JTable table = new JTable(tablemodel);
contentPane.add(new JScrollPane(table));

JButton btnCreateTheMovie = new JButton("Create the movie theater");
contentPane.add(btnCreateTheMovie, BorderLayout.NORTH);

snapshot:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM