Search code examples
javaswingcolorsjdialog

Add components to JDialog


When I run this, an empty title bar is displayed. I just want to be able to see the components and work from there, but nothing is displayed. The dialog is designed to allow the user to select a color by moving sliders, then return to color to the main page.

import java.awt.*;
import javax.swing.*;

public class ColourDialog extends JDialog
{
    String colorNames[] = {"Red: ", "Green: ", "Blue: "};
    Label labels[] = new Label[3];
    JSlider slider[]= new JSlider[3];
    Label lb;
    static ColourDialog d;

    public void ColourDialog()
    {
        setModal(true);
        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        JPanel sliderPanel = new JPanel();
        sliderPanel.setLayout(new GridLayout(0, 1));

        for (int i = 0; i < slider.length;  i++)
        {
            labels[i] = new Label(colorNames[i] + 255);

            sliderPanel.add(labels[i]);
            slider[i] = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
            slider[i].setMinorTickSpacing(10);
            slider[i].setMajorTickSpacing(50);
            slider[i].setPaintTicks(true);
            slider[i].setPaintLabels(true);

            sliderPanel.add(slider[i]);
            //slider[i].addChangeListener(this);
        }

        lb = new Label("Colour");

        c.add(sliderPanel, BorderLayout.CENTER);
        c.add(lb, BorderLayout.SOUTH);

        setSize(500, 450);
        setLocation(200,200);
        setTitle("Colour Dialog");
    }

    public static Color showDialog()
    {
        if (d == null)
        d = new ColourDialog();

        d.show();

        //return new Color(red,green,blue);
        return new Color(0,0,0);
    }

    public static void main(String args[])
    {
        ColourDialog.showDialog();
    }
}

Solution

  • I think it might be because you say "public void ColourDialog()" this is an invalid constructor. Try getting rid of the "void" and try again.