试图显示JTable

时间:2015-07-10 10:58:07

标签: java swing jtable layout-manager gridbaglayout

使用GridbagLayout,我试图显示一个JTable。这是我的代码(简化)。单击按钮后会显示我输入的jpanel代码。

我的小组:

   optionspane = new JPanel();
        optionspane.setLayout(new GridBagLayout());
        optionspane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());

我的小组成员:

  GridBagConstraints dgbc=new GridBagConstraints();

    dgbc.gridx=0;
    dgbc.gridy=0;
    dgbc.weightx = 1.0;
    dgbc.weighty = 1.0;
    dgbc.anchor=GridBagConstraints.CENTER;
    dgbc.fill=GridBagConstraints.BOTH;
    dgbc.insets=new Insets(5,10,10,5);
    datapane.add(ServiceInterface.eighthpanel(), dgbc);

    dgbc.gridx=0;
    dgbc.gridy=1;
    gbc.anchor=GridBagConstraints.SOUTH;
    dgbc.fill=GridBagConstraints.BOTH;
    dgbc.insets=new Insets(5,0,5,5);
    datapane.add(ServiceInterface.ninethpanel(), dgbc);

以下是包含JPanel的面板方法:

  public static JPanel eighthpanel() throws IOException{
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc=new GridBagConstraints();
    pane.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    JLabel datawelcome=new JLabel("Please Enter the QoS values'");
    datawelcome.setFont(new Font("Ubuntu", Font.BOLD, 16));


    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill=GridBagConstraints.BOTH;
    gbc.fill=GridBagConstraints.REMAINDER;
    gbc.insets=new Insets(10,20,10,20);
    pane.add(datawelcome, gbc);

    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.weightx=1;
    gbc.weightx=1;
    gbc.fill=GridBagConstraints.BOTH;
    gbc.fill=GridBagConstraints.REMAINDER;
    gbc.insets=new Insets(10,20,10,20);
    //pane.add(new JScrollPane(displayJTable), gbc);
    pane.add(myscrol, gbc);

    return pane;
}

以下是我填充JTable的方法(在按钮监听器中):

                mytableclass=new Mytable();
                displayJTable = new JTable(myModel);

    displayJTable.getColumnModel().getColumn(0).setPreferredWidth(60);
    displayJTable.setPreferredScrollableViewportSize(new Dimension(525,250));
                displayJTable.setFillsViewportHeight(true);
                displayJTable.setLocation(5,5);
                myscrol.add(displayJTable);
                myscrol.getViewport().setViewPosition(new Point(0,0));
                myscrol.setPreferredSize(new Dimension(600,400));
                principalpane.setVisible(false);
                datapane.setVisible(true);
            }
        }});    

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。我添加了另一个面板(默认布局),我将scrollPane添加到button.actionlistener中的此面板。 我认为是因为gridbaglayout的静态特性。