Ciao a tutti,
Sto costruendo un programmino java con paradigma mvc, quello che proprio non riesco a capire è come e dove intercettare il valore di una textbox premuto un dato pulsante.
In pratica alla pressione del Jbutton Aggiungi qualità vorrei intercettare il valore di JComboBox.
La logica mi suggerisce nel controller ma non so come accedere all'oggetto Jcombobox
Frame
codice:
public class GestionePersonaggioFrame extends JFrame {
public GestionePersonaggioFrame(Personaggio modelUser,Skills modelSkills) {
GestionePersonaggiVista gestUsrVista = new GestionePersonaggiVista(modelUser, modelSkills);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(gestUsrVista, BorderLayout.CENTER);
JButton exitButton = new JButton("exit");
cp.add(exitButton, BorderLayout.SOUTH);
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
this.addWindowListener(new WindowAdapter() {
@SuppressWarnings("unused")
public void WindowClosing(WindowEvent event) {
System.exit(0);
}
});
setTitle("Gestione Personaggio");
setSize(550, 250);
setVisible(true);
}
public static void main(String[] argv) {
Personaggio modelUser = new Personaggio();
Skills modelSkills = new Skills();
@SuppressWarnings("unused")
GestionePersonaggioFrame gestPersFrame = new GestionePersonaggioFrame(modelUser, modelSkills);
}
}
Controller
codice:
public class ControlloGestionePersonaggio implements ActionListener {
private Personaggio user;
public ControlloGestionePersonaggio(Personaggio user) {
this.user = user;
}
@Override
public void actionPerformed(ActionEvent event) {
JButton source = (JButton) event.getSource();
if(source.getText().equals("Incrementa Attacco")) {
user.addAttack();
}else if(source.getText().equals("Incrementa Difesa")) {
user.addDefense();
}else if(source.getText().equals("Aggiungi Qualita")) {
user.addSkill();
}else if(source.getText().equals("Missione")) {
user.Mission();
}
/*else if(source.getText().equals("Reset"))
contatore.reset(0);
else if(source.getText().equals("Frenzy")) {
if (frenzy.size() > 0) {
for (Thread t : frenzy)
t.interrupt();
frenzy.clear();
}
else {
for (int i=0; i<4; i++) {
frenzy.add(new Thread(new ContatoreFrenzy(contatore)));
frenzy.get(i).start();
}
}
}*/
}
}
View
codice:
public class GestionePersonaggiVista extends JPanel implements Observer {
private JLabel lblData;
private JLabel lblError;
private JLabel lblSkills;
private JComboBox<String> CBSkills;
private Personaggio personaggio;
public GestionePersonaggiVista(Personaggio modelUser,Skills modelSkills) {
super(new BorderLayout());
personaggio = modelUser;
//skills = modelSkills;
ControlloGestionePersonaggio controller = new ControlloGestionePersonaggio(personaggio);
lblData = new JLabel("");
JPanel pnlData = new JPanel(new FlowLayout());
pnlData.add(lblData);
add(pnlData, BorderLayout.WEST);
lblError = new JLabel("");
JPanel pnlError = new JPanel(new FlowLayout());
pnlError.add(lblError);
add(pnlError, BorderLayout.NORTH);
lblSkills = new JLabel("");
JPanel pnlSkills = new JPanel(new FlowLayout());
pnlSkills.add(lblSkills);
add(pnlSkills, BorderLayout.EAST);
//List<String> ListSk = new ArrayList<String>();
CBSkills = new JComboBox<>();
CBSkills.addItem("");
for(Skill sk : modelSkills.getSkills()) {
CBSkills.addItem(sk.getDescription());
}
JPanel pnlNewSkills = new JPanel(new FlowLayout());
pnlNewSkills.add(CBSkills);
add(pnlNewSkills, BorderLayout.CENTER);
JPanel panelSouth = new JPanel(new FlowLayout());
JButton bttAttack = new JButton("Incrementa Attacco");
bttAttack.addActionListener(controller);
panelSouth.add(bttAttack);
JButton bttDefense = new JButton("Incrementa Difesa");
bttDefense.addActionListener(controller);
panelSouth.add(bttDefense);
JButton bttSkill = new JButton("Aggiungi Qualità");
bttSkill.addActionListener(controller);
panelSouth.add(bttSkill);
JButton bttMission = new JButton("Missione");
bttMission.addActionListener(controller);
panelSouth.add(bttMission);
add(panelSouth, BorderLayout.SOUTH);
update(modelUser,null);
modelUser.addObserver(this);
}
@Override
public void update(Observable arg0, Object arg1) {
// TODO Auto-generated method stub
lblData.setText(personaggio.getData());
lblError.setText(personaggio.getError());
lblSkills.setText(personaggio.getSkills());
}
}