• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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