Solved

Creating a block of color

Posted on 2004-09-08
9
237 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java stored proc example 9 44
tomcat startup error 5 100
pagenation logic how it is working in my code 1 52
swing controls 2 15
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

791 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