1

I created a JPanel and i modify it a bit. I change it's background to gradient color here is the class.

public class JGradientPanel extends JPanel {
     private static final int N = 32;
     private Color color1, color2;
        public JGradientPanel(Color color1, Color color2) {
            this.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));

            this.color1 = color1;
            this.color2 = color2;
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            //Color color1 = getBackground();
            //Color color2 = color1.darker();
            int w = getWidth();
            int h = getHeight();
            GradientPaint gp = new GradientPaint(
                0, 0, color1, 0, h, color2);
            g2d.setPaint(gp);
            g2d.fillRect(0, 0, w, h);
        }
}

Now i added few components to the panel but it didn't display anything, here is the code

public JPanel getMenu() {
        JGradientPanel menu = new JGradientPanel(Color.WHITE, Color.white.darker());
        int menuHeight = (int) ((int) getHeight() * 0.07);
        menu.setPreferredSize(new Dimension(screenWidth,menuHeight));
        menu.setLayout(new GridLayout(1,10));
        menu.setBackground(Color.LIGHT_GRAY);

        //test
        JGradientButton test = new JGradientButton("test",Color.GREEN, Color.BLUE);
        menu.add(test);

        JLabel space = new JLabel(); // first blank space on the menu
        space.setBounds(0, 0, menu.getPreferredSize().width - 50, menu.getPreferredSize().height);
        space.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.GREEN));
        menu.add(space);
        JLabel moreSpaces[] = new JLabel[6];
        buttons = new JButton[buttonLabels.length];
        for(int counter = 0; counter < moreSpaces.length + buttonLabels.length; counter ++ ) {
            if(counter < 3) {
                buttons[counter] = new JButton(buttonLabels[counter]); //menu buttons

            } else {
                moreSpaces[counter - 3] = new JLabel(); // the rest of the blank in the menu
            }
        }
        // adding components to menu panel
        for(int counter = 0; counter < moreSpaces.length + buttonLabels.length; counter ++){
            if(counter < 3) {
                buttons[counter].setFocusPainted(false);
                menu.add(buttons[counter]); 
            } else {
                menu.add(moreSpaces[counter - 3]);
            }

        }
        return menu;
    }

Did i miss something or i did it wrong? What's wrong with my code?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1708134
  • 643
  • 1
  • 9
  • 21

1 Answers1

2

EDIT

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • what exactly will i put on the overridden `getPreferredSize` method? – user1708134 Apr 11 '13 at 15:24
  • (blue) colored text contains links – mKorbel Apr 11 '13 at 15:26
  • `@Override public Dimension getPreferredSize() { // TODO Auto-generated method stub return super.getPreferredSize(); }` what's missing? i don't quite understand your pointing at, sorry i'm a bit new to this – user1708134 Apr 11 '13 at 15:31
  • return new Dimension(300, 200); (for example) instead of return super.getPreferredSize();, because return super.getPreferredSize(); returns zero Dimension – mKorbel Apr 11 '13 at 15:40
  • you got it right bro.. but now i can't use the `setPreferredSize()` if i modified the `JPanel`? or do i need to override that too so that the `getPreferredSize()` will work according to the `setPreferredSize()`? – user1708134 Apr 11 '13 at 15:57
  • this override to make 1. JPanel resizable with it container 2. all painting could be based on getWeight/getHeight instead of static number, then painting is resizable with JPanel too – mKorbel Apr 11 '13 at 16:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28042/discussion-between-user1708134-and-mkorbel) – user1708134 Apr 11 '13 at 16:10