简体   繁体   中英

How do i remove contents of a JTabbedPane after a menu item action

i am making an exam software have a list of form 1,form 2 and form 3. and beside is a jtabbedpane with tabs term 1,term 2,term 3.

String[] lists={"Form 1","Form 2","Form 3,"Form 4"};    
JList forms=new JList(lists);
JTabbedPane tabs=new JTabbedPane();
//add lists to frame
//add component to tabbedpane("term1","term 2',"term 3")
//i created acionlistener for list value changed

now that all items in my jlist have different values for term1,2 and 3, how do i change the contents of the panels in the tabbedpane? i have tried

tabs.removeAll();
tabs.add(term1);
tabs.validate();

but it isnt working

I think, basically, you want to use the JTabbedPane#add(String, Component) method instead, for example

tabs.add("Term 1", term1);

And just in case, this is the code I used to test my ideas with...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTabbedPane100 {

    public static void main(String[] args) {
        new TestTabbedPane100();
    }

    public TestTabbedPane100() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTabbedPane tabbedPane;
        private int index;

        public TestPane() {
            setLayout(new BorderLayout());
            tabbedPane = new JTabbedPane();
            addTabs();

            add(tabbedPane);

            JButton btn = new JButton("Update");
            add(btn, BorderLayout.SOUTH);
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tabbedPane.removeAll();
                    addTabs();
                }
            });
        }

        protected void addTabs() {
            for (int count = 0; count < 10; count++, index++) {
                tabbedPane.add("Testing" + index, new JLabel("Testing" + index));
            }
        }
    }

}

For adding tabs use one of methods addTab(...) . For removing use removeTabAt(int i) .

Read tutorial for JTabbedPane

Next example add a new tab/ remove selected/remove all. I think it helps you:

public class Form extends JFrame {

    private static JTabbedPane tabs;
    private static int counter;

    public static void main(String[] args) {
        final JFrame frame = new JFrame("Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton add = new JButton("add");
        add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JLabel l = new JLabel(++counter+"");
                tabs.addTab("New Tab "+counter, l);
            }
        });

        JButton remove = new JButton("remove");
        remove.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                int selectedIndex = tabs.getSelectedIndex();
                if(selectedIndex != -1){
                    tabs.removeTabAt(selectedIndex);
                }
            }
        });

        JButton removeAll = new JButton("remove all");
        removeAll.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                while (tabs.getTabCount() > 0){
                    tabs.removeTabAt(0);
                }
            }
        });

        JPanel north = new JPanel();
        north.add(add);
        north.add(remove);
        north.add(removeAll);
        frame.getContentPane().add(north,BorderLayout.NORTH);

        tabs = new JTabbedPane();
        tabs.setPreferredSize(new Dimension(200,200));
        frame.add(new JScrollPane(tabs),BorderLayout.CENTER);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM