the problem im faceing here in my code is
The events that i want to happen are not occuring after i select an item from the combobox,and also all other components are showing the same faulty behaviour.
the faulty behaviour that they are showing is they are not firing the events when something is selected from them but they are firing the events if i minimize and then maximize the window.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.TextField;
import java.awt.event.*;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.TransferHandler;
import javax.swing.table.DefaultTableModel;
public class JVariousExamples extends JFrame implements ActionListener
{
JFrame aFrame= new JFrame("First Frame");
public class JEvent extends JApplet
{
Image eventlogo1;
ImageIcon eventlogo2;
public void init(String r,String a){
eventlogo1 = getImage(getCodeBase(r),a);
eventlogo2 = new ImageIcon("Database.gif");
}
public URL getCodeBase(String x)
{
java.net.URL imgURL = JEvent.class.getResource(x);
return imgURL;
}
public void paint (Graphics g)
{
g.drawImage(eventlogo1,0,0,this);
g.drawImage(eventlogo1,0,120, getWidth(), getHeight()-100, this);
eventlogo2.paintIcon(this, g, 180, 0);
}
}
JButton b= new JButton("Test");
JButton b1= new JButton("Save");
JButton b2= new JButton("Cancel");
JButton b3= new JButton("Wizard");
JButton Add = new JButton("Add");
JButton Remove = new JButton("Remove");
JButton print=new JButton("Print");
JRadioButton Yes = new JRadioButton("Yes");
JRadioButton No = new JRadioButton("No");
JRadioButton LineGraph = new JRadioButton("LineGraph");
JRadioButton PieGraph = new JRadioButton("PieGraph");
JComboBox XAxis;
JComboBox YAxis=new JComboBox();
JTextField text= new JTextField(10);
//JTextField text1= new JTextField(10);
JTextField text2= new JTextField(10);
JTextField text3= new JTextField(10);
JTextField text4= new JTextField(10);
JTextField text5= new JTextField(10);
TextField text6= new TextField(10);
JLabel title = new JLabel("Database JDBC Connection :");
JLabel name = new JLabel("Name:");
JLabel jdbcdriver = new JLabel("JDBC Driver:");
JLabel jdbcurl = new JLabel("JDBC URL:");
JLabel serveradress = new JLabel("Server Address:");
JLabel database = new JLabel("Data Base:");
JLabel uasername = new JLabel("User Name:");
JLabel password = new JLabel("Password:");
JLabel display = new JLabel("GOT CONNECTED TO DATA BASE");
JCheckBox checkbox=new JCheckBox("Show Query");
JTextArea txt;
String[] ifint=new String[50];
String[] ifnotint=new String[50];
JTable table;
String[] jdbcdrivers = {"Oracle(oracle.jdbc.driver.OracleDriver)","Java","Sun"};//has to go to model class
JComboBox jdbcdriverlist = new JComboBox(jdbcdrivers);
JComboBox tablelist;
JComboBox list;
static Insets insets = new Insets(0, 0, 0, 0);
GridBagLayout c= new GridBagLayout();
GridBagConstraints d = new GridBagConstraints();
GridBagConstraints a = new GridBagConstraints();
GridBagConstraints e = new GridBagConstraints();
ArrayList<String> listofusers = null;
ArrayList<String> details = null;
JList lst= null;
JList lst1=null;
String[] somearray= new String[1000];
DefaultListModel from = new DefaultListModel();
DefaultListModel copy = new DefaultListModel();
DefaultListModel move = new DefaultListModel();
JPanel p= new JPanel(new GridBagLayout());
JFrame f = new JFrame("List Frame");
JFrame newframe= new JFrame("Second Frame");
JFrame fr= new JFrame("Final frame");
JPanel pn=new JPanel(new GridBagLayout());
JPanel pane;
final DBConnectionclass dbob= new DBConnectionclass();
protected void set_a(int z)
{
a.anchor=z;
}
protected void set_d(int x, int y)
{
d.gridx=x;
d.gridy=y;
}
private static void addComponent(Container container, Component component, int gridx, int gridy,
int gridwidth, int gridheight, int anchor, int fill,int ipadx,int ipady) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
anchor, fill, insets, ipadx,ipady );
container.add(component, gbc);
}
protected void set_e(int x, int y,int width,int height,int fill)
{
e.gridx=x;
e.gridy=y;
e.gridwidth=width;
e.gridheight=height;
e.fill=fill;
}
public JVariousExamples()
{
super("Change Message");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel= new JPanel();
panel.setLayout(c);
//Positioning all the components
set_a(GridBagConstraints.PAGE_START);
//for label
set_a(GridBagConstraints.CENTER);
a.insets=new Insets(0,0,5,0);
c.setConstraints(title,a);
panel.add(title);
//for label
set_d(1,1);
c.setConstraints(name,d);
panel.add(name);
//for text box
set_e(2,1,1,1,GridBagConstraints.HORIZONTAL);
c.setConstraints(text,e);
panel.add(text);
//for label
set_d(1,2);
c.setConstraints(jdbcdriver,d);
panel.add(jdbcdriver);
//for text
set_e(2,2,1,1,GridBagConstraints.HORIZONTAL);
c.setConstraints(jdbcdriverlist,e);
panel.add(jdbcdriverlist);
//for label
set_d(1,3);
c.setConstraints(jdbcurl,d);
panel.add(jdbcurl);
//for text
set_e(2,3,1,1,GridBagConstraints.HORIZONTAL);
text2.setFocusable(false);
c.setConstraints(text2,e);
panel.add(text2);
//for label
set_d(1,4);
c.setConstraints(serveradress,d);
panel.add(serveradress);
//for text
set_e(2,4,1,1,GridBagConstraints.HORIZONTAL);
c.setConstraints(text3,e);
panel.add(text3);
//for label
set_d(1,5);
c.setConstraints(database,d);
panel.add(database);
//for text
set_e(2,5,1,1,GridBagConstraints.HORIZONTAL);
c.setConstraints(text4,e);
panel.add(text4);
set_d(3,5);
c.setConstraints(b3,d);
panel.add(b3);
b3.addActionListener(this);
//for label
set_d(1,6);
c.setConstraints(uasername,d);
panel.add(uasername);
//for text
set_e(2,6,1,1,GridBagConstraints.HORIZONTAL);
c.setConstraints(text5,e);
panel.add(text5);
//for label
set_d(1,7);
c.setConstraints(password,d);
panel.add(password);
//for text
set_e(2,7,1,1,GridBagConstraints.HORIZONTAL);
text6.setEchoChar('*');
c.setConstraints(text6,e);
panel.add(text6);
//for button
set_d(1,8);
c.setConstraints(b,d);
panel.add(b);
b.addActionListener(this);
//for button
set_d(2,8);
c.setConstraints(b1,d);
panel.add(b1);
b1.addActionListener(this);
//for button
set_d(3,8);
c.setConstraints(b2,d);
panel.add(b2);
b2.addActionListener(this);
//for label
set_d(2,11);
display.setVisible(false);
c.setConstraints(display,d);
panel.add(display);
//panel.add(panel1);
setContentPane(panel);
///USING LAYERED PANE
panel.setBackground(Color.BLUE);
panel.setBounds(100,100,600,500);
panel.setOpaque(true);
JPanel panel1= new ImagePanel("Database.gif");
panel1.setLayout(c);
set_e(0,0,100,100,GridBagConstraints.HORIZONTAL);
JEvent j = new JEvent();
JLabel picture = new JLabel();
picture.setIcon(j.eventlogo2);
c.setConstraints(picture,a);
panel1.add(picture);
panel1.setBackground(Color.RED);
panel1.setOpaque(true);
JLayeredPane lpane= new JLayeredPane();
//adding the two layers into layeredpane
lpane.setBounds(0,0,1000,1000);
lpane.add(panel,new Integer(0),0);
lpane.add(panel1,new Integer(1),0);
aFrame.setPreferredSize(new Dimension(1000, 1000));
aFrame.add(lpane, BorderLayout.CENTER);
aFrame.pack();
aFrame.setSize(1000,1000);
aFrame.add(lpane);
aFrame.setVisible(true);
aFrame.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.out.println("Exit via windowClosing.");
System.exit(0);
}
});
}
private class listItemListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()== ItemEvent.SELECTED)
{
JComboBox cb = (JComboBox)e.getSource();
final String st = (String)cb.getSelectedItem();
ArrayList<String> listoftables = dbob.getArrayofTables(st);
String[] tablearray= new String[500];
tablearray = listoftables.toArray(tablearray);
tablelist= new JComboBox(tablearray);
addComponent(p,tablelist,3,0,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
addComponent(p,new JLabel("Tabel Name"),2,0,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
tablelist.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
newframe.validate();
newframe.setVisible(false);
newframe.setVisible(true);
JComboBox cb = (JComboBox)e.getSource();
String st1 = (String)cb.getSelectedItem();
details=dbob.getDetails(st, st1);
somearray = details.toArray(somearray);
for(int i=0;i<somearray.length;i++)
{
from.addElement(somearray[i]);
}
lst = new JList(from);
lst.setTransferHandler(new FromTransferHandler());
lst.setDragEnabled(true);
lst.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
lst.setLayoutOrientation(JList.VERTICAL);
lst.setVisibleRowCount(-1);
JScrollPane listScroller = new JScrollPane(lst);
listScroller.setPreferredSize(new Dimension(250, 80));
addComponent(p,new JLabel("Source"),0,1,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
addComponent(p,listScroller,0,2,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
lst1= new JList(move);
lst1.setTransferHandler(new ToTransferHandler(TransferHandler.COPY));
lst1.setDropMode(DropMode.INSERT);
lst.setLayoutOrientation(JList.VERTICAL);
lst.setVisibleRowCount(-1);
JScrollPane listScroller1 = new JScrollPane(lst1);
listScroller1.setPreferredSize(new Dimension(250, 80));
addComponent(p,new JLabel("Destination"),2,1,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
addComponent(p,listScroller1,2,2,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
addComponent(p,Add,1,2,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
Add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Object selected[] = lst.getSelectedValues();
int size = selected.length;
for (int i = 0; i < size; i++) {
move.add(i, selected[i]);
}
}});
addComponent(p,Remove,1,3,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
Remove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Object selected[] = lst1.getSelectedValues();
for (int i = selected.length - 1; i >= 0; --i) {
move.removeElement(selected[i]);
}
lst1.getSelectionModel().clearSelection();
}
}
);
addComponent(p,new JLabel("Inclde Graph"),0,4,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
addComponent(p,Yes,1,4,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
Yes.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
addComponent(p,new JLabel("Graph Type"),0,5,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
addComponent(p,LineGraph,1,5,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
addComponent(p,PieGraph,2,5,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
String[] axis=new String[100];
for(int i=0;i<move.getSize();i++){
axis[i]=(String) move.getElementAt(i);
}
XAxis= new JComboBox(axis);
addComponent(p,XAxis,1,6,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
try{
int x=0;
Connection con= DBConnectionclass.getOracleJDBCConnection(text2.getText(), text5.getText(),text6.getText());
for(int i=0;i<axis.length;i++){
PreparedStatement ps=con.prepareStatement("SELECT ? FROM ALL_TAB_COLS AC, DATATYPE D WHERE AC.OWNER = ? AND D.DATATYPENAME = AC.DATA_TYPE AND AC.TABLE_NAME = ? AND D.DATATYPENAME != 'XMLTYPE'");
ps.setString(1,axis[i]);
ps.setString(2, (String)list.getSelectedItem());
ps.setString(3, (String)tablelist.getSelectedItem());
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
for(int j=1;j<rsmd.getColumnCount();j++)
{
if(rsmd.getColumnTypeName(j).equalsIgnoreCase("int"))
{
ifint[x]=rsmd.getColumnName(j);
x++;
System.out.println(ifint[x]);
}
}
}
YAxis = new JComboBox(ifint);
addComponent(p,YAxis,2,6,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
}
catch(SQLException ex){System.err.println("SQLException: " + ex.getMessage());}
addComponent(p,checkbox,0,6,1,1,GridBagConstraints.FIRST_LINE_START,GridBagConstraints.HORIZONTAL,-1,-1);
checkbox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ps="SELECT "+gString(move)+
" \n FROM ALL_TAB_COLS AC, DATATYPE D " +
"\n WHERE AC.OWNER = "+(String)list.getSelectedItem()+ " AND D.DATATYPENAME = AC.DATA_TYPE AND AC.TABLE_NAME = "+(String)tablelist.getSelectedItem()+" AND D.DATATYPENAME != 'XMLTYPE'";
System.out.println(ps);
txt= new JTextArea(ps);
JScrollPane scl = new JScrollPane(txt);
scl.setPreferredSize(new Dimension(50,50));
addComponent(p,scl,0,7,1,1,GridBagConstraints.FIRST_LINE_START,GridBagConstraints.HORIZONTAL,0,0);
String[] axis1=new String[100];
for(int i=0;i<move.getSize();i++){
axis1[i]=(String) move.getElementAt(i);
}
///to show the data in the form of grid
try{
Connection con1= DBConnectionclass.getOracleJDBCConnection(text2.getText(), text5.getText(),text6.getText());
String querey="SELECT "+gString(move)+" FROM "+(String)tablelist.getSelectedItem()+" GROUP BY "+gString(move);
PreparedStatement ps1=con1.prepareStatement(querey);
ResultSet rs=ps1.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
DefaultTableModel model= new DefaultTableModel();
String cols[]=new String[rsmd.getColumnCount()];
for(int i=0;i< cols.length;++i)
{
cols[i]= rsmd.getColumnLabel(i+1);
}
model.setColumnIdentifiers(cols);
while(rs.next())
{
Object data[]= new Object[cols.length];
for(int i=0;i< data.length;++i)
{
data[i]=rs.getObject(i+1);
}
model.addRow(data);
}
table = new JTable(model);
addComponent(pn,new JScrollPane(table),1,1,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
addComponent(pn,print,1,2,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
print.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
PrintableDocument.printComponent(pn);
}
}
);
setContentPane(pn);
fr.add(pn);
fr.setVisible(true);
fr.setSize(1000,1000);
}
catch(SQLException ex){System.err.println("SQLException: " + ex.getMessage());}
}
});
}});
addComponent(p,No,2,4,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
}
}
});
}
}
}
public String gString(DefaultListModel mv){
int i=0;
String str="";
for(;i<mv.getSize()-1;i++)
{
str=str + mv.getElementAt(i)+", \n";
}
if(i<mv.getSize())
{str=str + mv.getElementAt(i);}
return str;}
public String gtString(DefaultListModel mv){
int i=0;
String str="";
for(;i<mv.getSize()-1;i++)
{
str=str +"AC."+ mv.getElementAt(i)+",";
}
if(i<mv.getSize())
{str=str+"AC."+mv.getElementAt(i);}
return str;}
public static void main(String[] args)
{
new JVariousExamples();
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source==b3)
{
text2.setFocusable(true);
text2.setText("jdbc:oracle:thin:@"+text3.getText()+":1521:"+text4.getText());
}else if (source==b)
{
dbob.url=text2.getText();
dbob.userid=text5.getText();
dbob.password=text6.getText();
DBConnectionclass.getOracleJDBCConnection(dbob.url, dbob.userid,dbob.password);
boolean show=true;
DBConnectionclass.getOracleJDBCConnection(dbob.url, dbob.userid,dbob.password).equals(show);
if(show)
{display.setVisible(true);};
}else if(source==b1)
{
aFrame.setVisible(false);
pane=new JPanel();
DBConnectionclass.getOracleJDBCConnection(text2.getText(), text5.getText(),text6.getText());
//Arrraylist for schemas
listofusers = dbob.getArrayofUsers();
String[] strarray = new String[109];
strarray=listofusers.toArray(strarray);
System.out.println(strarray.length);
list= new JComboBox(strarray);
ItemListener ict = new listItemListener();
list.addItemListener(ict);
addComponent(p,list,1,0,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
/*/*set_d(0,0);
c.setConstraints(new JLabel("Schema name"), d);
pane.add(new JLabel("Schema name"));*/
addComponent(p,new JLabel("SchemaName"),0,0,1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,-1,-1);
/*/*set_d(1,0);
c.setConstraints(list,d);
pane.add(list);
setContentPane(pane);*/
setContentPane(p);
newframe.setVisible(true);
//newframe.add(pane);
newframe.add(p);
newframe.setSize(2000,2000);
}
else if(source==b2)
{
System.exit(0);
}
}
class FromTransferHandler extends TransferHandler {
public int getSourceActions(JComponent comp) {
return COPY_OR_MOVE;
}
private int index = 0;
public Transferable createTransferable(JComponent comp) {
index = lst.getSelectedIndex();
if (index < 0 || index >= lst.getModel().getSize()) {
return null;
}
return new StringSelection((String)lst.getSelectedValue());
}
public void exportDone(JComponent comp, Transferable trans, int action) {
if (action != MOVE) {
return;
}
from.removeElementAt(index);
//somearray.removeElementAt(index);
}
}
class ToTransferHandler extends TransferHandler {
int action;
public ToTransferHandler(int action) {
this.action = action;
}
public boolean canImport(TransferHandler.TransferSupport support) {
// for the demo, we will only support drops (not clipboard paste)
if (!support.isDrop()) {
return false;
}
// we only import Strings
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return false;
}
// check if the source actions contain the desired action -
// either copy or move, depending on what was specified when
// this instance was created
boolean actionSupported = (action & support.getSourceDropActions()) == action;
if (actionSupported) {
support.setDropAction(action);
return true;
}
// the desired action is not supported, so reject the transfer
return false;
}
public boolean importData(TransferHandler.TransferSupport support) {
// if we cannot handle the import, say so
if (!canImport(support)) {
return false;
}
// fetch the drop location
JList.DropLocation dl = (JList.DropLocation)support.getDropLocation();
int index = dl.getIndex();
// fetch the data and bail if this fails
String data;
try {
data = (String)support.getTransferable().getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e) {
return false;
} catch (java.io.IOException e) {
return false;
}
JList list = (JList)support.getComponent();
DefaultListModel model = (DefaultListModel)list.getModel();
model.insertElementAt(data, index);
Rectangle rect = list.getCellBounds(index, index);
list.scrollRectToVisible(rect);
list.setSelectedIndex(index);
list.requestFocusInWindow();
return true;
}
}
class FromTransferHandler1 extends TransferHandler {
public int getSourceActions(JComponent comp) {
return COPY_OR_MOVE;
}
private int index = 0;
public Transferable createTransferable(JComponent comp) {
index = lst1.getSelectedIndex();
if (index < 0 || index >= lst1.getModel().getSize()) {
return null;
}
return new StringSelection((String)lst1.getSelectedValue());
}
public void exportDone(JComponent comp, Transferable trans, int action) {
if (action != MOVE) {
return;
}
move.removeElementAt(index);
//somearray.removeElementAt(index);
}
}
}