tags:

views:

66

answers:

2

I've created a custom swing component. I can see it (the grid from the paint method is drawn), but the buttons that are added (verified by println) aren't shown. What am I doing wrong?

Background information: I'm trying to build a tree of visible objects like the Flash/AS3 display list.

public class MapPanel extends JComponent { // or extends JPanel, same effect

    private static final long serialVersionUID = 4844990579260312742L;

    public MapPanel(ShapeMap map) {
        setBackground(Color.LIGHT_GRAY);
        setPreferredSize(new Dimension(1000,1000));
        setLayout(null);
        for (Layer l : map.getLayers()) {
//          LayerView layerView = new LayerView(l);
//          add(layerView);
            System.out.println(l);
            JButton test = new JButton(l.getName());
            add(test);
            validate();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {

        // necessary?
        super.paintComponent(g);

        // background
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());

        // grid
        g.setColor(Color.GRAY);         
        for (double x = 0; x < getWidth(); x += 10) {
            g.drawLine((int)x, 0, (int)x, getHeight());
        }
        for (double y = 0; y < getHeight(); y += 10) {
            g.drawLine(0, (int)y, getWidth(), (int)y);
        }

    }

}
+1  A: 

It would be easier for us to diagnose your problem if you gave us a SSCCE. As it stands, we may not have enough information to fix your problem.

I can see it (the grid from the paint method is drawn),

I don't know what that means, there is no paint() method in the posted code. (But I suppose it is easy enough to assume that you meant paintComponent(g))

However, it looks like the problem is that you are uisng a "null layout". The children will not paint unless you manually set the size and location of the children.

You should probably read a quick tutorial on LayoutManagers. It may make things easier for you when drawing components.

camickr
The "null" layout is probably the issue, but for the rest, the given code and explanation is in my opinion enough to diagnose, even without running it. I will +1 if you ease a bit the last part which can sound a bit condescending. Also, complaining about the quality of question should remain in comments, I think.
Gnoupi
@Gnoupi Agreed! (with all of it)
jjnguy
When I posted there where 2 other responses which have since been deleted because they where off topic. This tells me the question may not have been as complete as you think. By definition when asking a question the OP doesn't know where the problem is and therefore doesn't know if all the necessary information has been included. That is why a SSCCE should be posted with every question. Yes, sometimes we can guess correctly, However, in general, people need to spend more time making sure the question is clear and concise so we don't spend time guessing and assuming.
camickr
Too many people in this forum are worried about their reputation points and are therefore afraid to say anything to offend the OP for fear that they won't get an upvote. I will continue to offer advice on how to get better answers faster in the future.
camickr
Actually, I took away my answer because I was wrong about the behavior of paintComponent() in general, and my answer was stating wrong facts about it. No reason to leave such thing, so. About the SSCCE and in general advices to people, I can't agree more. But advising works better when expressed nicely, not as a critic or a "I have enough of such things". The way @jjnguy edited makes it sounds more pleasant, and more inviting.
Gnoupi
Actually I know about layoutmanagers and using none is my intention. I probably should have put this in the question earlier, but I'm building a map editor where polygons and others objects can be placed on a plane, so things need to be absolutely positioned. Thanks for trying to help anyway
Bart van Heukelom
Fine, for really helping then, have it your way. It's just that the real answer got a bit lost in the rest of your post
Bart van Heukelom
What do you mean "trying to help anyway"? My correct answer was posted 15 minutes before the other person posted. I stated "The children will not paint unless you manually set the size and location of the children". We should not have to provide a full detailed explanation of the solution. That is what tutorials are for. I can help more people daily if I don't have to fully explain every solution. If you don't understand the suggestion then you can post a follow up question.
camickr
+2  A: 

Setting null as the layout manager and then adding buttons will not have any effect. A layout manager is responsible for computing the bounds of the children components, and setting layout manager to null effectively leaves all your buttons with bounds = (0,0,0,0).

Try calling test.setBounds(10, 10, 50, 20) as a quick test to see if the buttons appear. If they do, they will be shown at exactly the same spot. From there you can either install a custom layout manager that gives each button the required bounds, or use one of the core / third party layout managers.

Kirill
Oh, I wrongly assumed the buttons would be placed at 0,0 with a sensible default size. Guess not :psetBounds made them appear. next step: replacing the button with my own components
Bart van Heukelom