views:

38

answers:

4

I have class extended from JPanel. This class contains several other JComponents and a lot of painting. What I did and I know it isn't correct is, I overrided the paintComponent for the paintings, so far so good.

But I also set the location and the size for all subcomponents in this method, because they depend on the getWidth and getHeight from the JPanel. I know this isn't the place for the components, but where do I do this kind of stuff. At constructor time the sizes aren't set. So how do it correct? Because as long as I have the location and size method in the paintComponent method, the CPU never stops calculating something.

Edit: what i did looked like this;

public class abc extends JPanel {

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

        /** all the painting stuff */
        textfield.setLocation(
            this.getWidth() * someValue,
            this.getHeight() * someotherValue);
        // also depends on getHeight() and getWidth()
        textfield.setSize(new Dimension(getHeight(), getWidth());
    }
}    

Everything worked fine, except that I always had a CPU usage around 10%. If commeted both lines out, the CPU usage dropped to 0%. So now I used a Layoutmanager like you recommended. And still everything lookes fine, but the CPU load didn't change it still stayed by around 10%. The code, how it looks now:

public class abc extends JPanel {

    public abc() {
        GridBagLayout gblayout = new GridBagLayout();

        this.setLayout(gblayout);

        textfield = new JTextField();
        textfield.setHorizontalAlignment(JTextField.CENTER);
        textfield.setBackground(new Color(0, 0, 0, 0));
        textfield.setBorder(null);
        textfield.setText("0");

        gblayout.setConstraints(textfield, new GridBagConstraints(
            0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
            GridBagConstraints.BOTH, new Insets(4, 4, 4, 4), 1, 1));

        this.add(textfield);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        /** still the same paintings */ }
}
+1  A: 

It will really help if you show us some code and maybe explain what you are trying to do but from what I understand, you're trying to do too much in one JPanel. You should have a separate panel that overrides paintComponent() if you intend to draw on it, etc and another one (or more) to hold your other components.

It is very hard, in fact, almost impossible to resize individual components because they have to obey the layout chosen for their parent component (usually a JPanel) so you have to keep that in mind as well.

Coding District
+1  A: 

The size and position of components are set by the layout manager, After a frame is initialized, pack() is called to adjust the size of all components.

You should initially set the min, max and preferred size, and let the layout manager do its job. You can get the width and height of the component in paintComponent() to render it accordingly to its size.

Maurice Perry
@Christian: There are two related examples in your previous question on this topic. http://stackoverflow.com/questions/3646832
trashgod
A: 

thx for the answer.

what i did looked like this;

[Migrated new code to question.]

Note: propably the code does look horrorable, how did you format it so nicely in StackOverflow?

Christian
I have edited your question to show the new material, so you can delete this answer.
trashgod
A: 

@trashgod yes thanks that looks fine.

Christian
@Christian: I think the @ form only works in comments; you can delete this answer, too.
trashgod