Results 1 to 6 of 6
  1. #1
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Post [SOLVED] Jtabbed pane hide a tab...?

    Dear Members,

    I have a tabbed pane, and in that i have 5 tabs, within each tab is a text area, and i have to check the text of the tab and if 'getText()' of tab value is 'null' or 'empty', i have to hide the tab, else i have to display the tab.

    For this I have a jtree and on selecting a tree node i will be triggering the values for the tabs, while selecting one node some of the tabs may not have the value and that time it should hide the particular tab and on selecting another tree node it may have the value, that time i have to display the tab.

    Please help me in this...

    Looking forward for your reply.....


    Regards,
    Prabhu.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    have to hide the tab, else i have to display the tab
    The JTabbedPane api has a method to enable/disable tabs: setEnabledAt(int index, boolean enabled).
    If you must hide/show tabs then I would make up way to track the tabs by index and use the removeTabAt and insertTab methods to set the tabs according to each TreeNode selection. You could make up a small helper class to track and manage the tabs.

  3. #3
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Post Jtabbed pane hiding tabs...?

    Hi Senior,

    Thank you very much for sharing your ideas. But if it is possible to provide me a sample code it would be better.

    For me the tab order should not change and the index too...

    If say suppose, i have 5 tabs namely the index are(0,1,2,3,4)

    And if any of the tabs textArea value is 'null', say the tab index '2' value is null, then i should remove the tab of index '2' and on selecting another tree node, if it has the value, i should be able to show the tab of index '2' and in that case some other tab may have null value, in that case i should hide that tab....

    Please help me in this...

    Looking forward for your reply...

    Regards,
    Prabhu.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TabManTest implements ActionListener {
        TabManager tabManager;
    
        public void actionPerformed(ActionEvent e) {
            JComboBox combo = (JComboBox)e.getSource();
            String item = (String)combo.getSelectedItem();
            String[] titles = item.split("\\,\\s");
            tabManager.setTabs(titles);
        }
    
        private JPanel getSourceComponent() {
            String[] items = {
                "two, four, three", "five, two, one, three",
                "two, one, five, four", "one, five, three"
            };
            JComboBox comboBox = new JComboBox(items);
            comboBox.addActionListener(this);
            JPanel panel = new JPanel();
            panel.add(comboBox);
            return panel;
        }
    
        private JTabbedPane getTabbedComponent() {
            JTabbedPane tabbedPane = new JTabbedPane();
            String[] titles = {
                "one", "two", "three", "four", "five"
            };
            JComponent[] children = initComponents();
            tabManager = new TabManager(tabbedPane, titles, children);
            return tabbedPane;
        }
    
        private JComponent[] initComponents() {
            Color[] colors = {
                Color.red, Color.green, Color.blue,
                Color.magenta, Color.yellow
            };
            JPanel[] panels = new JPanel[colors.length];
            for(int i = 0; i < colors.length; i++) {
                JLabel label = new JLabel(String.valueOf(i+1), JLabel.CENTER);
                label.setFont(label.getFont().deriveFont(18f));
                panels[i] = new JPanel(new BorderLayout());
                panels[i].add(label);
                panels[i].setBackground(colors[i]);
            }
            return panels;
        }
    
        public static void main(String[] args) {
            TabManTest test = new TabManTest();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getSourceComponent(), "First");
            f.add(test.getTabbedComponent());
            f.setSize(400,400);
            f.setLocation(50,50);
            f.setVisible(true);
        }
    }
    
    class TabManager {
        JTabbedPane tabbedPane;
        String[] titles;
        JComponent[] components;
    
        public TabManager(JTabbedPane tabbedPane, String[] titles,
                          JComponent[] components) {
            this.tabbedPane = tabbedPane;
            this.titles = titles;
            this.components = components;
            setTabs(titles);
        }
    
        public void setTabs(String[] titles) {
            removeTabs(titles);
            addTabs(titles);
        }
    
        private void addTabs(String[] titles) {
            String[] tabTitles = getTabTitles();
            for(int i = 0; i < titles.length; i++) {
                String title = titles[i];
                if(!contains(tabTitles, title)) {
                    insert(title);
                }
            }
        }
    
        private void insert(String title) {
            String[] tabTitles = getTabTitles();
            int index = getIndex(tabTitles, title);
            JComponent component = components[getIndex(title)];
            if(index == -1) {
                tabbedPane.addTab(title, component);
            } else {
                tabbedPane.insertTab(title, null, component, null, index);
            }
        }
    
        private int getIndex(String[] array, String insert) {
            int insertIndex = getIndex(insert);
            for(int i = 0; i < array.length; i++) {
                int index = getIndex(array[i]);
                if(insertIndex < index) {;
                    return i;
                }
            }
            return -1;
        }
    
        private int getIndex(String str) {
            for(int i = 0; i < titles.length; i++) {
                if(titles[i].equals(str)) {
                    return i;
                }
            }
            return -1;
        }
    
        private void removeTabs(String[] titles) {
            String[] tabTitles = getTabTitles();
            for(int i = tabTitles.length-1; i >= 0; i--) {
                String title = tabTitles[i];
                if(!contains(titles, title)) {
                    tabbedPane.removeTabAt(i);
                }
            }
        }
    
        private boolean contains(String[] array, String element) {
            for(int i = 0; i < array.length; i++) {
                if(array[i].equals(element)) {
                    return true;
                }
            }
            return false;
        }                
    
        private String[] getTabTitles() {
            int tabCount = tabbedPane.getTabCount();
            String[] titles = new String[tabCount];
            for(int i = 0; i < tabCount; i++) {
                titles[i] = tabbedPane.getTitleAt(i);
            }
            return titles;
        }
    }

  5. #5
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Hi Senior,

    Thank you very much. Let me work on this code and let you know the status.

    Regards,
    Prabhu.

  6. #6
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Hi Senior,

    Thank you very much. Your code helped me a lot to solve my issue.

    Regards,
    Prabhu.

Similar Threads

  1. Drag out text editor pane
    By p4ndaman in forum Eclipse
    Replies: 3
    Last Post: 07-21-2008, 05:19 PM
  2. Tabbed pane using struts 2.x......?
    By prabhurangan in forum Web Frameworks
    Replies: 1
    Last Post: 07-19-2008, 07:48 AM
  3. Layered Pane Demo
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 08:44 PM
  4. call exe in Desktop Pane
    By smartsubroto in forum New To Java
    Replies: 4
    Last Post: 06-16-2008, 08:49 AM
  5. Replies: 3
    Last Post: 01-06-2008, 12:13 AM

Posting Permissions

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