Need help on Swing

L

learner9

Hello,
I am trying to populate a JList in the button click event. So I just
defined a vector and then
doing list1 = new JList(v); . But have no luck yet. When I tried in the
cmd prompt I can see all the values.

It would be great if you can try copy pasting the below code in your
(eclipse thats what I have) editor and you can see the results. I am
just parsing an XML file located the specified URL in the program.

Please help.
thanks in advance,
-L
*****************************Here is my
program***************************************************
//import java.awt.Font;
import java.awt.event.ActionListener;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
//import javax.swing.JTextPane;


/**
* @abc
*
*/
public class PubMedAssistant extends JPanel
{
private static final String urlSearch =
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?";
private static final String urlFetch =
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?";

private JPanel pane = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JList list1 = null;
private JList list2 = null;
private JScrollPane jScrollPane1 = null;
private JScrollPane jScrollPane2 = null;
private JLabel label = null;
private JLabel label1 = null;
private JLabel label2 = null;

public PubMedAssistant()
{
createAndShowGUI();
}

private void initialize()
{
pane = new JPanel();
pane.setLayout(null);

label = new JLabel("Please enter query terms:");
label.setBounds(11,20, 171, 23);

label1 = new JLabel("Abstracts returned: ");
label1.setBounds(100,100, 200, 23);

label2 = new JLabel("Abstract");
label2.setBounds(650, 100, 100,25);



pane.setLayout(null);
pane.setSize(1000, 700);
pane.add(label);
pane.add(getJTextField());
pane.add(getJButton());
pane.add(label1);
//pane.add(getList1());
pane.add(getJScrollPane1());
pane.add(label2);
//pane.add(getList2());
pane.add(getJScrollPane2());


}

/**
* This method resets jList, jList1 and jTextPane
*
* @void
*/
private void reset()
{

list1.removeAll();
list2.removeAll();
label.setText("Please enter query terms: 0");
label1.setText("Abstracts returned: 0");
label2.setText("Abstracts");
}

/**
* This method set button enable or not
*
* @void
*/
public void setButtonEnable(boolean enable)
{
jButton.setEnabled(enable);
}

/**
* This method add Abstracts to list
*
* @void
*/
public void addSequenceAbstracts()
{
label1.setText(" Abstracts returned: " + list1.getModel().getSize());

}


/**
* This method returns jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField()
{
if (jTextField == null)
{
jTextField = new JTextField();
jTextField.setText("Arabidopsis + autophagy + ATG18");
jTextField.setBounds(174, 19, 550, 28);
}
return jTextField;
}


/**
* This method returns jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton()
{
if (jButton == null)
{
jButton = new JButton();
jButton.setText("Get Abstracts");
jButton.setBounds(780, 19, 164,28);
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
//ArrayList IdsArray = new ArrayList();
Vector v = new Vector();
System.out.println("actionPerformed()"); // TODO Auto-generated
Event stub actionPerformed()
jTextField.setText("Text has been changed");
try{
Document d =
PubMedAssistant.getDocument(urlSearch,"db=pubmed&retmax=10000&term=cmyc");
NodeList nodeList = d.getElementsByTagName("IdList");
for(int j = 0 ; j < nodeList.getLength(); j++){
NodeList IdList = d.getElementsByTagName("Id");
//NodeList nList = IdList.getChildNodes();
for (int i = 0; i < IdList.getLength(); i++){
//System.out.println(IdList.item(i).getTextContent());
//getGeneRecord(IdList.item(i).getTextContent());
//String[] entries; //= {IdList.item(i).getTextContent()};
//IdsArray.add(IdList.item(i).getTextContent());
v.addElement(IdList.item(i).getTextContent());

}
}
/*for (int i = 0; i < IdList.getLength(); i++){

}*/
list1 = new JList(v);
list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list1.setSelectedIndex(0);
//list1.addListSelectionListener(this);
list1.setVisibleRowCount(5);
jScrollPane1 = new JScrollPane(list1);
list1.setVisible(true);
pane.add(list1);

}

catch (Exception ex) {
System.out.println(ex);
}
}
});

}
return jButton;
}
/**
* This method returns list1
*
* @return java.swt.List1
*/
private static Document getDocument(String urlSearch, String args){

InputStream input = null;
Document d = null;
try {
URL u = new URL (urlSearch + args);
input = u.openStream();
DocumentBuilderFactory dF = DocumentBuilderFactory.newInstance();
d = dF.newDocumentBuilder().parse(input);
}
catch (Exception e) {
System.out.println(e);
}
return d;
}

/*private JList getList1()
{
if (list1 == null)
{
list1 = new JList();
list1.setBounds(11, 130, 350, 500);
}
return list1;
} */
/**
* This method returns jScrollPane1
*
* @return javax.swing.JScrollPane1
*/
private JScrollPane getJScrollPane1()
{
jScrollPane1 = new JScrollPane();
jScrollPane1.setBounds(10, 128, 354, 505);
return jScrollPane1;
}

/**
* This method returns jList2
*
* @return java.swt.List2
*/
/*private JList getList2()
{
if (list2 == null)
{
list2= new JList();
list2.setBounds(-16, 5, 459, 500);
}
return list2;
} */
/**
* This method returns jScrollPane2
*
* @return javax.swing.JScrollPane2
*/
private JScrollPane getJScrollPane2()
{
jScrollPane2 = new JScrollPane();
jScrollPane2.setBounds(398, 126, 634, 505);
return jScrollPane2;
}


private void createAndShowGUI()
{
//
// Create and set up the window.
JFrame frame = new JFrame("PubMedAssistant");
frame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initialize();
frame.getContentPane().add(pane);

//
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
new PubMedAssistant();
}
}

***************************End of the
program*************************************************
 
C

Chris Smith

learner9 said:
Hello,
I am trying to populate a JList in the button click event. So I just
defined a vector and then
doing list1 = new JList(v); . But have no luck yet. When I tried in the
cmd prompt I can see all the values.

The problem seems to be that you don't understand the difference between
references and objects. When you say "list1 = new JList(v)" you are
creating a brand new JList object. It is entirely a different JList
from the one that you added earlier to your GUI. Since you never add
this new JList to any visible container, it never actually displays.

Instead of creating a new JList, you probably want to modify the
existing one. If your desire is to completely replace the contents,
then look up the setModel method, and the DefaultListModel class. You
can use a DefaultListModel instead of the Vector you are using now.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
L

learner9

Hello Chris,
yes thats right I have got it working. Thanks for your time and
expertise :)
you have a good day.
-L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top