Hello,

The program below has two JPanels. The first JPanel, panel contains the labels, buttons and combobox. The second JPanel, panel_1 has the image.I am using BoxLayout to line up the belows vertically. The issue I am having is when I run the program, the compoments of the panel do not show up and the image in panel_1 shows up fine. Only when I point my mouse to the first panel, I see the combo box and the buttons. The labels do not show up at all. I will be grateful for any suggestions to fix this issue.
Thank you!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;

public class Scrolls extends JApplet
{
    private JComboBox <String> cbox;
    private JPanel panel, panel_1;
    private JButton button;
    private int j = 0, cell = 0;;
    private JLabel label1, label2;
    private String options[]  = {"A", "B", "C", "D", "E"};
    private String answer[] = new String[10];
    private BufferedImage image;
    private LayoutManager layout;

    public void init()
    {
        getContentPane().setBackground (Color.GRAY);
        File file = new File ("image.jpg");

        try 
        {
            image = ImageIO.read(file);

        }
        catch (IOException e)
        {
        }
        layout = new BoxLayout (getContentPane(), BoxLayout.Y_AXIS);
        setLayout (layout);

        label1 = new JLabel("Answer Options ===>");
        label2 = new JLabel("<=== Press this button to turn in your answer");
        cbox = new JComboBox <String> (options);
        button = new JButton ("Select");
        panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel_1 = new JPanel();

        panel.add(label1);
        panel.add(cbox);
        panel.add(button);
        panel.add(label2);

        cbox.addItemListener(
            new ItemListener()
            {
                public void itemStateChanged (ItemEvent e)
                {
                    if (e.getStateChange() == 1)
                    {
                        j = cbox.getSelectedIndex();
                    }
                }
            }

        );

        button.addActionListener(
            new ActionListener()
            {
                public void actionPerformed (ActionEvent e)
                {
                    answer[cell] = options[j];
                    cell++;
                    JOptionPane.showMessageDialog (null, "Your choice was " + options[j]);
                }
            }
        );


        add(panel);
        add(panel_1);
    }

    public void paint (Graphics g)
    {                                           
        g.drawImage (image, 0,100,image.getWidth(), image.getHeight(), panel_1);

    }

}

You overide paint for the whole applet, but in it you only paint the image. That leaves the rest of the applet unpainted. You could try a super.paint(g); as the first line of your paint, so the rest of the applet gets drawn properly befroe you draw the image.
A better solution is to have your own trivial subclass of JPanel for the image, then you only need to subclass paint for that component, not the whole applet.
An even better solution is to convert your image to an ImageIcon and put that in a JLabel. Then there's no need for any custom paint code at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.