tom
tom

Reputation: 179

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

I am building up a very simple contact book. I am trying to realize a table with checkboxes for each of user's contact list. I am basing on this: https://stackoverflow.com/a/7392163/6528351. I have edited a little bit that code, choosing (UserContactTablePane) to extend the JScrollPane and use it as container of the table [EDIT 2: code edited to fit a hard code version]:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

public class RubricaMain {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    RubricaGui frame = new RubricaGui();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

class RubricaGui extends JFrame implements ActionListener {

    public RubricaGui() throws IOException {
        this.setResizable(false);
        setTitle("Contact book");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setShowRubricaFrame(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }


    private void setShowRubricaFrame(JFrame main) {

        main.setBounds(250, 100, 1000, 355);

        JPanel panel = new JPanel();
        panel.setLayout(null);

        main.setContentPane(panel);
        main.setVisible(true);

        Object[] columnNames = {"", "Name", "Surname", "Telephone number", ""};
        Object[][] dataTable = {
                {"1","Harry","Kane","+44 333333",false},
                {"2","David","Bechkam","+44 444444",false},
                {"3","Steven","Gerrard","+44 555555",false}
        };

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

    }

}


class UserContactTablePane extends JScrollPane {

    private JTable table;

    public UserContactTablePane(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.

The method setShowRubricaFrame sets up the main frame inside which showing the telephone number list of a user, list that I pass as an Object[][] to UserContactTablePane.

I don't know what I am missing. The scroll pane with the table does not get created (shown).

Might I ask for your help, please? Thanks.

EDIT 1 - the scroll pane "Rubrica" should show my list, but it does not: enter image description here

Upvotes: 0

Views: 75

Answers (1)

camickr
camickr

Reputation: 324108

I didnt know that now it is mandatory to copy-paste all executable code.

If you make an effort to ask a good clear question we will make an effort to give a good answer

In any case you have NOT been asked to post all the executable code. You have been asked to post an MRE which is completely different. The point of the MRE is for you to make sure you understand the question you are asking by simplifying the code.

Your question is How to I add a JTable to a JFrame. 90% of the code you posted is completely irrelevant to that question. It does not matter where the data comes from. That is that data can be hard coded, which means the SQL logic is completely irrelevant to your stated problem.

It takes one line of code to create a JTable:

JTable table = new JTable(10, 50);

So figure out how to add that table to your frame first. Then later you worry about creating a TableModel from data in your database. Learn to walk before you run. It is easier to debug 10 lines of code that it is 100. So your first task is to simplify the problem.

With a quick glance at the code (there is too much code posted to look at it in detail) I see some problems:

  1. Using a null layout is wrong. Swing was designed to be used with layout managers.
  2. When you create your Rubrica frame you pass a reference to the previous frame, why? Once the user is logged in thee is no need to reference that frame. Instead I would pass the "user". Then you can use the user to do your SQL query.
  3. Don't extend JScrollPane!!! You are not adding any new functionality to the scrollpane. In fact this may be your main problem. You extend the scrollpane and create a JTable, but you never add the JTable to the scrollpane. Get rid of that class and just create your table and the scroll pane in the constructor of your class.
  4. Swing components should be added to the frame BEFORE the frame is made visible.

Upvotes: 3

Related Questions