jeudi 21 novembre 2019

Cannot build, or show, a Swing JTable with checkboxes inside a JScrollPane

I am trying to realize a table with checkboxes. I am basing on this: https://stackoverflow.com/a/7392163/6528351. I have edited a little bit that code, choosing to extend the JScrollPane and use it as container of the table:

public class UserContactTable extends JScrollPane {

    private JTable table;

    public UserContactTable(Object[] columnNames, Object[][] data) {

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) {
            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return String.class;
                    case 1:
                        return String.class;
                    case 2:
                        return String.class;
                    case 3:
                        return String.class;
                    default:
                        return Boolean.class;
                }
            }
        };

        table.setPreferredScrollableViewportSize(table.getPreferredSize());
    }
}

I am not managing to create and show it.

I have a method that sets up a frame inside which showing the telephone number list of a user, list that I pass to the class above as an Object[][]. Inside this frame is a JPanel that I want it to contain the scroll panel containing the table:

private void setShowRubricaFrame(JFrame main) {

    JFrame frame = new JFrame("Rubrica di");
    frame.setResizable(false);
    frame.setBounds(250, 100, 1000, 355);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setLayout(null);
    frame.setContentPane(panel);
    frame.setVisible(true);

    /* in the middle other components and jdbc operations that retrieve my contact list: 
       I instantiate Object[][] from a ResultSet */

    JScrollPane scrollingContactsPanel = new UserContactTable(columnNames, dataTable);
    scrollingContactsPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"),         
                                                         "Rubrica", TitledBorder.LEADING,  
                                                       TitledBorder.TOP, null, new Color(0, 0, 0)));
    scrollingContactsPanel.setBounds(10, 50, 975, 260);
    scrollingContactsPanel.setLayout(null);
    panel.add(scrollingContactsPanel);

}

I don't know what I am missing. The scroll pane with the table does not get created (shown). I even ran the code of the post I am basing on for my app. It works, of course!

I have tried many posts here but it seems that for each of them I always am missing even a little trigger that might fire the behavior I want to.

Might I ask for your help, please? Thanks.




Aucun commentaire:

Enregistrer un commentaire