JButton problem
Hi All
i have a question about JButton, below that program list :
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class _TvSample extends JPanel implements ActionListener{
private JButton selectVolume;
private JButton selectColor;
private JButton sendUp;
private JButton sendDown;
public _TvSample() {
add(selectVolume= new JButton("Volume"));
add(sendUp = new JButton("UP"));
add(sendDown = new JButton("DOWN"));
add(selectColor = new JButton("Color"));
sendDown.setActionCommand("down");
sendUp.setActionCommand("up");
selectVolume.setActionCommand("volume");
selectColor.setActionCommand("color");
selectVolume.addActionListener(this);
selectColor.addActionListener(this);
sendUp.addActionListener(this);
sendDown.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("volume")){
if(e.getActionCommand().equals("up")){
System.out.println("Volume UP");
volume_status.write("1");
}else if(e.getActionCommand().equals("down")){
System.out.println("Volume DOWN");
volume_status.write("0");
}
}else if(e.getActionCommand().equals("color")){
if(e.getActionCommand().equals("up")){
System.out.println("Color UP");
color_status.write("1");
}else{
System.out.println("Color Down");
color_status.write("0");
}
}
}
....
/////////////////////////////////////////////////
i need to send the volome or color status via socket (not list in this program), the problem is while i try to write the volume_status or color_status (by press the UP or DOWN button after press volume or color) there is nothing to do? i have debug with println below getActionCommand for volume or color, and it look enter the bracket, but it's ignore the up and down
[2018 byte] By [
Sukmana] at [2007-9-30 17:39:44]

Well what you did does not work because,
after you press the volume button, you automatically will go into the actionPerformed method.
The if clause if(e.getActionCommand().equals("up")) is never reached, because before you press the UP button, the whole thing is already over.
You cannot ask for two actionEvents within one Event. Beacause the actionEvent will only contain either volume or up. Not both simultaniously!
hii don't have idea how to make two actionEvents in this case (i still study about GUI with Java), can you help me ?
Think about how a GUI works. On a physical device, like a clock radio, you can press two buttons
simultaneously -- that's how I changed my alarm time this morning. Then I got up and had a bowl
of hemp granola. It's good but you really have to floss afterwards. On a GUI, you can't press two
JButtons simultaneously. There are other controls you can use, for example you could use a
JCheckBox, or a group of JRadioButtons (or JToggleButtons) intead. Then you could test in your
Up and Down buttons where a checkbox is selected or a certain JCheckBox or JToggleButton is
selected:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JPanel cp = new JPanel(new GridLayout(0,1));
cp.add(createCheckBoxPanel());
cp.add(createToggleButtonPanel());
cp.add(createRadioButtonPanel());
JFrame f = new JFrame("ButtonExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(cp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
static JPanel createCheckBoxPanel() {
final JCheckBox floss = new JCheckBox("Floss");
JButton btn = new JButton("go");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
System.out.println("flossed = " + floss.isSelected());
}
});
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.setBorder(BorderFactory.createTitledBorder("JCheckBox"));
p.add(floss);
p.add(btn);
return p;
}
static JPanel createToggleButtonPanel() {
AbstractButton bathe = configure(new JToggleButton("Bathe"));
AbstractButton shower = configure(new JToggleButton("Shower"));
AbstractButton cologne = configure(new JToggleButton("Cologne"));
JButton btn = new JButton("go");
final ButtonGroup bg = new ButtonGroup();
bg.add(bathe);
bg.add(shower);
bg.add(cologne);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
ButtonModel model = bg.getSelection();
if (model != null)
System.out.println(model.getActionCommand());
else
System.out.println("no selection");
}
});
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.setBorder(BorderFactory.createTitledBorder("JToggleButton"));
p.add(bathe);
p.add(shower);
p.add(cologne);
p.add(btn);
return p;
}
static JPanel createRadioButtonPanel() {
AbstractButton hemp = configure(new JRadioButton("Hemp"));
AbstractButton shreddies = configure(new JRadioButton("Shreddies"));
AbstractButton donuts = configure(new JRadioButton("Donuts"));
JButton btn = new JButton("go");
final ButtonGroup bg = new ButtonGroup();
bg.add(hemp);
bg.add(shreddies);
bg.add(donuts);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
ButtonModel model = bg.getSelection();
if (model != null)
System.out.println(model.getActionCommand());
else
System.out.println("no selection");
}
});
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.setBorder(BorderFactory.createTitledBorder("JRadioButton"));
p.add(hemp);
p.add(shreddies);
p.add(donuts);
p.add(btn);
return p;
}
//The actioncommand of the model is not automatically copied from the button
static AbstractButton configure(AbstractButton btn) {
btn.getModel().setActionCommand(btn.getActionCommand());
return btn;
}
}
Hi, thank for your explain. i have run your program, but in my case there are still have problem
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import CommLibrary.*;
public class _TvSample2 {
//////////////////////////////
CommunicationManager cm;
CommunicationOutputStream volume_status;
CommunicationOutputStream color_status;
////////////////////////////////
public static void main(String[] args) {
//createRadioButtonPanel
JPanel cp = new JPanel(new GridLayout(0,1));
cp.add(createRadioButtonPanel());
JFrame f = new JFrame("ButtonExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(cp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
cp.initCommLibrary();
}
static JPanel createRadioButtonPanel(){
AbstractButton hemp = configure(new JRadioButton("Hemp"));
AbstractButton shreddies = configure(new JRadioButton("Shreddies"));
AbstractButton donuts = configure(new JRadioButton("Donuts"));
JButton btn = new JButton("go");
JButton btn2 = new JButton("down");
btn.setActionCommand("btnGo");
btn2.setActionCommand("btnDown");
final ButtonGroup bg = new ButtonGroup();
bg.add(hemp);
bg.add(shreddies);
bg.add(donuts);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
ButtonModel model = bg.getSelection();
if (model != null){
System.out.println(model.getActionCommand());
System.out.println("positif");
}
else
System.out.println("no selection");
}
}
);
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
ButtonModel model = bg.getSelection();
if (model != null){
System.out.println(model.getActionCommand());
System.out.println("negatif");
}
else
System.out.println("no selection");
}
}
);
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.setBorder(BorderFactory.createTitledBorder("JRadioButton"));
p.add(hemp);
p.add(shreddies);
p.add(donuts);
p.add(btn);
p.add(btn2);
return p;
}
//The actioncommand of the model is not automatically copied from the button
static AbstractButton configure(AbstractButton btn) {
btn.getModel().setActionCommand(btn.getActionCommand());
return btn;
}
public void initCommLibrary() {
//////////////////////////////////////////////////////
CommunicationManager cm = new CommunicationManager();
volume_status = cm.createOutputStream("volume_status");
color_status = cm.createOutputStream("color_status");
//port_b = cm.createInputStream("port_b");
//port_b.addInputListener(this);
cm.init("127.0.0.1");
System.out.println("Initialization completed");
///////////////////////////////////////////////////////
}
public void termCommLibrary() {
///////////////////////////////////////////////////////
cm.term();
///////////////////////////////////////////////////////
}
}
the error is i cannot initialize the initCommLibrary, see the errror resutl :
$ javac -classpath 'CommLibrary.jar;.' _TvSample2.java
_TvSample2.java:38: cannot resolve symbol
symbol : method initCommLibrary ()
location: class javax.swing.JPanel
cp.initCommLibrary();
^
1 error
You are making basic mistakes in syntax -- you define initCommLibrary as a static method in class _TvSample2,but you invoke:cp.initCommLibrary(); where cp is a JPanel. So you should writeTvSample2.initCommLibrary();Right?