Bitcoin

Bitcoin
Bitcoin

JTabbedPane Java

JTABBEDPANE

Java Swing Tutorial Explaining the JTabbedPane Component. A JTabbedPane contains a tab that can have a tool tip and a mnemonic, and it can display both text and an image.
The shape of a tab and the way in which the selected tab is displayed varies by Look and Feel.

JTABBEDPANE SOURCE CODE

import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;

public class JTabbedPaneDemo extends JPanel {

 public JTabbedPaneDemo() {
  ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG");
  JTabbedPane jtbExample = new JTabbedPane();
  JPanel jplInnerPanel1 = createInnerPanel("Tab 1 Contains Tooltip and Icon");
  jtbExample.addTab("One", icon, jplInnerPanel1, "Tab 1");
  jtbExample.setSelectedIndex(0);
  JPanel jplInnerPanel2 = createInnerPanel("Tab 2 Contains Icon only");
  jtbExample.addTab("Two", icon, jplInnerPanel2);
  JPanel jplInnerPanel3 = createInnerPanel("Tab 3 Contains Tooltip and Icon");
  jtbExample.addTab("Three", icon, jplInnerPanel3, "Tab 3");
  JPanel jplInnerPanel4 = createInnerPanel("Tab 4 Contains Text only");
  jtbExample.addTab("Four", jplInnerPanel4);
  // Add the tabbed pane to this panel.
  setLayout(new GridLayout(1, 1));
  add(jtbExample);
 }
 protected JPanel createInnerPanel(String text) {
  JPanel jplPanel = new JPanel();
  JLabel jlbDisplay = new JLabel(text);
  jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
  jplPanel.setLayout(new GridLayout(1, 1));
  jplPanel.add(jlbDisplay);
  return jplPanel;
 }
 public static void main(String[] args) {
  JFrame frame = new JFrame("TabbedPane Source Demo");
  frame.addWindowListener(new WindowAdapter() {

   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
  frame.getContentPane().add(new JTabbedPaneDemo(),
    BorderLayout.CENTER);
  frame.setSize(400, 125);
  frame.setVisible(true);
 }
}
Output

Other JTabbedPane Examples
A JTabbedPane component provides quick access to the mulitiple JPanels using the same space and Window. Consider the following example, which shows the use of JTabbedPane to group information about an end-user.

JTABBEDPANE SOURCE CODE

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JTabbedPaneDemo2 extends JFrame {
        JTabbedPane t1=new JTabbedPane();
        JPanel p1,p2,p3;
        Container c;
        Container c1;
        JLabel l1,l2,l3;
        JTextField text1,text2,text3;
        JRadioButton r21,r22,r23;
        JCheckBox ch1,ch2,ch3;

        public JTabbedPaneDemo2()
        {
                p1=new JPanel();
                p2=new JPanel();
                p3=new JPanel();

                c1=getContentPane();
                p1.setLayout(new GridLayout(3,2));

                l1=new JLabel("Name");
                l2=new JLabel("Date of Birth (dd.mm.yyyy)");
                l3=new JLabel("Identification Number");

                text1=new JTextField(10);
                text2=new JTextField(10);
                text3=new JTextField(10);

                p1.add(l1);
                p1.add(text1);
                p1.add(l2);
                p1.add(text2);
                p1.add(l3);
                p1.add(text3);
                c1.add(p1);

                r21=new JRadioButton("Graduate");
                r22=new JRadioButton("Post Graduate");
                r23=new JRadioButton("Ph.D");
                p2.add(r21);
                p2.add(r22);
                p2.add(r23);

                ch1=new JCheckBox("Computers");
                ch2=new JCheckBox("Electronics");
                ch3=new JCheckBox("Marketing");
                p3.add(ch1);
                p3.add(ch2);
                p3.add(ch3);

                c=getContentPane();
                c.setLayout(new GridLayout(2,2));

                t1.addTab("Personal Information",p1);
                t1.addTab("Educational Qualification",p2);
                t1.addTab("Areas of Interest",p3);
                c.add(t1);
        }
        public static void main(String args[])
        {
                JTabbedPaneDemo2 tdemo=new JTabbedPaneDemo2();
                tdemo.setSize(300,350);
                tdemo.setVisible(true);                
        }
}

The above listing uses JTabbedPane to group together the Personal Information, Education Qualification and Areas of Interest of an individual.

C:\>sjavaspot>javac JTabbedPaneDemo.java
C:\>sjavaspot>java JTabbedPaneDemo

"Output of JTabbedPaneDemo.class"

Simple JTabbedPane Codes

import javax.swing.*;  
public class TabbedPaneExample {  
JFrame f;  
TabbedPaneExample(){  
    f=new JFrame();  
    JTextArea ta=new JTextArea(200,200);  
    JPanel p1=new JPanel();  
    p1.add(ta);  
    JPanel p2=new JPanel();  
    JPanel p3=new JPanel();  
    JTabbedPane tp=new JTabbedPane();  
    tp.setBounds(50,50,200,200);  
    tp.add("main",p1);  
    tp.add("visit",p2);  
    tp.add("help",p3);    
    f.add(tp);  
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);  
}  
public static void main(String[] args) {  
    new TabbedPaneExample();  
}
}  
The output
JAVA Jtabbedpane 1

No comments:

Post a Comment

Facebook