Google Ads

Google Search Result

Sunday, November 4, 2007

JTabbedPane Program

package testClass;


import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class TabbedPane extends JFrame implements ItemListener,ActionListener{

JPanel p,jpl1,jpl2,jpl3;
JTextField t2;
JTabbedPane jtb;
JRadioButton jr1,jr2;
JComboBox jc1,jc2;
JButton button1,button2;
boolean bol = false;

public TabbedPane()
{
this.setTitle("Assignment 7");
this.setSize(450, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabs();
this.getContentPane().add(p);
this.setVisible(true);
}

public void tabs()
{
p = new JPanel();
p.setLayout(new GridLayout(1,1));
jtb = new JTabbedPane();

jpl1 = tab1();
jtb.addTab("Personal Details", jpl1);
//jtb.setSelectedIndex(0);

jpl2 = tab2();
jtb.addTab("Item Details", jpl2);

//jtb.setSelectedIndex(0);


//jtb.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

p.add(jtb);

}

public JPanel tab1()
{
JPanel t = new JPanel();

t.setLayout(new BorderLayout());


ImageIcon ii = new ImageIcon("Sunset.jpg");
JLabel j1 = new JLabel("",ii,JLabel.CENTER);


t.add(j1,BorderLayout.NORTH);

JPanel pp = new JPanel();
pp.setLayout(new GridBagLayout());
GridBagConstraints gb1 = new GridBagConstraints();

JLabel j2 = new JLabel("Name");
gb1.gridx=0;
gb1.gridy=0;
pp.add(j2,gb1);

JTextField t2 = new JTextField(20);
gb1.gridx=1;
gb1.gridy=0;
gb1.ipady=5;
gb1.ipadx=5;
gb1.weighty=0.001;

pp.add(t2,gb1);

JLabel j3 = new JLabel("Address");
gb1.gridx=0;
gb1.gridy=1;
pp.add(j3,gb1);

JTextArea t3 = new JTextArea(3,20);
gb1.gridx=1;
gb1.gridy=1;
gb1.weighty=0;
pp.add(t3,gb1);


t.add(pp,BorderLayout.CENTER);

JPanel lp = lowerpanel();
t.add(lp,BorderLayout.SOUTH);
return(t);
}

public JPanel tab2()
{

JPanel t = new JPanel();

t.setLayout(new BorderLayout());

JPanel pp = new JPanel();
pp.setLayout(new GridBagLayout());
GridBagConstraints gb1 = new GridBagConstraints();


JLabel j5 = new JLabel("Buy an Item");
gb1.gridx=0;
gb1.gridy=0;
gb1.weighty=0.1;

pp.add(j5,gb1);

jr1 = new JRadioButton("Yes");
jr1.addActionListener(this);
gb1.gridx=1;
gb1.gridy=0;
gb1.weighty=0;
gb1.weightx=0;
pp.add(jr1,gb1);

jr2 = new JRadioButton("No");
jr2.addActionListener(this);
gb1.gridx=2;
gb1.gridy=0;

gb1.weightx=0;
pp.add(jr2,gb1);

ButtonGroup g1 = new ButtonGroup();
g1.add(jr1);
g1.add(jr2);


JLabel j6 = new JLabel("Item Count");
gb1.weighty=0.1;
gb1.gridx=0;
gb1.gridy=1;

pp.add(j6,gb1);

jc1 = new JComboBox();
jc1.addItem("Not Selected");
jc1.addItem("0");
jc1.addItem("1");
jc1.addItem("2");
jc1.addItemListener(this);
gb1.gridx=1;
gb1.gridy=1;

pp.add(jc1,gb1);

JLabel j2 = new JLabel(" Item Name");

gb1.gridx=0;
gb1.gridy=2;
pp.add(j2,gb1);

t2 = new JTextField(20);
t2.setText("Please enter Item Name");

//t2.addKeyListener(this);

gb1.gridx=1;
gb1.gridy=2;
gb1.ipady=5;
gb1.ipadx=5;


pp.add(t2,gb1);



JLabel j3 = new JLabel("Item Category");

gb1.gridx=0;
gb1.gridy=3;

pp.add(j3,gb1);

JComboBox jc = new JComboBox();
jc.addItem("Category A");
jc.addItem("Category B");
jc.addItem("Category C");
jc.addItem("Category D");
gb1.gridx=1;
gb1.gridy=3;

pp.add(jc,gb1);

JLabel j4 = new JLabel(" Item Rate");
gb1.gridx=0;
gb1.gridy=4;
pp.add(j4,gb1);

JTextField t4 = new JTextField(20);
gb1.gridx=1;
gb1.gridy=4;
gb1.ipady=5;
gb1.ipadx=5;


pp.add(t4,gb1);

t.add(pp,BorderLayout.CENTER);

JPanel lp = lowerpanel();
t.add(lp,BorderLayout.SOUTH);
return(t);
}

public JPanel tab3()
{
JPanel t = new JPanel();

t.setLayout(new BorderLayout());

JPanel pp = new JPanel();
pp.setLayout(new GridBagLayout());
GridBagConstraints gb1 = new GridBagConstraints();

JLabel j2 = new JLabel("Mode of Payment");
gb1.gridx=0;
gb1.gridy=0;
gb1.weighty=0.1;
pp.add(j2,gb1);

jc2 = new JComboBox();
jc2.addItem("Cash");
jc2.addItem("Cheque");
gb1.gridx=1;
gb1.gridy=0;


pp.add(jc2,gb1);

JLabel j3 = new JLabel("Type of Order");
gb1.gridx=0;
gb1.gridy=1;
pp.add(j3,gb1);

JTextArea t3 = new JTextArea(2,20);
gb1.gridx=1;
gb1.gridy=1;
gb1.weighty=0.1;
pp.add(t3,gb1);

t.add(pp,BorderLayout.CENTER);

JPanel lp = lowerpanel();
t.add(lp,BorderLayout.SOUTH);
return(t);
}

public JPanel lowerpanel()
{
JPanel lp = new JPanel();

button1 = new JButton();
button1.addActionListener(this);
button1.setText("Generate Bill");


lp.add(button1);

button2 = new JButton("Cancel Transaction");
lp.add(button2);


return(lp);

}

public static void main(String[] args) {

new TabbedPane();
}


public void itemStateChanged(ItemEvent ie) {
String a = (String)ie.getItem();
System.out.println(a);
if(!(a=="0")){button1.setText("Generate Bill");}

if(a=="0"){
System.out.println("value of n=0");
button1.setText("Save Customer");
if(bol){
button1.setText("Save Customer");
jtb.removeTabAt(2);}
bol=false;
jr2.setSelected(true);

}

}

public void actionPerformed(ActionEvent ae) {

if(ae.getSource().equals(button1))
{
System.out.println("button1");
String a = button1.getText();
System.out.println(a);
if(a=="Save Customer"){
System.out.println("Inside Save Customer");

int response = JOptionPane.showConfirmDialog(null, "Save User For Future Refrence", "Save User",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
System.out.println("No button clicked");
} else if (response == JOptionPane.YES_OPTION) {
System.out.println("Yes button clicked");
JOptionPane.showMessageDialog(null,"User Information Save for Future Refrence","Message",JOptionPane.PLAIN_MESSAGE);
} else if (response == JOptionPane.CLOSED_OPTION) {
System.out.println("JOptionPane closed");
}

}


}
if(ae.getSource().equals(button2))
{
System.out.println("button2");


}

if(ae.getSource().equals(jr1))
{
System.out.println("Yes");
button1.setText("Generate Bill");
bol=true;
jpl3 = tab3();
jtb.addTab("Payment Details", jpl3);
jc1.setSelectedIndex(0);

}
if(ae.getSource().equals(jr2))
{
System.out.println("No");

button1.setText("Save Customer");
if(bol){
jtb.removeTabAt(2);}
bol=false;
}

}



}