Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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 >
# 2 Re: Playing with JLabel and JPanel
Ur great sjalle..how long does it take for u to create all these codes? 5 minutes?

Thx man :D
Ant_Magma at 2007-11-11 22:41:14 >
# 3 Re: Playing with JLabel and JPanel
Something like that, I do a lot of copy/paste and I have an extensive set of code
templates. I also have a database (with free text search) for storing all my code so
I very rarely make something from scatch. And, I use JBuilder :)
sjalle at 2007-11-11 22:42:18 >
# 4 Re: Playing with JLabel and JPanel
Ic, since we are chatting here i might as well ask u another question

I have to radio buttons

JRadioButton s1=new JRadioButton("Default distance table");
s1.setSelected(true);
JRadioButton s2=new JRadioButton("User distance table");

//Groups radio buttons, so that one on the othe will off
ButtonGroup group=new ButtonGroup();
group.add(s1);
group.add(s2);

There is another NEXT button after the radio buttons. I want something to happen when s1 is selected and something else to happen if s2 is selected. Clicking the NEXT button will run the selected operation.

How do i code this?
Ant_Magma at 2007-11-11 22:43:12 >