• Post Reply Bookmark Topic Watch Topic
  • New Topic

actionEvent problems

 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.*;
import java.awt.*;
public class HoyrePan extends JPanel{
JTextField txtSok;
JRadioButton rbtnNr, rbtnForn, rbtnEttn;
public HoyrePan() {
ButtonGroup gruppe = new ButtonGroup();
rbtnNr = new JRadioButton();
rbtnForn = new JRadioButton();
rbtnEttn = new JRadioButton();
txtSok = new JTextField("");

gruppe.add(rbtnNr);
gruppe.add(rbtnForn);
gruppe.add(rbtnEttn);

add(rbtnNr);
add(rbtnForn);
add(rbtnEttn);
add(txtSok);

setLayout(new GridLayout(4,1));
}

public JTextField getTxtSok() {
return txtSok;}

public JRadioButton getRadioNr() {
return rbtnNr;}

public JRadioButton getRadioFornavn() {
return rbtnForn;}

public JRadioButton getRadioEtternavn() {
return rbtnEttn;}

}












GUI_Vis_StudInfo2 is the class that generates those panels.
the problem is on the code where it is bold.
the button doesn't do anything, but it should.

any ideas?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For testing String equality we use the equals method
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it worked before even i didnt use the equals method.
but now it doesnt work even i use the equals method.

the programs goes directly to the "else" instruction.
why is that?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ActionListener for btnSok was being added to the sor instance but another new instance of SorPanel was being added to the content pane. Try adding the sor instance to the content pane.
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much graig!

i got another question :


why do we actually need to add this (class) as an argument to
the listener class?
is that because those reference of buttons can be used by the listener class in this way?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems to have to do with the design of your application. The class that contains the ActionListener event code, CTRL_Vis_StudInfo, needs a reference to the GUI_Vis_StudInfo2 class, namely gvs

so it can access some member fields and/or methods in the GUI_Vis_StudInfo2 class, such as the getNr method and the sOk member shown here

So these statements

pass a reference to their enclosing class, GUI_Vis_StudInfo2, on to the listener, ie, CTRL_Vis_StudInfo, via its class constructor. An improvement in the registration of listeners might be

in which you use one instance of the listener for both components.

As an aside, the keyword this, which refers to the enclosing class, can be deciphered with a statement like
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!