Solved

Creating a block of color

Posted on 2004-09-08
9
225 Views
Last Modified: 2010-03-31
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
Comment
Question by:agrees
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12006618
Use three JLabels and set as label icon a gif of a square block of the wanted color
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 12006630
Use a panel for your square block and set its colour and size.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12006631
See http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JLabel.html (especially the setIcon() function)
0
 
LVL 92

Expert Comment

by:objects
ID: 12006654
JPanel p = new JPanel();
p.setBackground(Color.red);

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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 12006679
BoxLayout would seem the best, and set the maximum size of the panel

panel.setMaximumSize(new Dimension(w, h));
0
 

Author Comment

by:agrees
ID: 12009099
Using a JPanel seems to be the simplest way and it works, thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12009205
   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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12009210
Not simpler than that ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12012080
> Using a JPanel seems to be the simplest way and it works, thanks!

your welcome :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 53
jar file executable 12 40
oracle 11g 23 49
even odd program using while loop 3 28
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now