开发者

Dynamic JPanel with objects not displaying

开发者 https://www.devze.com 2023-03-03 11:08 出处:网络
Can someone take a look at this part of my code and tell me why it won\'t return the objects inside the JPanel? It definitely goes inside the loop since I tried printing statements inside. Also this J

Can someone take a look at this part of my code and tell me why it won't return the objects inside the JPanel? It definitely goes inside the loop since I tried printing statements inside. Also this JPanel object is being put inside a TabbedPane just for clarification. Let me know if I need to explain in more detail or show more code to find a solution. Thanks.

JPanel createTipTailoringPanel(TipCalcModel model)
{

    JPanel content = new JPanel();
    int size = model.getNumOfPeople();
    content.removeAll();
    content.updateUI();
    content.setLayout(new GridLayout(0,4));

    JTextField text[] = new JTextField[size];
    JSlider slider[] = new JSlider[size];
    JLabel label[] = new JLabel[size];
    JLabel cash[] = new JLabel[size];


    if(size == 0)
    {
        return content;
    }
    else
    {
        for(int i=0; i<size; i++)
        {
            text[i] = new JTex开发者_如何转开发tField();
            slider[i] = new JSlider();
            label[i] = new JLabel("$");
            cash[i] = new JLabel();
            content.add(text[i]);
            content.add(slider[i]);
            content.add(label[i]);
            content.add(cash[i]);
        }

        return content;
    }


}

Here is my calling method and the actionlistener that I use to pass in the numberofpeople:

 TipCalcView(TipCalcModel model)
{
    setTitle("Tip Calculator");
    JTabbedPane tabbedPane = new JTabbedPane();
    getContentPane().add(tabbedPane);
    tabbedPane.addTab("Main Menu", createMainPanel());
    tabbedPane.setSelectedIndex(0);
    tabbedPane.addTab("Tip Tailoring", createTipTailoringPanel(model));
    tabbedPane.addTab("Config Panel", createConfigPanel());

}

class GuestsListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        int userInput = 0;
        try{
            userInput = m_view.getGuests();
            m_model.setNumOfPeople(userInput);
            m_view.createTipTailoringPanel(m_model);
        }
        catch(NumberFormatException nfex)
        {
            m_view.showError("Bad input: '" + userInput + "'");
        }
    }
}


I suspect that your problem is outside of the code you listed. Here's a simplified working example:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    DynamicJPanel dynamic = new DynamicJPanel();
    frame.add(dynamic.createTipTailoringPanel(3));
    frame.pack();
    frame.setVisible(true);
}

JPanel createTipTailoringPanel(int size) {
    JPanel content = new JPanel();
    content.setLayout(new GridLayout(0, 4));

    for (int i = 0; i < size; i++) {
        content.add(new JTextField());
        content.add(new JSlider());
        content.add(new JLabel("$"));
        content.add(new JLabel());
    }

    return content;
}


First, since you don't use the arrays anywhere, it could be shortened to:

JPanel createTipTailoringPanel(TipCalcModel model)
{

    JPanel content = new JPanel();
    int size = model.getNumOfPeople();
    content.setLayout(new GridLayout(0,4));

    if(size == 0)
    {
        return content;
    }
    else
    {
        for(int i=0; i<size; i++)
        {
            content.add(new JTextField());
            content.add(new JSlider());
            content.add(new JLabel("$"));
            content.add(new JLabel());
        }
        return content;
    }
}

Second, seems like you add an empty components to the panel, maybe that's what you actually get?

Third, add you need to add the content panel to the JFrame (or other container) once it returns from the method above.

0

精彩评论

暂无评论...
验证码 换一张
取 消