简体   繁体   中英

Netbeans GUI builder and Jtable

I am having some difficulty with getting a JTable to appear within a JPanel. I have built the UI using the Netbeans GUI builder and then created the jtable from code and attempted to add to an existing GUI built jpanel. I can't work out what the problem is. Code for the jtable part of the code is shown below (data is made up). I suspect I am missing some step which is needed when using the GUI builder as I can get a jtable to work if I do not use the GUI builder

   private void fillOrderDetailsJtable (){

        String[] columnNames = {"Order Line No", "Product", "Quantity" };

        Object[][] data = {
           {"César Cielo", "Filho", "Brazil", "50m freestyle" },
           {"Amaury", "Leveaux", "France", "50m freestyle"},
           {"Eamon", "Sullivan", "Australia", "100m freestyle"},

        }; 
        JTable table = new JTable(data, columnNames);
        table.setFillsViewportHeight(true);

        JScrollPane tableScrollPane = new JScrollPane(table);

        tableScrollPane.setPreferredSize(new Dimension(300, 50));

        pnOrderLines.add(tableScrollPane);

        Order_OrderLine.this.add(pnOrderLines); 
         // table.setAutoCreateRowSorter(true);

        tableScrollPane.setVisible(true);
        pnOrderLines.setVisible(true);
        table.setVisible(true);          
   }

So did you place the JTable to the Form and check the code Netbeans generates before trying it from the scratch?

This is IMHO the easiest way to get a clue of constructing forms.

I would suppose that you do this and compare the generated code to your own.

There is some sample code:

jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);

jTable1.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {
        {null, null, null, null},
        {null, null, null, null},
        {null, null, null, null},
        {null, null, null, null}
      },
      new String [] {
        "Title 1", "Title 2", "Title 3", "Title 4"
      }
    ));
    jScrollPane1.setViewportView(jTable1);

    getContentPane().add(jScrollPane1);
    jScrollPane1.setBounds(158, 100, 292, 246);

    pack();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM