This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I am new to Swing and have been trying for 2 days to get a simple interface to work. It is supposed to have a drawing space on top and a button below which (for the moment) causes an orange square to appear in the drawing space. To make the drawing panel, I made a subclass of JPanel and overrode the paintComponent method, as suggested in Head First Java. Right now I have 2 problems.
1. the background color (supposed to be red) on the drawing panel never appears
2. when I hit the draw button, a copy of the button gets drawn in the upper left hand corner of the applet window.
I have also tried using a BoxLayout, but I can't get it to reserve room for the drawing panel.
1 — background color doesn't show up. When you override the 'paintComponent' method you take over the reins from the swing team and must make arrangements to paint all of the space in your component (the JPanel). An easy way to do this is to call 'super' first thing
This tells the JPanel to paint it's default background. The red will now be painted onto the JPanel before you begin your custom painting. Another option is to leave out the calls to 'setBackground' in the constructor and 'super' in 'paintComponent' and do it yourself:
2 — The JButton that shows up in the upper left corner is a result of not painting the background. This is generally referred to as 'artifacts'.
3 — about the BoxLayout. This concerns layout managers and how swing puts things together. In your code you added 'drawPanel' to the 'center' section of a BorderLayout which expands the component in width and height to fill the available space. With this there is no need to be concerned about providing size information for the child component (DrawPanel). The 'setSize' method is useful only after the gui is realized so it has no affect here (and none in the center section of the BorderLayout in any event). We generally use it only for top–level containers, eg, JFrame.
The usual way to specify size information in swing is with the preferred size property. Usually a components layout manager will compute the preferred size in the process of laying out the child components. With a graphic component like DrawPanel the layout manager has no idea that you are drawing so you have to find a way to tell the parent container (JFrame) what size your component requires. One way is to call 'setPreferredSize' on or in (the constructor of) the component. Another is to override the 'getPreferredSize' method in the component and return the size information that you want, like this:
Now you can add your 'drawPanel' to a container with a layout manager that respects/uses the child components preferred size, eg, FlowLayout and GridBagLayout, and it will show up. Box layout is a specialized layout manager designed for buttons and/or similarly–sized components (see api). In some cases it may require a minimum and/or maximum size to work properly. Sometimes you can do this
or even put it inside the constructor of the component