0

I want to make a setup wizard that will ask for the background color of your widow. Also, I want a browse button that saves a variable of type IconImage. I am getting compile errors for this code. Please help! Here is the beginning code:

import java.awt.color.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Setup {
     private static String colorSelected;
    public static void main(String[] args) {
        final JFrame f = new JFrame("Test Setup wizard");
        Container a = f.getContentPane();
        a.setBackground(Color.white);
        a.setLayout(new  FlowLayout());
        JLabel question1 = new JLabel("What would you like the background color to be?");
        JButton Next = new JButton("Next");
        final String Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
        final JList colors = new JList(Colors);
        colors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        colors.setLayoutOrientation(JList.VERTICAL);
        JScrollPane listScroller = new JScrollPane(colors);
        colors.addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    int index = e.getFirstIndex();
                    colorSelected = Colors[index];
                    }
                });
        f.add(question1);
        f.add(colors);
        f.add(Next);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(500,500);
        f.setVisible(true);
        final ImageIcon img = new ImageIcon("HardDisk.jpg");
        f.setIconImage(img.getImage());
        Next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent Ev) {
                    final Color[] Selected = new Color[1];
                    if (colorSelected .equals("black")) {
                        Selected[0] = new Color(0,0,0);
                    }
                    else if (colorSelected .equals("blue")) {
                        Selected[0] = new Color(0,0,255);
                    }
                    else if (colorSelected .equals("cyan")) {
                        Selected[0] = new Color(0,225,225);
                    }
                    else if (colorSelected .equals("darkGray")) {
                        Selected[0] = new Color(169,169,169);
                    }
                    else if (colorSelected .equals("gray")) {
                        Selected[0] = new Color(128,128,128);
                    }
                    else if (colorSelected .equals("green")) {
                        Selected[0] = new Color(0,255,0);
                    }
                    else if (colorSelected .equals("lightGray")) {
                        Selected[0] = new Color(211,211,211);
                    }
                    else if (colorSelected .equals("magenta")) {
                        Selected[0] = new Color(255,0,255);
                    }
                    else if (colorSelected .equals("orange")) {
                        Selected[0] = new Color(255,165,0);
                    }
                    else if (colorSelected .equals("pink")) {
                        Selected[0] = new Color(255,20,147);
                    }
                    else if (colorSelected .equals("red")) {
                        Selected[0] = new Color(255,0,0);
                    }
                    else if (colorSelected .equals("white")) {
                        Selected[0] = new Color(255,255,255);
                    }
                    else if (colorSelected .equals("yellow")) {
                        Selected[0] = new Color(255,255,0);
                    }
                f.dispose();
                JLabel complete = new JLabel("You are now complete.");
                JFrame f = new JFrame("Complete");
                Container a = f.getContentPane();
                a.setBackground(Selected[0]);
                f.add(complete);
                f.setSize(500,500);
                f.setVisible(true);
                f.setIconImage(img.getImage());
            }
            });
    }
}

And the errors are:

Setup.java:15: error: incompatible types
                final String Colors = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
                                      ^
  required: String
  found:    String[]
Setup.java:16: error: no suitable constructor found for JList(String)
                final JList colors = new JList(Colors);
                                     ^
    constructor JList.JList() is not applicable
      (actual and formal argument lists differ in length)
    constructor JList.JList(Vector) is not applicable
      (actual argument String cannot be converted to Vector by method invocation conversion)
    constructor JList.JList(Object[]) is not applicable
      (actual argument String cannot be converted to Object[] by method invocation conversion)
    constructor JList.JList(ListModel) is not applicable
      (actual argument String cannot be converted to ListModel by method invocation conversion)
Setup.java:23: error: array required, but String found
                        colorSelected = Colors[index];
                                              ^
3 errors

Any help/new code appreciated! Thanks By the way, a JList can be made with an array of Strings. I don't understand! Please help!

2
  • Color c = Color.RED; (Color is not an enum; it's a class with some static public data members.) Jul 9, 2013 at 4:02
  • See also enum Hue, seen here.
    – trashgod
    Jul 9, 2013 at 7:59

2 Answers 2

2

You can give this code a try, it listens every time a "color" is clicked and saves the color to "colorSelected". You probably need to tweak it a bit since it's invoked twice since a click is two events. Anyway, hopefully it accomplishes the requirement you asked:

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Setup {

    private static String colorSelected;

    public static void main(String[] args) {
        JFrame f = new JFrame("Test Setup wizard");
        JLabel question1 = new JLabel("What would you like the background color to be?");
        final String Colors[] = new String[]{"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"};
        JList colors = new JList(Colors);
        colors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        colors.setLayoutOrientation(JList.VERTICAL);
        colors.setVisibleRowCount(-5);
        colors.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                int index = e.getFirstIndex();
                colorSelected = Colors[index];
                System.out.println(colorSelected);
            }
        });

        JScrollPane listScroller = new JScrollPane(colors);
        f.add(colors);
        f.setSize(500, 500);
        f.setVisible(true);
    }
}

Also, I think most people are getting confused that "Colors" is an enum because it's uppercase. Colors is just a String array. The convention is to camelCase object names, so "colors" would've been a preferable name and you probably should rename your JList "colors" to just being "jlist" perhaps.

0
1

I don't think Color is an enum; it has a bunch of static variables but it's not defined as an enum.

That being said, adding more values to a given class is generally a bad idea. The best option would be to instantialize a Color variable, and then assign it either one of the existing colors or use the Color(int r, int g, int b) constructor, like this:

Color purple = new Color((139, 0, 139);
guiObject.setBackground(purple);

If you must have a Color.//my variable format, just create a new class, say public class CustomColor extends Color and then tweak to your heart's content.

1
  • I don't need that but I think this is a great idea. Thanks!
    – user2558684
    Jul 9, 2013 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy