Wednesday, February 20, 2013

Implement tabbed panel using JTabbedPane

Example to implement tabbed panel using JTabbedPane.

JTabbedPane
JTabbedPane


package javajframeapplication;

import java.awt.BorderLayout;
import java.awt.GridLayout;
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.SwingUtilities;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaJFrameApplication {
    
    static public class MyTabJPanel extends JPanel {
        
        public MyTabJPanel(){
            super(new GridLayout(1, 1));
            
            JTabbedPane jTabbedPane = new JTabbedPane();
            
            JPanel jPaneA = new JPanel();
            JLabel jLabelA = new JLabel("It's Tab A");
            jPaneA.add(jLabelA);
            
            JPanel jPaneB = new JPanel();
            JButton jButton = new JButton("Button in Tab B");
            jPaneB.add(jButton);
            
            JPanel jPaneC = new JPanel();
            JButton buttonExit = new JButton("Exit button on Tab C");
            buttonExit.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }

            });
            jPaneC.add(buttonExit);
            
            jTabbedPane.addTab("Tab A", jPaneA);
            jTabbedPane.addTab("Tab B", jPaneB);
            jTabbedPane.addTab("Tab C", jPaneC);
            
            add(jTabbedPane);
        }
        
    }
    
    public static void main(String[] args) {
        
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                JFrame mainJFrame = new JFrame();
                mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                mainJFrame.setTitle("java-buddy.blogspot.com");
                mainJFrame.setSize(400, 300);

                MyTabJPanel myTabJPanel = new MyTabJPanel();
                mainJFrame.add(myTabJPanel, BorderLayout.CENTER);
                mainJFrame.setVisible(true);
            }
        });
    }
}


No comments:

Post a Comment