aspose file tools
The moose likes Swing / AWT / SWT and the fly likes problem with displaying dynamic list in Jpanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with displaying dynamic list in Jpanel" Watch "problem with displaying dynamic list in Jpanel" New topic

problem with displaying dynamic list in Jpanel

bhuvanesh kumar

Joined: Apr 14, 2009
Posts: 7
i added a dynamic list which displays the items starts with the string entered in the Jtextfield i was able to display the list properly but when the list is clicked if there is underlying focusable object like Jtextfield list is not selected. some strange error i'm getting i have attached the screenshot of what actually happened.i'm new to swings so finding difficult to fix the bugs..please help me.

private void jTextField_itemTypeKeyReleased(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
ItemMaster itemMaster = new ItemMaster();
int iCount = itemMaster.getItemNamesCount(jTextField_itemType.getText());
if (iCount == 0)
if (jList1 == null)
strItemNamesList = new String[iCount][2];
String[] strDisplayList = new String[iCount];
System.out.println("icount :" + iCount);
strItemNamesList = itemMaster.getItemNames(jTextField_itemType.getText());
System.out.println("string text :" + jTextField_itemType.getText());
for (int iLoopIndex = 0; iLoopIndex < iCount; iLoopIndex++)
strDisplayList[iLoopIndex] = strItemNamesList[iLoopIndex][1];
if (jList1 != null)

//this is the popuplist class defined by me
jList1 = new PopUpList(strDisplayList);
ilistXpos = jTextField_itemType.getX();
MDIApplication.jTextField_selected = jTextField_itemType;
iListYpos = jTextField_itemType.getY() + jTextField_itemType.getHeight();
jList1.setBounds(ilistXpos, iListYpos, jTextField_itemType.getWidth(), iCount * 20);
System.out.println("x pos:" + ilistXpos + " ypos :" + iListYpos);
catch (SQLException ex)
Logger.getLogger(addItem.class.getName()).log(Level.SEVERE, null, ex);


this is the popuplist class

* To change this template, choose Tools | Templates
* and open the template in the editor.

package UI;

import java.awt.Container;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;

* @author Administrator
public class PopUpList extends JList
public PopUpList(String strDisplayList[])

addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {

public void jList1ValueChanged(ListSelectionEvent evt) {
Container parent=getParent();


[Thumbnail for frame.JPG]

pete stein

Joined: Feb 23, 2007
Posts: 1561
That's a lot of adding and removing going on. It looks as though you're trying to make things harder than they should be. I recommend that you first and foremost read through the FAQ on how to ask questions at JavaRanch. In particular read the subsections on Tell the Details and Isolate the Problem which will help you ask questions that are easier to answer. Also read up on Use Code Tags which will make your code much easier to read.

Best of luck.

edit: looking through some of your code, you seem to be subclassing JList, and I'm not sure that this is to your advantage. Again, a paragraph or two describing what it is you are trying to achieve (and not how you are trying to achieve it) would help us greatly in figuring out the best way to do things here.
pete stein

Joined: Feb 23, 2007
Posts: 1561
I'm still trying to understand your code and your problem and it appears as if you're trying to re-invent the combo box. I wonder if you would be much better off here simply using a JComboBox instead.
pete stein

Joined: Feb 23, 2007
Posts: 1561
bhuvanesh Lk wrote:just from yesterday i'm coding in swing so don't know which is the best way.before that i worked in J2Me.first time creating a desktop application as a practice. thanks once again for your reply..can you guide me through to create with combobox control dynamically.
(sent to me via private message)

Please keep all discussions in the forum, not in email or private messages. Again, please read the forum FAQs on how to ask questions at JavaRanch including this one: Use The Forum Not Email.

The answer to this and your questions to come is to go through the Sun Swing tutorials. No one can really guide you through the creation of your app -- as volunteers we just don't have that kind of time, and in fact we will have a much easier time giving you suggestions as you gain a better understand of the rudiments of Swing. I've found that the tutorial is the best place to start, and you can find it here: Using Swing Components.

Much luck, Pete
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: problem with displaying dynamic list in Jpanel