JTabbedPane has become on of the most important element of the GUIs ever since it was introduced. So, did Sun incorporated this into Swing. Java is known for its simplicity, so you can create JTabbedPane easily. While working with JTabbedPane, you should know ten important methods which I'll discuss in this example.
The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)
import javax.swing.*;
import java.awt.*;
class JTabbedPaneExample extends JFrame
{
JTabbedPane t;
JPanel p1,p2,p3,p4;
ImageIcon i1=new ImageIcon("imageicon.png");
public JTabbedPaneExample()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
setTitle("JTabbedPane Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
t=new JTabbedPane();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
// Add some items, 1st tab
for(int i=1;i<=5;i++)
p1.add(new JButton("Button "+i));
// 2nd tab
JList<String> l=new JList<>(new String[]{"Apple","Mango","Orange","Strawberry","Grape"});
p2.add(l);
// A JComboBox, 3rd tab
JComboBox<String> jc=new JComboBox<>();
jc.addItem("Vanilla");
jc.addItem("Chocolate");
jc.addItem("Butterscotch");
jc.addItem("Strawberry");
p3.add(jc);
// 4th tab
JTextArea jt=new JTextArea(20,30);
p4.add(new JScrollPane(jt));
// Add all tabs, come oN!!
t.addTab("Buttons",p1);
t.addTab("List",i1,p2);
t.addTab("ComboBox",i1,p3,"I am tooltip!");
t.addTab("JTextArea",p4);
// Set mnemonics so that tab can
// be activated using alt+mnemonic
t.setMnemonicAt(0,'T');
t.setMnemonicAt(1,'L');
t.setMnemonicAt(2,'C');
t.setMnemonicAt(3,'J');
// Set icon at index
t.setIconAt(0,i1);
// Set background at 0 index
t.setBackgroundAt(0,Color.LIGHT_GRAY);
t.setForegroundAt(1,Color.BLUE);
// Set title at
t.setTitleAt(0,"Button");
// Set tooltip at
t.setToolTipTextAt(1,"I am tooltip!");
// select tab with 2nd index
t.setSelectedIndex(2);
// Underline third t in Buttons (0th tab)
t.setDisplayedMnemonicIndexAt(0,3);
// Set tab placement to bottom
t.setTabPlacement(JTabbedPane.BOTTOM);
add(t);
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
new JTabbedPaneExample();
}
}
The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)
No comments:
Post a Comment