Issue On JTable And JComboBox
Hi Friends
I am doing a program which uses a JComboBox and JTable there are few Items in the JComboBox now i want to show some content in the JTable according to the Selection in the JComboBox.
That is if i select a item1 if that Item is equal to a String then it will display the Content in the JTable.
it does this Very well. Now my problem is, when i start the Application, it shows a Item called"item1", it is not showing any content whis is equal toItem1.
This is my code. you can Execute this and help me
import java.awt.event.*;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
publicclass ActionTestextends JFrameimplements ActionListener{
JComboBox jComboBox ;
JTable table;
JScrollPane scroll;
public ActionTest(){
String[] items ={"item1","item2","item3"};
jComboBox =new JComboBox(items);
//jComboBox.setSelectedIndex(-1);
jComboBox.addActionListener(this);
table=new JTable();
scroll=new JScrollPane(table);
JPanel pnl=new JPanel();
pnl.add(jComboBox);
pnl.add(scroll);
getContentPane().add(pnl);
pack();
setVisible(true);
}
publicstaticvoid main(String[] args){
ActionTest actionTest =new ActionTest();
actionTest.setSize(250,250);
}
publicvoid actionPerformed(ActionEvent e){
Object obj=e.getSource();
if(obj==jComboBox)
{
String it = (String)jComboBox.getSelectedItem();
if(it.equals("item1"))
{
String head[]={"Description","open price","latest price","End Date"};
String body[][]={{"Box of Biros","1.00","4.99","a","b"},
{"Blue Biro","0.10","0.14","m","n"},
{"legal pad","1.00","2.49","m","n"}};
((DefaultTableModel)table.getModel()).setDataVector(body,head);
}
elseif(it.equals("item2"))
{
String head[]={"Description","open price","latest price","End Date"};
String body[][]={{"jotgh","vimal","4.99","a","b"},
{"Blue Biro","0.10","raj","m","n"},
{"legal pad","peter","2.49","m","n"}};
((DefaultTableModel)table.getModel()).setDataVector(body,head);
}
else
{
}
}
}
}
When the Application Starts the JComboBox is with some item, so the same way the Jtable also should be with the Content which is equals to the Item Selected in the ComboBox
Please help me on this Issue
I will be Very greatful to you
Thank you for your service
Cheers
Jofin

