I have been looking at your code for a while, but I cannot understand what you are really trying to do.

} 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);
			list.addItemListener (this);
			addComponent (p, list, 1, 0, 1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL, -1, -1);
			addComponent (p, new JLabel ("SchemaName"), 0, 0, 1, 1, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL, -1, -1);
			setContentPane (p);
			newframe.setVisible (true);
			newframe.add (p);
			newframe.setSize (2000, 2000);

What exactly do you want to happen here? You hide the window, create a JPanel, create a ComboBox, but dont add the CB to the JPanel. This way the BC will never be changed ofcourse.

Can you explain a little of what you want to happen here?

I think he adds the CB via his addComponent method - which looks like a wrapper to simplify adding components with a gridbag layout.

You are right. There was a warning on my side that the ComboBox was not used, but the warning was invalid.

I changed getArrayOfUsers to

public ArrayList<String> getArrayofUsers() {
		ArrayList<String> result = new ArrayList<String>();
		
		result.add ("one");
		result.add ("two");
		result.add ("three");

		return result;
	}

and made the main class implement ItemListener:

public class Test extends JFrame implements ActionListener, ItemListener {

and thus changed the list.addItemListener() to:

list.addItemListener (this);

The beginning of the itemListener is now:

public void itemStateChanged(ItemEvent e) {
		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);
		if (e.getStateChange () == ItemEvent.SELECTED) {
			if (e.getSource () == list) {
				JComboBox cb = list;
				final String st = (String) cb.getSelectedItem ();

				System.out.println (st);

This works. Everytime I select an option in the new window, the selected item gets printed out.

I think you should use:

someJComponent.addSomeListener(this);

and implement this Listener with the main class.
This way the code is much easier to read, and you can pass objects back and forth far easier.

In the attacked Test.zip file are all the files you sent me, with the changes. Change them to your needs :).

Thank you for the above solution.

I got to know how to handle the scenario where we need to write ItemListeners with in another ItemListener.

Like

someJComponent.addItmeListener{
   public void itemStateChanged(ItemEvent e)
        {
          .....
             ..........
             someJComponent2.addItemListener{
        public void itemStateChanged(ItemEvent e)
         {
          ..........
           ......
         }

Is this the proper way to write .
Or is there any other way to do that.Im using the above mentioned way but im not getting the proper result.
Please help me in this regard.
Thanks in advance.

You can use an anonymous inner class. For many people (including me) this is the preferred way to define listeners in general

someJComponent2.addItemListener(new ItemListener() {
  public void itemStateChanged(ItemEvent e) {
    ..........
    ......
  }
});

I don't really like that. I usually make sure my "outer" class, the one the object is in, implements ItemListener, or WhatEverOtherListener.
Then, add the Listener to the JComponent:

class MyClass implements ActionListener{
JButton myButton;

someMethod () {
// asume button has been initialised
myButton.addActionListener(this); //notice the 'this', use THIS object.
}
public void actionPerformed (ActionEvent e){
if (e.getSource().equals(myButton)){
// handle your event
}
}
}

I like implementing a class with a Listener better because of the cleaner code. It is easier to read, and less {()}{)} blocks...

Yes, both techniques are valid, and a question of personal preferences or company coding standards.
In the majority of the commercial projects I've worked on the site standards prefer the anonymous inner class, and I don't know of a GUI code generator that uses the class-implements-all-the-listeners-with-big-if/then/else constructs approach (maybe because it's harder to generate or maybe because it's readability and maintainablility doesn't scale well into complex GUIs). Where people don't use anonymous inner classes they usually use explicit inner classes for much the same reasons. But for beginners and small projects the (this) technique is OK.
On the other hand... for small projects and quick'n'dirty prototypes I use an @annotation on the component to name a method, and use a little reflection-based utility class to deal with the listening and calling the named method, but then I'm just lazy.

Here why am i getting the output two times.
And if possible try to correct it.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

public class ex extends JPanel implements ItemListener
{
	JComboBox cb= new JComboBox();
	JComboBox cb1=new JComboBox();
	public ex()
	{
		cb.addItem("Rajesh");
		cb.addItem("Sudheer");
		cb.addItem("Ramakanth");
	    cb.addItem("Fasiah");
	    cb.addItem("Monica");
	    cb.addItemListener(this);
	    add(cb);
	}
	public void itemStateChanged(ItemEvent ie)
	{
		if(ie.getStateChange()== ItemEvent.SELECTED)
		{
			add(cb1);
       	String s=(String)ie.getItem();
	    cb1.addItem(s);
	    cb1.setVisible(true);
	    cb1.addActionListener(new iActionListener());
	   // cb1.addItemListener(this);

		}
	}
	public class iActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(cb1.equals(e.getSource()))
			{      JComboBox cb=(JComboBox)e.getSource();
			    	String s= (String)cb.getSelectedItem();
			    	System.out.println(s);
			}   	 
		}
		public void focusLost(FocusEvent e)
		{
			System.out.println("Sorry focus is lost");
		}
	}
	public static void main(String args[])
	{
		JFrame frame = new JFrame();
		frame.getContentPane().add(new ex());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setSize(200,200);
	    frame.setVisible(true);
	
	}

}

Every time your itemStateChanged is called you add another instance of iActionListener to cb1, so after n calls to itemStateChanged you have n listeners attached to cb1 and they give you n copies of the output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.