0

I have a Jframe called TabletAddDetails and when I click back button the Jframe TabletAddDetails will be hidden and open Jframe Tablet. There is a button in Jframe Tablet open Jframe TabletAddDetails . Is there any way that I could open the same previously hidden Jframe TabletAddDetails instead of a completely new instance of Jframe TabletAddDetails so that my progress bar update will not get lost when I come back.

Here is the code for back Buttom in Jframe TabletAddDetails

JButton btnBack = new JButton("BACK");
        btnBack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try
                {

                Tablet tab=new Tablet();
                tab.setVisible(true);
                tab.setLocationRelativeTo(null);
                tab.setResizable(false);
                tab.setTitle("Tablet"); 
                setVisible(false);
                                try {
                    tab.setIconImage(ImageIO.read(new File("img/icon.png")));
                }
                catch (IOException exc) {
                    exc.printStackTrace();
                }

                }
                 catch (Exception e) 
                {

                     JOptionPane.showMessageDialog(null,e.getClass().getName() + ": " + e.getMessage()); 
                    }
            }
        });

Here is the code for Button TabletAddDetails in Jframe Tablet

JButton btnDetails = new JButton("TabletAddDetails");
        btnDetails.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try
                {

                TabletAddDetails tabadd=new TabletAddDetails();
                tabadd.setVisible(true);
                tabadd.setLocationRelativeTo(null);
                tabadd.setResizable(false);
                tabadd.setTitle("Tablet Add Details"); 
                setVisible(false);
                                    try {
                    tabadd.setIconImage(ImageIO.read(new File("img/icon.png")));
                }
                catch (IOException exc) {
                    exc.printStackTrace();
                }
                                }
                 catch (Exception e) 
                {

                     JOptionPane.showMessageDialog(null,e.getClass().getName() + ": " + e.getMessage()); 
                    }

            }
        });
Geordy James
  • 2,358
  • 3
  • 25
  • 34

3 Answers3

2

Any way I just found a solution to update the progress bar even if I open another instance of Jframe TabletAddDetails . Now when I come back the progress bar update will be continued . I implement it in a quite easy way . I have a time taking function which is done by using swingworker . Previously I implement the swingworker in the same Jframe TabletAddDetails . Now what I do is very simple , just make another class in src folder and copy all the codes in the swingworker to it and call that class when I press the buttion in Jframe TabletAddDetails.

Here since the data manipulation and update of progress bar is done by another separate class (ie the Model in MVC) which is linked to your Jframe (View in MVC), so even if you close and come back to same Jframe the functional part will still connected to it. So I hardly recommend to follow MVC when implementing java swing projects.

Community
  • 1
  • 1
Geordy James
  • 2,358
  • 3
  • 25
  • 34
0

Implement WindowListener in your new jFrame and pass a reference to the parent jFrame.

void windowClosed(WindowEvent e) {
    parent.setVisible(true);
}
Breina
  • 538
  • 5
  • 20
0

Use a reference to Tablet as argument in the constructor of TabletAddDetails

        private JFrame myTablet;

        public TabletAddDetails(JFrame parent) {
                 myTablet = parent;
        }

...

        JButton btnBack = new JButton("BACK");
        btnBack.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent arg0) {
                    try {

                        myTablet.setVisible(true);
                        // Maybe you have already set this in original instance   
                        // myTablet.setLocationRelativeTo(null);
                        // myTablet.setResizable(false);
                        // myTablet.setTitle("Tablet"); 
                        setVisible(false);

...

RubioRic
  • 2,442
  • 4
  • 28
  • 35
  • ? That code does exactly what you ask. I don't know what you mean. Maybe it doesn't work with the rest of your app. – RubioRic Mar 17 '16 at 18:47