FjjF
FjjF

Reputation: 87

Why the JTable is not showing?

I have this piece of code (yes, I'm a newbie) but JTable is not showing. Before creating the panels I could see it (a magenta box because I still haven't made the DataModel).

But after adding the panels, It dissapeared. Can anyone tell me why?. Thanks in advance.

public Ost() {
    super();
    JTable table;
    OstBridge bridge;
    JButton btn;
    JPanel p1=new JPanel(new FlowLayout());
    JPanel p2=new JPanel(new FlowLayout());
    Container cp=getContentPane();
    cp.setLayout(new BoxLayout(cp,BoxLayout.Y_AXIS));




    p1.setBorder(BorderFactory.createTitledBorder("Panel 1"));
    p2.setBorder(BorderFactory.createTitledBorder("Panel 2"));
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setSize(320, 240);
    this.setTitle("OST - Downloader");
    //this.setLayout(new FlowLayout());

    cp.add(p1);
    cp.add(p2);

    table=new JTable();
    table.setBackground(new Color(0xFF00FF));
    table.setSize(100, 100);
    table.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    p2.add(table);

    bridge=new OstBridge();

    btn=new JButton("Prueba");
    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println("Action event");
            System.out.println(e);
            System.out.println(bridge);
            new Ost();

        }
    });

    p1.add(btn);
    p1.setVisible(true);
    this.pack();

    this.setVisible(true);
}

Upvotes: 0

Views: 60

Answers (1)

Oliver Watkins
Oliver Watkins

Reputation: 13539

It needs data and/or a datamodel for you to see something interesting.

If you add the table inside a JScrollPane like so :

p2.add(new JScrollPane(table));

you can at least see the outline of the component.

Upvotes: 1

Related Questions