1

My plan is to get a 2-dimensional array into a JTable into a JScrollPanel. The Table already shows the 2-dimensional array's data. The problem is that the table won't show when I add it to a jscrollpanel (The scrollpanel is visible though but empty. It does work when I put the table in the JPanel (not jscrollpanel). but not when using a jscrollpanel.

Does any one have any idea?

package arraytablestable;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class ArrayTablesTable extends JFrame {
public static JFrame frame;


public static void main(String[] args) {    

    frame = new JFrame();
    frame.setSize(1280,720);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Array Table 7 in Tables");
    frame.setContentPane(new Paneel());
    frame.setVisible(true);
}

private static class Paneel extends JPanel {

    // Declareren
    public static JTable Table;

    public JTableHeader tblHeader;
    public TableModel tblModel;
    public Object[] columnNames;
    public static Object[][] Data;
    public int Resultaat, a ,i ;
    public JScrollPane jScrollPane;



    public Paneel() {
    this.setLayout(new GridLayout(1,1));
    // Initialiseren
    Data = new Object[200][1];
    columnNames = new Object[]{"Product","Resultaat"};


    // Model maken & importen
    tblModel = new DefaultTableModel(columnNames,0);
    Table = new JTable(tblModel);

    // Table Eigenschaooen
//      tblHeader = Table.getTableHeader();     
//      tblHeader.setBackground(Color.decode("#a181a1"));
//      tblHeader.setForeground(Color.decode("#1b1b1b"));
//      Table.setFocusable(false);
//      Table.setRowSelectionAllowed(false);

    Data[0] = new String[100];
    Data[1] = new Integer[100];



    // For Loop
        for(i = 0; i < 100; i++){ 
        Data[0][i]= "7 x " + i;
        Resultaat = 7 * i;
        Data[1][i]= Resultaat;

        TabelVullen(new Object[]{Data[0][i],Data[1][i]});

    }

        System.out.println("object: " + Data[0][4]);

        this.add(jScrollPane);

    }
    public void TabelVullen(Object[] data){
    ((DefaultTableModel)Table.getModel()).addRow(data);

    // Components -> Panel

       jScrollPane = new JScrollPane(Table);
       jScrollPane.add(Table);


    }
}
}

1 Answer 1

3
   jScrollPane = new JScrollPane(Table);

The above line is correct. It creates the scrollpane and adds the table to the "viewport" of the scrollpane.

   //jScrollPane.add(Table);

The above line is incorrect. It removes the table from the viewport of the scrollpane. Never use the add(...) method of a scrollpane. All components must be added to the viewport.

Learn of follow Java naming conventions. Variable names should NOT start with an upper case character. "Table" should be "table". All you other names follow this standard. Be consistent!!!

Also, method names should NOT start with an upper case character (TabelVullen).

Sign up to request clarification or add additional context in comments.

1 Comment

Thx mate, I guess I looked over it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.