Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Need help on Swing

Thread view: 
learner9 - 10 Apr 2006 14:51 GMT
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*************************************************
Chris Smith - 10 Apr 2006 16:28 GMT
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

learner9 - 11 Apr 2006 14:53 GMT
Hello Chris,
yes thats right I have got it working. Thanks for your time and
expertise :)
you have a good day.
-L


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.