• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Creating a block of color

Hi all,

I am writing a key for a graph and want to have a list of decriptions, preceeded by a square block of color. ie:

[BLACK] ....some text
[RED] ... some text
[BLUE] ... some text

Whats the best way to add a colored square to a JPanel so that it lines up with some text?
0
agrees
Asked:
agrees
  • 4
  • 2
  • 2
  • +1
1 Solution
 
zzynxSoftware engineerCommented:
Use three JLabels and set as label icon a gif of a square block of the wanted color
0
 
objectsCommented:
Use a panel for your square block and set its colour and size.
0
 
zzynxSoftware engineerCommented:
See http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JLabel.html (especially the setIcon() function)
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
objectsCommented:
JPanel p = new JPanel();
p.setBackground(Color.red);

then add the panel to your container using the required layout.

0
 
objectsCommented:
BoxLayout would seem the best, and set the maximum size of the panel

panel.setMaximumSize(new Dimension(w, h));
0
 
agreesAuthor Commented:
Using a JPanel seems to be the simplest way and it works, thanks!
0
 
CEHJCommented:
   getContentPane().setLayout(new FlowLayout());
    JLabel labRed = new JLabel("\u220e");
    labRed.setForeground(Color.red);
    JLabel labRedDesc = new JLabel(" This is the red key");
    getContentPane().add(labRed);
    getContentPane().add(labRedDesc);
0
 
CEHJCommented:
Not simpler than that ;-)
0
 
objectsCommented:
> Using a JPanel seems to be the simplest way and it works, thanks!

your welcome :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now