1

Hi i'm trying to pass values from one JPanel to another JPanel. I can't figure it out since i've got no constructors to pass it through. I'm trying to get the context from my JTextField in class "CallsPanel" and pass it to my "ActiveCall".

public class ActiveCall extends JPanel {

    private MainFrame frame;
    private JLabel lblNumber;
    private JButton btnHangUp;


    private Controller controller = new Controller();

    public ActiveCall(MainFrame frame) {

        this.frame = frame;
        this.setSize(300, 380);
        this.setLocation(10, 10);

        lblNumber = new JLabel();
        this.add(lblNumber);
        lblNumber.setLocation(10, 10);
        this.lblNumber.setSize(270, 40);

        btnHangUp = new JButton("Læg på");
        this.add(btnHangUp);
        btnHangUp.setLocation(10, 270);
        this.btnHangUp.setSize(270, 40);
        btnHangUp.addActionListener(controller);
    }

     private class Controller implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                JButton source = (JButton) e.getSource();

                if (source == btnHangUp) {
                    frame.showPanel("Calls");   //TODO: SKAL AFSLUTTE OPKALD, GEMME DET I - OG REDIRECT TIL LOGGEN!

                }

            }

        }
}



public class CallsPanel extends JPanel {


    private MainFrame frame;
    private JTextField txfCall;
    private JButton btnSearch, btnCall, btnCallLast, btnDelete;
    private JList lstOpkald;
    private JScrollPane scpOpkald;

    private Controller controller = new Controller();

    public CallsPanel(MainFrame frame) {

        this.frame = frame;
        this.setSize(300, 380);
        this.setLocation(10, 10);

        txfCall = new JTextField();
        this.add(txfCall);
        txfCall.setLocation(10, 10);
        txfCall.setSize(210, 40);
        txfCall.setText("Indtast Telefonnummer...");
        txfCall.addActionListener(controller);

        btnCall = new JButton("Ring Op");
        this.add(btnCall);
        btnCall.setLocation(10, 60);
        btnCall.setSize(270, 40);
        btnCall.addActionListener(controller);

        lstOpkald.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent arg0) {
                // TODO Sætter valgte nummer op i JTextField
                if(!lstOpkald.isSelectionEmpty())
                    txfCall.setText(Integer.toString(Service.getOpkaldList().get(lstOpkald.getSelectedIndex()).getTelefonnummer()));
            }
        });
    }

     private class Controller implements ActionListener
     {
            public void actionPerformed(ActionEvent e)
            {
                JButton source = (JButton) e.getSource();

                if (source == btnSearch) {      //SKAL ÅBNE EN NY TOM FRAME, HVOR MAN KAN SØGE OG VÆLGE EN KONTAKT HVOR TELEFONNUMMERET BLIVER AUTOMATISK PASTET IND I TLFNUMMER TEKSTFELTET!

                }

                if (source == btnCall) {        //TODO: SKAL SIMULERE ET OPKALD!
                    frame.showPanel("ActiveCall");
                }

                if (source == btnCallLast) {    //TODO: SKAL SIMULERE ET OPKALD FRA DET SIDSTE OPKALDTE NUMMER!
                    frame.showPanel("ActiveCall");
                }

                if (source.equals(btnDelete)) { // SLETTER MARKERET OPKALD FRA ARRAYLIST!
                    Opkald2 opkald = (Opkald2) lstOpkald.getSelectedValue();

                    if (opkald != null)
                    {
                        Service.deleteOpkald(opkald);
                    }
                    updateOpkaldList();         
                }
            }


            private void updateOpkaldList()
            {
                //Listen over opkald opdateres
                lstOpkald.setListData(Service.getOpkaldList().toArray());
            }
        }           

     public String getNumber()
     {
         String number = txfCall.getText();
         return number;
     }
}
  • 1
    Tip: Clean up the unnecessary fields, so people can focus on the actual problem. Dumping whole classes is not a good thing. – givanse Dec 07 '13 at 23:10
  • I tried to clean it up a bit, i don't know if that is enough. trashgod i need something more simple i think. – user2993005 Dec 07 '13 at 23:53

1 Answers1

1

I need something more simple, I think.

I infer that you want ActiveCall to receive an event the signifies a change in the selection state of the JList in CallsPanel. CallsPanel itself contains a ListSelectionListener that updates txfCall. As a component can have more that one listener, let ActiveCall also contain a ListSelectionListener that is registered to receive events from lstOpkald.

If no existing event suits your need, you can define you own PropertyChangeEvent, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045