0

I create a jPanel and I open a new jDialog when I click on button (dlgSegments is a JDialog that opened):

JButton btnAddSegment = new JButton("Add GeoSegment");
btnAddSegment.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        dlgSegments.setVisible(true);
    }
});

Then in the opened jDialog I want to return the "selected" back to the jPanel which called this jDialog.

How can I implement it?

This is the button listener in the opened jDialog and seleced is the variable that I want to pass to the jPanel:

addButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        GeoSegment selected = lstSegments.getSelectedValue();
        // i want to send back the selected value
    }
});
messivanio
  • 2,263
  • 18
  • 24
user1720281
  • 35
  • 1
  • 2
  • 6
  • 1
    for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, complilable, just about a.m. issues – mKorbel Nov 30 '12 at 10:35
  • If the dialog is modal, it will block at the point it was called until it is closed, at which point you would be able to request the request information you need from it – MadProgrammer Nov 30 '12 at 10:50
  • @MadProgrammer , i didnt understand you. i will be happy for an short example – user1720281 Nov 30 '12 at 11:00
  • 1
    *"Pass data via jdialog to another jpanel"* This question makes no sense unless you have a class that extends `JPanel`. A tip: **Don't extend `JPanel`, just keep a reference to one.** If you follow that advice, the problem might solve itself. Otherwise, post an SSCCE of your best attempt. – Andrew Thompson Nov 30 '12 at 11:11

2 Answers2

0

One possible solution: If you create your Dialog send an DataObject to the Dialog via his constructor. The DataObject conatins the value you are interested in.

MyDataObject dataObject = new MyDataObject();
MyDialog dialog = new MyDialog(dataObject);

fill the DataObject with the selected value in the dialog.

ollins
  • 1,851
  • 12
  • 16
  • hi, thanks for answer, i want to do that every time that i click on button addButton in the opened jdialog it will send a variable to the "original" jpanel...with your suggestion i can only pass one variable... – user1720281 Nov 30 '12 at 10:59
  • The content of MyDataObject can be a list. So you can add the selected objects to the list. Add ProperyChangeSupport to the dataobject and register the panel as a listener. – ollins Nov 30 '12 at 14:23
0

If you're still working on this problem, your Add button can call getSelectedIndex() or getSelectedIndices() to find out what's selected. Then fire a PropertyChangeEvent like @Hovercraft Full Of Eels shows here. Have you main panel do addPropertyChangeListener() to listen to the dialog.

Community
  • 1
  • 1
Catalina Island
  • 7,027
  • 2
  • 23
  • 42