Solved

Creating a block of color

Posted on 2004-09-08
9
215 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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 …

760 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

20 Experts available now in Live!

Get 1:1 Help Now