Results 1 to 14 of 14

Thread: Deleting panels

  1. #1
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default Deleting panels

    Hey

    I want to delete a panel from a tabbedPane from a separate class. The class extends the main form which contains the tabbedPane and panel
    So i have a class -
    public class delete extends form {

    public void main() {
    tabbedPane.remove(panel);
    }
    }
    This class is called using this method in the main frame
    delete delClass = new delete();
    delClass.main();

    I have added a print statement in the class, and it prints out so the main method from the delete class runs, but the panel doesn't get removed
    any idea??

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Comment on your coding style: Conventions start class names with uppercase letters: Delete vs delete

    Read about the Container validate method

  3. #3
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    thanks for pointing that out norm, i should be getting used to conventions now
    I read about the container validate method, it causes a components to layout all of it's sub-components again. So i added
    tabbedPane.validate();
    in the main form, that didn't work. Any idea
    sorry if i am missing something :confused:

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Show us more of your code. I recommend that you create the smallest possible program that demonstrates your problem but has no code unrelated to your problem. It would likely consist of two Java classes, one of the panel and one for the gui with a tabbed pane, and could have a button which tries to do what you desire on button press.

  5. #5
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    here is a little test app i created, a netbeans project - testApp.zip/
    and here is the code

    TestFrame.java - This contains all the UI created in Netbeans IDE 6.9.1


    PHP Code:
    public class TestFrame extends javax.swing.JFrame {
    
        /** Creates new form TestFrame */
        public TestFrame() {
            initComponents();
        }
        private void initComponents() {
         //All the UI generated code from NetBeans, which i cut out
            }
    
        private void panelDelBtnActionPerformed(java.awt.event.ActionEvent evt) {
            DeletePanelClass deleteClass = new DeletePanelClass();
            deleteClass.deletePanel();
        }
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TestFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        public javax.swing.JPanel panel1;
        public javax.swing.JPanel panel2;
        public javax.swing.JButton panelDelBtn;
        public javax.swing.JTabbedPane tabbedPane;
        // End of variables declaration
    
    }
    DeletePanelClass


    PHP Code:
    public class DeletePanelClass extends TestFrame{
        public void deletePanel()
        {
            tabbedPane.remove(panel2);
            tabbedPane.validate();
            System.out.println("I run");
        }
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Your posted code doesn't seem to do anything. It displays a minimum frame that doesn't close.
    Can you explain what the code you've posted does when executed?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To create a test app that we can all test and run, don't do it with netbeans-generated code, but rather a very simple Swing app with simple layout managers. Then you can post the whole app here rather than having us wade through the zip with gobs of ugly NetBeans-generated code.

    edit:
    I think I see what your problem is: you're using inheritance inappropriately. Your DeletePanelClass extends TestFrame, and by doing so inherits all its fields, but those fields, including the tabbedPane and panel2 fields refer to completely different objects from the one displayed in the visible TestFrame object. What you should be doing instead is passing a reference of the TestFrame object to your outside class and calling methods on this reference; for instance, something like this:

    Java Code:
    class DeletePanelHandler {
       private TestFrame testFrame;
       
       public DeletePanelHandler(TestFrame testFrame) {
          this.testFrame = testFrame;
       }
       
       public void deletePanel() {
          testFrame.tabbedPane.remove(testFrame.panel2);
          testFrame.tabbedPane.revalidate();
          testFrame.tabbedPane.repaint();
       }
    }
    But having said this, I wouldn't recommend directly fiddling with the testFrame's fields, but rather giving testFrame itself a public deletePanel method and having the handler call this method.

    Also, my first recommendation, that you would be far better off simplifying your test code and ridding it of netbeans generated code before posting it here, still holds.

    Much luck!
    Last edited by Fubarable; 08-10-2010 at 02:24 PM.

  8. #8
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    apologies, so i wrote a little app but Fubarable i haven't tested your suggestion but this is the code
    Main.java

    PHP Code:
    package testapplication;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    
    public class Main extends JPanel{
        JTabbedPane tabbedPane = new JTabbedPane();
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JButton delBtn = new JButton("Delete");
        public Main() {
            
            panel1.setPreferredSize(new Dimension(300, 200));
            tabbedPane.add("Panel 1", panel1);
            panel2.setPreferredSize(new Dimension(300, 200));
            tabbedPane.add("Panel 2", panel2);
            add(tabbedPane);
            panel1.add(delBtn);
            //delBtn.setText("Delete Button");
            delBtn.addActionListener(new ButtonListener());
        }
    
        public void actionPerformed(ActionEvent e) {
    
        }
        /**
         * @param args the command line arguments
         */
            private static void createShowUI() {
            JFrame frame = new JFrame("TabbedPaneDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //Add content to the window.
            frame.add(new Main(), BorderLayout.CENTER);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
        public static void main(String[] args) {
            createShowUI();
        }
    
    
    
    }
    
    class ButtonListener implements ActionListener {
      ButtonListener() {
      }
    
      public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Delete")) {
          DeletePanel deleteClass = new DeletePanel();
          deleteClass.deletePanel();
        }
      }
    }

    DeletePanel.java


    PHP Code:
    package testapplication;
    
    public class DeletePanel extends Main{
        public void deletePanel()
        {
            tabbedPane.remove(panel2);
            tabbedPane.validate();
            System.out.println("I run");
        }
    
    }
    sorry for posting generated code, but Fubarable i'll try out your suggestion
    Thank you guys for your help so far

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I think you are having a problem with Swing's EDT. I put the remove & validate in a SwingUtilities invokeLater() method and it works.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Norm View Post
    I think you are having a problem with Swing's EDT. I put the remove & validate in a SwingUtilities invokeLater() method and it works.
    I don't think that's it Norm. Previously I thought that the problem was one of misuse of inheritance and lack of adequate reference, and on review of the most recent code, I'm now sure of it. A solution is noted in my post above, but I'm not crazy about it as it fiddles directly with the fields of one class in the other. Better is to give the GUI class a removePanel method and then have the handler class call the method.

    Correction: I think that the main problem is what I mentioned above. You are absolutely correct though that any and all Swing code needs to be called on the EDT!
    Last edited by Fubarable; 08-10-2010 at 06:36 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I fiddled the code to make put all the classes in one file. I like to keep the generated output ided by the base classname since there are hundreds of files in my test folder.

    This works for me:
    Java Code:
    import java.awt.BorderLayout; 
    import java.awt.Dimension; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 
    import javax.swing.JTabbedPane; 
    import javax.swing.*;
    
    public class TestDeletePanel extends JPanel { 
        JTabbedPane tabbedPane = new JTabbedPane(); 
        JPanel panel1 = new JPanel(); 
        JPanel panel2 = new JPanel(); 
        JButton delBtn = new JButton("Delete"); 
    
        public TestDeletePanel() { 
             
            panel1.setPreferredSize(new Dimension(300, 200));
            tabbedPane.add("Panel 1", panel1); 
            panel2.setPreferredSize(new Dimension(300, 200));
            tabbedPane.add("Panel 2", panel2); 
            add(tabbedPane); 
    
            panel1.add(delBtn); 
            //delBtn.setText("Delete Button"); 
            delBtn.addActionListener(new ButtonListener());
        } 
    
        public void actionPerformed(ActionEvent e) { 
    
        } 
        /** 
         * @param args the command line arguments 
         */ 
            private static void createShowUI() { 
            JFrame frame = new JFrame("TabbedPaneDemo"); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             
            //Add content to the window. 
            frame.add(new TestDeletePanel(), BorderLayout.CENTER);
    
            //Display the window. 
            frame.pack(); 
            frame.setVisible(true); 
        } 
        public static void main(String[] args) { 
            createShowUI(); 
        } 
    
        //-------------------------------------------------
       class ButtonListener implements ActionListener { 
         ButtonListener() { 
         } 
       
         public void actionPerformed(ActionEvent e) { 
           if (e.getActionCommand().equals("Delete")) { 
             DeletePanel deleteClass = new DeletePanel(); 
       
             deleteClass.deletePanel(); 
           } 
         }
       } 
       //------------------------------------------------
       class DeletePanel extends TestDeletePanel {
           public void deletePanel()     { 
              SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                 TestDeletePanel.this.tabbedPane.remove(TestDeletePanel.this.panel2);
                 TestDeletePanel.this.tabbedPane.validate(); 
                 System.out.println("I run");
               }
             });
           } 
       
       }  
    
    
    }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The way I looked at it, his problem is stemming from his using two distinct classes, and assuming that inheritance will allow him to manipulate the objects in one class from the other class. I too combined his files, but I made one a non-inner class, like so:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DilpreetMain extends JPanel{
        JTabbedPane tabbedPane = new JTabbedPane();
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JButton delBtn = new JButton("Delete");
        public DilpreetMain() {
            
            panel1.setPreferredSize(new Dimension(300, 200));
            tabbedPane.add("Panel 1", panel1);
            panel2.setPreferredSize(new Dimension(300, 200));
            tabbedPane.add("Panel 2", panel2);
            add(tabbedPane);
            panel1.add(delBtn);
            delBtn.addActionListener(new ButtonListener());
        }
    
        public void actionPerformed(ActionEvent e) {
    
        }
    
            private static void createShowUI() {
            JFrame frame = new JFrame("TabbedPaneDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //Add content to the window.
            frame.add(new DilpreetMain(), BorderLayout.CENTER);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
        public static void main(String[] args) {
            createShowUI();
        }
    }
    
    class ButtonListener implements ActionListener {
      ButtonListener() {
      }
    
      public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Delete")) {
          DilPreetDeletePanel deleteClass = new DilPreetDeletePanel();
          deleteClass.deletePanel();
        }
      }
    } 
    
    //  **** note the inheritance here let's us use DilpreetMain fields, but the objects referred to by 
    //  **** these fields are not the same in the visible program, and so his code doesn't work
    class DilPreetDeletePanel extends DilpreetMain{
       public void deletePanel()
       {
           tabbedPane.remove(panel2);
           tabbedPane.validate();
           System.out.println("I run");
       }
    
    }

    But this can be fixed by passing references:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DilpreetMain extends JPanel {
       JTabbedPane tabbedPane = new JTabbedPane();
       JPanel panel1 = new JPanel();
       JPanel panel2 = new JPanel();
       JButton delBtn = new JButton("Delete");
    
       public DilpreetMain() {
    
          panel1.setPreferredSize(new Dimension(300, 200));
          tabbedPane.add("Panel 1", panel1);
          panel2.setPreferredSize(new Dimension(300, 200));
          tabbedPane.add("Panel 2", panel2);
          add(tabbedPane);
          panel1.add(delBtn);
          delBtn.addActionListener(new ButtonListener(this));
       }
    
       public void actionPerformed(ActionEvent e) {
    
       }
    
       private static void createShowUI() {
          JFrame frame = new JFrame("TabbedPaneDemo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          frame.add(new DilpreetMain(), BorderLayout.CENTER);
    
          frame.pack();
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          createShowUI();
       }
    }
    
    class ButtonListener implements ActionListener {
       private DilpreetMain main;
    
       ButtonListener(DilpreetMain main) {
          this.main = main;
       }
    
       public void actionPerformed(ActionEvent e) {
          if (e.getActionCommand().equals("Delete")) {
             DilPreetDeletePanel deleteClass = new DilPreetDeletePanel(main);
             deleteClass.deletePanel();
          }
       }
    }
    
    class DilPreetDeletePanel {
       private DilpreetMain main;
    
       public DilPreetDeletePanel(DilpreetMain main) {
          this.main = main;
       }
    
       public void deletePanel() {
          main.tabbedPane.remove(main.panel2);
          main.tabbedPane.validate();
          System.out.println("I run");
       }
    
    }

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Though I like this example better since the panel with the tabs handles its own deletion code, and the handler calls it:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class DeleteTabMainPanel extends JPanel {
       private TabPaneHolder dtTab = new TabPaneHolder();
       private ButtonHolder buttonHolder = new ButtonHolder(dtTab);
       
       public DeleteTabMainPanel() {
          setLayout(new BorderLayout());
          add(dtTab.getComponent(), BorderLayout.CENTER);
          add(buttonHolder.getPanel(), BorderLayout.PAGE_END);
       }
       
       private static void createAndShowUI() {
          JFrame frame = new JFrame("DelTabTest");
          frame.getContentPane().add(new DeleteTabMainPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class TabPaneHolder {
       private static final Dimension SIZE = new Dimension(400, 300);
       private static final int TOTAL_TAB_COUNT = 6;
       private JTabbedPane tabPane = new JTabbedPane();
       
       public TabPaneHolder() {
          tabPane.setPreferredSize(SIZE);
          for (int i = 0; i < TOTAL_TAB_COUNT; i++) {
             String text = "Tab " + (i + 1);
             JLabel label = new JLabel(text, SwingConstants.CENTER);
             tabPane.addTab(text, label);
          }
       }
    
       public JComponent getComponent() {
          return tabPane;
       }
       
       public void removeCurrentTab() {
          int selectedTab = tabPane.getSelectedIndex();
          if (selectedTab >= 0) {
             tabPane.removeTabAt(selectedTab);
          }
       }
    }
    
    class ButtonHolder {
       private JPanel buttonHolderPanel = new JPanel();
       private TabPaneHolder tabPaneHolder;
       
       public ButtonHolder(TabPaneHolder tabPaneHolder) {
          this.tabPaneHolder = tabPaneHolder;
          JButton removeTabBtn = new JButton("Remove Selected Tab");
          removeTabBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                removeTabAction();
             }
          });
          buttonHolderPanel.add(removeTabBtn);
       }
       
       private void removeTabAction() {
          tabPaneHolder.removeCurrentTab();
       }
    
       public JPanel getPanel() {
          return buttonHolderPanel;
       }
    }

  14. #14
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    thank you both for your input and the swing utilities invoke later method suggested by norm worked, so i'll stick with that but i'll check out the other ways and try them out
    thanks

Similar Threads

  1. Adding panels into a panel
    By kcakir in forum AWT / Swing
    Replies: 7
    Last Post: 12-07-2009, 05:19 PM
  2. resizeable panels
    By simo_mon in forum AWT / Swing
    Replies: 4
    Last Post: 07-29-2009, 01:45 AM
  3. Spliting the Panel to three panels
    By suraw in forum New To Java
    Replies: 0
    Last Post: 03-25-2009, 06:05 PM
  4. I obviously don't understand positioning panels
    By javatard in forum New To Java
    Replies: 2
    Last Post: 12-10-2008, 07:48 AM
  5. images, panels and applets
    By jamesfrize in forum Java Applets
    Replies: 3
    Last Post: 03-20-2008, 04:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •