Playing with JLabel and JPanel
I have a JPanel called operationPanel in BorderLayout.WEST
1)
In that panel i have 4 JLabels which i want to be able to manipulate individually. The labels contains steps to operating a simple application, and when a step is completed, i want the text in the JLabel to turn grey.
But i've check the API for JLabel and it doesn't take it any parameters for Color. Then how do i change the color?
2)
How do i display these labels 1 label below the other? I do not want to use GridLayout because it leaves alot of space between labels...
[579 byte] By [
Ant_Magma] at [2007-11-11 6:49:22]

# 1 Re: Playing with JLabel and JPanel
Note: when dealing with JLabels (background) coloring you must use the
label.setOpaque(true) method, or else the background container color will
show.
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
/**
* Test driver frame for labels panel
*/
public class LabelsFrame
extends JFrame implements ActionListener {
BorderLayout borderLayout1 = new BorderLayout();
LabelPanel lblPan = new LabelPanel();
JPanel centerPan = new JPanel();
JButton setBtn = new JButton();
JTextField lblNoTF = new JTextField();
JButton resetBtn = new JButton();
public LabelsFrame() {
try {
jbInit();
setBtn.addActionListener(this);
resetBtn.addActionListener(this);
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
setBtn.setText("Set Label");
lblNoTF.setText("");
lblNoTF.setColumns(7);
resetBtn.setOpaque(true);
resetBtn.setText("Reset Label");
this.getContentPane().add(lblPan, BorderLayout.WEST);
this.getContentPane().add(centerPan, BorderLayout.CENTER);
centerPan.add(setBtn, null);
centerPan.add(lblNoTF, null);
centerPan.add(resetBtn, null);
}
public void actionPerformed(ActionEvent e) {
int lblNo=Integer.parseInt(lblNoTF.getText().trim());
if (e.getSource()==setBtn) {
lblPan.setLabelCompleted(lblNo, true);
} else {
lblPan.setLabelCompleted(lblNo, false);
}
}
/**
* ******************** MAIN *********************
* @param args
*/
public static void main(String[] args) {
LabelsFrame lf = new LabelsFrame();
lf.setBounds(200,200, 300,200);
lf.addWindowListener(new WindowAdapter() { // ensure proper termination
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
lf.pack();
lf.setVisible(true);
}
}
/*****************************************************
* The labels panel class.
* Uses a bordeLayout with a gridlayout panel in NORTH.
* This will pack the labels panel to the size required
* by the labels, and keep them at equal size.
*/
class LabelPanel
extends JPanel {
BorderLayout borderLayout = new BorderLayout();
JPanel labelPanel = new JPanel();
GridLayout gridLayout = new GridLayout();
Color baseColor = null;
JLabel lbl1 = new JLabel();
JLabel lbl2 = new JLabel();
JLabel lbl3 = new JLabel();
JLabel lbl4 = new JLabel();
JLabel[] labels = {
lbl1,
lbl2,
lbl3,
lbl4
};
public LabelPanel() {
try {
jbInit();
baseColor = lbl1.getBackground();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void setLabelCompleted(int labelNo, boolean completed) {
if (completed) {
labels[labelNo].setBackground(Color.gray);
}
else {
labels[labelNo].setBackground(baseColor);
}
}
/**
* Without this JPanel method override the labels panel will
* size to the width of the longest label (the label with the
* longest text string)
*/
public Dimension getPreferredSize() {
return new Dimension (100,100);
}
/**
* GUI setup JBuilder style
*/
private void jbInit() throws Exception {
this.setLayout(borderLayout);
labelPanel.setLayout(gridLayout);
gridLayout.setColumns(1);
gridLayout.setHgap(0);
gridLayout.setRows(4);
gridLayout.setVgap(4);
lbl1.setBorder(BorderFactory.createEtchedBorder());
lbl1.setOpaque(true);
lbl1.setText("lbl1");
lbl2.setBorder(BorderFactory.createEtchedBorder());
lbl2.setOpaque(true);
lbl2.setText("lbl2");
lbl3.setBorder(BorderFactory.createEtchedBorder());
lbl3.setOpaque(true);
lbl3.setText("lbl3");
lbl4.setBorder(BorderFactory.createEtchedBorder());
lbl4.setOpaque(true);
lbl4.setText("lbl4");
this.add(labelPanel, BorderLayout.NORTH);
labelPanel.add(lbl1, null);
labelPanel.add(lbl2, null);
labelPanel.add(lbl3, null);
labelPanel.add(lbl4, null);
}
}
sjalle at 2007-11-11 22:40:14 >
