Dan
Dan

Reputation: 597

How to transfer data between panels of cardLayout

In my software I am using card layout to create a "wizard-based" interface. In panel, user chooses a file, in the other one get information regarding the chosen file in previous panel.

The problem is that CardLayout loads all panels all together. So panels deals with predefined data. But I would like to update the next panel with the information given in the current panel. each panel has 'next' and 'back' buttons, so I think that is the point where next panels can get updated somehow. I thought using setters and getters methods but could not implement it correctly.

Here is a sample code with two sub-panels: BASE CLASS:

   public Base(){
            frame.setLayout(bl);
            frame.setSize(800, 600);
            frame.add(new MainPanel(), BorderLayout.CENTER);

            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
        public static void main(String[] args) {
            // TODO code application logic here
            new Base();
        }
    }

MainPanel (sub-panels holder)

class MainPanel extends JPanel{
    private CardLayout cl = new CardLayout();
    private JPanel panelHolder = new JPanel(cl);

    public MainPanel() {
        ChooseFile chooseFile = new ChooseFile(this);
        ShowResult showResult = new ShowResult(this);

        panelHolder.add(showResult, "showResult");
        panelHolder.add(chooseFile, "chooseFile");

        cl.show(panelHolder, "chooseFile");
        add(panelHolder);
    }
    public void showPanel(String panelIdentifier){
        cl.show(panelHolder, panelIdentifier);
    }
}

Sub-Panel 1:

class ChooseFile extends JPanel{
    MainPanel ob2;
    JPanel directoryChooserPanel, bottomPanel;
    JButton btn, localSourceBack, localSourceNext;
    JTextField field;
    public ChooseFile(MainPanel mainPanel){
        this.ob2 = mainPanel;
        ShowResult showResult = new ShowResult();
        setLayout(new BorderLayout());

        directoryChooserPanel = new JPanel(new GridLayout(0,2));
        btn = new JButton("Browse");
        btn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                JFileChooser chooser = new JFileChooser("D:\\Desktop");
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int returnVal = chooser.showOpenDialog(null);
                if(returnVal == JFileChooser.APPROVE_OPTION){
                    File myFile = chooser.getSelectedFile();
                    String text = myFile + "";

                    field.setText(text); 
                }
            }
        });


        directoryChooserPanel.add(btn);

        field = new JTextField(20);
        directoryChooserPanel.add(field);



        localSourceNext = new JButton("Next");
        localSourceNext.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                ob2.showPanel("showResult");
                showResult.setRoot(getPath());
            }
        });

        add(directoryChooserPanel, BorderLayout.NORTH);
        add(localSourceNext, BorderLayout.EAST);
    }
    public String getPath(){
        return field.getText();
    }
}

Sub Panel 2:

class ShowResult extends JPanel{
    MainPanel ob2;
    JPanel bottomPanel, labelsPanel;
    JButton srcLocalBTN, srcFtpBTN, sourceLocationBack;
    JLabel result;
    File directory;
    String root;
    ArrayList<String> myFiles = new ArrayList<String>();
    public ShowResult(MainPanel mainPanel){
        this.ob2 = mainPanel;
        setLayout(new BorderLayout());
        result = new JLabel();
        root = "No ADDRESS";
        directory = new File(root);

        listFiles(directory, myFiles);
        String filesNumber = "It contains " + myFiles.size() + " files.";
        result.setText(filesNumber);

        add(result, BorderLayout.NORTH);
    }

    public void listFiles(File directory, ArrayList<String> list){
            for(File file : directory.listFiles()){
                list.add(file.getName());
                if(file.isDirectory()){
                    listFiles(file.getAbsoluteFile(), list);
                }
            }
    }
    public ShowResult(){

    }
    public void setRoot(String chosenPath){
        root = chosenPath;
    }
}

So it firsts loads 'sub panel 1' so user chooses a directory by jFileChooser and then I need to transfer this data to 'sub panel 2'. So it can calculate how many files it is containing. I tried to transfer data by getting the chosen directory and assigning to a variable in the second variable. But doesn't work.

Any idea?

Upvotes: 1

Views: 601

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You're creating more than one ShowResult object, displaying one but then passing information to the other, the non-displayed one, but this is not how Java works (this is easy to discover by simply searching this page for number of new ShowResult() matches). You need to be sure that the ShowResult object that is displayed is the exact same as the one you pass information to, meaning you'll have to pass the reference of the displayed object into your ChooseFile class via a constructor or method parameter.

Better option: use an MVC design pattern, make, have your controls change the state of the model and your views display the state of your model. This may reduce the cyclomatic complexity of your code.

Upvotes: 3

Related Questions