Nimila Hiranya
Nimila Hiranya

Reputation: 5192

Adding a Class extended with JPanel to an existing JPanel

I created a Class with JPanel extended to it. Then when I created an object of it and added to an existing JPanel, it doesn't show up. What am I doing wrong here?

public class StartTower extends JPanel{
private int value;

public StartTower(int value){
    this.value = value;
}

public static StartTower send(int value){
    return new StartTower(value);  
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(752, 359);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.fillRect(120, 60, 10, 270);
    g.fillRect(20, 330, 210, 10);

    g.fillRect(375, 60, 10, 270);
    g.fillRect(275, 330, 210, 10);

    g.fillRect(630, 60, 10, 270);
    g.fillRect(530, 330, 210, 10);

    int val1 = 20;
    int val2 = 315;
    int val3 = 210;
    int col = 100;
    for (int i = 0; i < value; i++) {
        g.setColor(new Color(100, col, 100));
        g.fillRect(val1, val2, val3, 15);
        System.out.println(val1 + " " + val2 + " " + val3 + " " + col);
        val1 += 10;
        val2 -= 15;
        val3 -= 20;
        col -= 10;
    }
}

}

Class with JPanel extended

private void startBtActionPerformed(java.awt.event.ActionEvent evt) {                                        
    //mainPanel.repaint();
    name = javax.swing.JOptionPane.showInputDialog(rootPane, "Enter you name!", "Ready?", WIDTH);
    if (name != null) {
        clockFunc(true);
        game.initialDisk((int) disksSpinner.getValue());

        StartTower startTower = new StartTower((int) disksSpinner.getValue());
        mainPanel.setLayout(null);
        this.add(startTower);

        a2bBt.setEnabled(true);
        a2cBt.setEnabled(true);
        b2aBt.setEnabled(true);
        b2cBt.setEnabled(true);
        c2aBt.setEnabled(true);
        c2bBt.setEnabled(true);

        optimumLabel.setText(getNoOfOptimumMoves((int) disksSpinner.getValue()));
        disksSpinner.setEnabled(false);
        startBt.setEnabled(false);
    }
}

Method where I made the object of the before said class and added it to the existing JPanel. (mainPanel is the existing JPanel created using drag drop in NetBeans).

Why isn't this showing up? What's wrong in the code? Thanks in advance!

Upvotes: 0

Views: 87

Answers (2)

Spiff
Spiff

Reputation: 4104

I'm not sure what's your complete code, but I just tried this and it works fine:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class StartTower extends JPanel
{
    private int value;

    public StartTower(int value)
    {
        this.value = value;
    }

    public static StartTower send(int value)
    {
        return new StartTower(value);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(752, 359);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.fillRect(120, 60, 10, 270);
        g.fillRect(20, 330, 210, 10);

        g.fillRect(375, 60, 10, 270);
        g.fillRect(275, 330, 210, 10);

        g.fillRect(630, 60, 10, 270);
        g.fillRect(530, 330, 210, 10);

        int val1 = 20;
        int val2 = 315;
        int val3 = 210;
        int col = 100;
        for (int i = 0; i < value; i++)
        {
            g.setColor(new Color(100, col, 100));
            g.fillRect(val1, val2, val3, 15);
            System.out.println(val1 + " " + val2 + " " + val3 + " " + col);
            val1 += 10;
            val2 -= 15;
            val3 -= 20;
            col -= 10;
        }
    }

    private static void createAndShowGUI()
    {
        StartTower startTower = new StartTower(5);

        JFrame jFrame = new JFrame();

        JPanel jPanel = new JPanel();
        jPanel.add(startTower);

        jFrame.getContentPane().add(jPanel);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

Upvotes: 1

arcy
arcy

Reputation: 13133

If you are going to use null layout, which I don't recommend, then before you add a component you have to set its bounds so the null layout knows where to put it.

Doing without a layout

Upvotes: 0

Related Questions