0

I want to store the variable (id_Category) between my two JPanels because I need the value to work on it on the second Jpanel This the first JPanel

public class PreparerTest extends JPanel
int idCategorie;
public void mouseClicked(MouseEvent e) {
            idCategorie = categories.get(table.getSelectedRow()).getId();

the second JPanel

public class PreparerTestManuelle extends JPanel {
JButton btnAfficher = new JButton("Afficher");
    btnAfficher.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            questions=GestionTestDelegate.doPrepareManuallyTest(the id is supposed to be here, nombreQuestion.getText());

        }
    });
Daly
  • 19
  • 8
  • Making it static works ? – Suresh Atta Apr 25 '16 at 16:32
  • no it doesn't work sorry – Daly Apr 25 '16 at 16:34
  • This [example](http://stackoverflow.com/q/10523343/230513) communicates using a `PropertyChangeEvent`. – trashgod Apr 25 '16 at 16:47
  • The answer will depend. You could use a model which contains the `idCategorie` and which you pass from one panel to another, so when one panel wants to change the value, it changes the value in the model, making it available to both classes. You could also use an observer pattern to provide notification when the value changes, allowing the panels to react to the change automativally – MadProgrammer Apr 25 '16 at 21:52
  • N.B. Neither of those panels should `extends JPanel`. Instead both should just be instances of standard panels. After all, you don't `extends JButton` every time you need a button, so why do it with `JPanel` (or `JFrame`)? – Andrew Thompson Apr 26 '16 at 00:43

1 Answers1

0

Are both JPanels in the same class? If yes you should create field like: public int idCategorie;

And than you can use it in the class wherever you want

GlacialMan
  • 579
  • 2
  • 5
  • 20