0

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

tom
  • 159
  • 2
  • 10
  • 1) `panel.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) *"I don't know what I am missing."* A [mre], for starters. – Andrew Thompson Nov 21 '19 at 10:33
  • I have linked an image of the frame. If it is a problem of the panel layout, shouldn't it create a frame with components positioned in casual way? Instead I put certain bounds and the view is like this. The bordered scroll pane appears but its content (the table) does not. – tom Nov 21 '19 at 10:43
  • *"I have linked an image of the frame."* .. why? – Andrew Thompson Nov 21 '19 at 10:44
  • To try to explain graphically my issue. I am not so good with english. Isn't it useful? – tom Nov 21 '19 at 10:45
  • 2
    Do not set the layout of scrollingContactsPanel to null, don't set its layout to anything. Also, extending JScrollPane for what you are doing is a bad idea. Also, what Andrew Thompson is saying is on point: pasting a few fragments of your code is not very helpful to us for helping you out. Instead, create a small program that shows the problem you have, and do it all in one class (including your main method). That allows us to copy/paste your program, quickly test it, and possible provide you with a corrected program or indications on what to do to solve you problem. – TT. Nov 21 '19 at 12:30
  • 2
    The following link explains in detail what we mean by "a small program in one class that showcases the problem": How to create a [Short, Self Contained, Correct (Compilable), Example](http://sscce.org) (an SSCCE). Once you've created such an SSCCE, [edit] your question, remove the code fragments and instead add your SSCCE. Good luck! – TT. Nov 21 '19 at 12:31
  • I didnt know that now it is mandatory to copy-paste all executable code. I guess it is a very new rule. I am providing it. I was, then, wondering why -1 for my post. I am going to provide this sscce. Thanks. – tom Nov 21 '19 at 13:17
  • 2
    It is a guideline rather than a rule. The guideline is akin to a [MCVE], though worded more explicitely. The point is the same though. For people around here to be able to help you out, it is the most practical and gives the highest chance of getting an answer or help. You're welcome. – TT. Nov 21 '19 at 13:34
  • Done. I hope that -1 may be removed. Though I have to understand yet if it is always the case of putting a minimal executable. – tom Nov 21 '19 at 15:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202808/discussion-between-tom-and-tt). – tom Nov 21 '19 at 15:06
  • *"now it is mandatory to copy-paste all executable code"* 1) *"now it is"* Now? It has been the case for over 2 years. 2) *"mandatory"* It is not mandatory. It is redundant for some types of questions, and even for questions related to code, it is not mandatory . Having said that, it is not mandatory for us to answer. Preparing & posting an MRE / SSCCE makes it easier for others to help, *encourages* us to help, so is a good idea. 3) *"all executable code"* An MRE / SSCCE is ***not*** your entire project. But a dut down version of it with all code not related to the problem removed. .. – Andrew Thompson Nov 22 '19 at 00:07
  • .. 4) Speaking of which, it should not take over 300 lines of code & DB access to show what is essentially a layout problem. Hard code some data, add the `import` statements etc. 5) *"I hope that -1 may be removed."* The question is not yet good enough to justify removing the -1. – Andrew Thompson Nov 22 '19 at 00:09
  • Hello Andrew. I had already understood almost everything :) though I would have other questions, but I don't know how to make them in private chat. I guess removing the db access from that code is not sufficient to remove -1 :) – tom Nov 22 '19 at 08:53
  • Sorry Andrew, I have just understood the meaning of hard coding. I will edit the code soon :) – tom Nov 22 '19 at 13:52
  • Hello! I hope the code is now good :) – tom Nov 25 '19 at 15:20

1 Answers1

3

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.
camickr
  • 321,443
  • 19
  • 166
  • 288
  • hello dear. I understood about MRE later, sure! "90% of the code you posted is completely irrelevant to that question" -> if the aim is to provide a SSCCE, I will have to put ALL NECESSARY code that is minimal. "That is that data can be hard coded, which means the SQL logic is completely irrelevant to your stated problem." -> sure! it is as I initially post my issue, before my last edit. About the rest of your answer, I am going to follow it, thanks :) :) – tom Nov 21 '19 at 16:03
  • I was asked to provide a sscce, if you didnt notice :) – tom Nov 21 '19 at 16:13
  • *I was asked to provide a sscce,* - you were first asked to post a `minimal reproducible example (MRE)`, then you were asked to post a `SSCCE`, then again you were asked to post an `MRE`. – camickr Nov 21 '19 at 16:28
  • Got it. I find them not so difficult to understand. Thanks, Andrew. " If there is any part of either document you don't understand, it is a good time/place to ask questions about them. Many people here can answer them " -> I guess on MetaSO (?) – tom Nov 22 '19 at 08:44
  • @tom don't forget to "accept" the answer by clicking on the checkmark so people know the problem has been solved. – camickr Nov 22 '19 at 15:54
  • @camickr dear, I havent solved yet, especially because of my work. I have yet to edit the code, putting and hard code version. If after such an edit you will get to spot what is going wrong, I will accept your answer :) :) – tom Nov 22 '19 at 16:06
  • it seems now my app works. Your hints helped me, finally :) I don't know why but when I started studying the solution I linked, that is however equal to what I coded to solve the problem, I reused that code and mine wasn't working. I would like to say that now it is working even without following your 4-th point. Do I have to post the reviewed code in a new answer? Thanks. – tom Nov 25 '19 at 15:25