setText problem

0

Witam. Mam problem z obsługą GUI w Javie. Piszę czytnik RSS, ale jak do tej pory potrafi on wyświetlić wyniki w NetBeansowym output'cie, natomiast przesłanie danych do textArea leży [glowa]

        public void writeNews() {
		try {

			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			URL u = new URL("http://wiadomosci.onet.pl/2,kategoria.rss");

			Document doc = builder.parse(u.openStream());

			NodeList list = doc.getElementsByTagName("item");

			for(int i=0;i<list.getLength();i++) {

				Element element = (Element)list.item(i);
                            
                               
                                
                                System.out.println("Tytuł: " + getElementValue(element,"title"));
				System.out.println("Link: " + getElementValue(element,"link"));
				System.out.println("Data publikacji: " + getElementValue(element,"pubDate"));
				System.out.println("Streszczenie: " + getElementValue(element,"description"));
				System.out.println();
			}//for
		}//try
		catch(Exception ex) {

		}

	}

Zamiast System.out.println dawałem jTextArea1.setText(getElementValue(element,"title"), i nic, jtextArea1 pozostało puste.

Pisałem wcześniej w C/C++ na konsolę windowsa i ( trochę ) linuxa, ale okienek i Javy dopiero się uczę, jeżeli problem jest tak trywialny, że nadaje się do działu Newbie, to przepraszam ;]

0

Pokaż kod tworzący te JTextArea. Zapewne do innego piszesz, a inne wyświetlasz.

0

hmm? całe okienka są tworzone przez Netbeans - przeciągnij i upuść ;]

[[...]
jTextArea1 = new javax.swing.JTextArea();
[...]
 jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

Wklejałem tylko te miejsca z kodu wygenerowanego, gdzie pojawił się textArea1 ( jest tylko jeden textarea w programie).
Nie bardzo wiem skąd się wziął jScrollPanel...
Poza tym gdy dopiszę akcję do buttona

 private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
                jTextArea1.setText("Shit!");
    
    }                   

Działa ;p

0

Zmień metodę setText() na append(). Przy setText() masz w JTextArea tylko to co zapisało ostatnie wywołanie.

0

Nie zadziałało. Wrzucam to co mam do tej pory. Może być trochę bałaganu ;]

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

/*
 * RSS_UI.java
 *
 * Created on 2009-10-23, 11:59:37
 */

package my.rssver1;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
//Defines the API to obtain DOM Document instances from an XML document.
//Using this class, an application programmer can obtain a Document from XML.
import javax.xml.parsers.DocumentBuilderFactory;
//defines a factory API that enables applications to obtain a parser
//that produces DOM object trees from XML documents
import org.w3c.dom.CharacterData;
//Provides the interfaces for the Document Object Model (DOM)
//which is a component API of the Java API for XML Processing.
import org.w3c.dom.Document;
//The Document interface represents the entire HTML or XML document. Conceptually,
//it is the root of the document tree, and provides the primary access to the document's data.
import org.w3c.dom.Element;
//The Element interface represents an element in an HTML or XML document.
import org.w3c.dom.Node;
//The Node interface is the primary datatype for the entire Document Object Model.
//It represents a single node in the document tree
import org.w3c.dom.NodeList;
//The NodeList interface provides the abstraction of an ordered collection of nodes,
//without defining or constraining how this collection is implemented

/**
 *
 * @author Sparrow
 */
public class RSS_UI extends javax.swing.JFrame {

    private static RSS_UI instance = null;


	public static RSS_UI getInstance() {
		if(instance == null) {
			instance = new RSS_UI();
		}
		return instance;
	}


        public void writeNews() {
		try {

			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			URL u = new URL("http://wiadomosci.onet.pl/2,kategoria.rss");

			Document doc = builder.parse(u.openStream());

			NodeList list = doc.getElementsByTagName("item");

			for(int i=0;i<list.getLength();i++) {

				Element element = (Element)list.item(i);


                                jTextArea1.append("Shit!");
                                jTextArea1.append(getElementValue(element,"title"));
                                System.out.println("Tytuł: " +getElementValue(element,"title"));
				System.out.println("Link: " +getElementValue(element,"link"));
				System.out.println("Data publikacji: " + getElementValue(element,"pubDate"));
				System.out.println("Streszczenie: " + getElementValue(element,"description"));
				System.out.println();
			}//for
		}//try
		catch(Exception ex) {

		}

	}

        private String getCharacterDataFromElement(Element e) {
			try {
				Node child = e.getFirstChild();
			        CharacterData cd = (CharacterData) child;
				return cd.getData();

			}
			catch(Exception ex) {

			}
			return "";
		}


     private String getElementValue(Element parent,String label)
                {
             return  getCharacterDataFromElement((Element)parent.getElementsByTagName(label).item(0));
		}


//GetElementsByTagName - Returns a list of elements with the given tag name.
//The subtree underneath the specified element is searched, excluding the element itself.


    /** Creates new form RSS_UI */
    public RSS_UI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton5.setText("Wyjście");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("pogoda.onet.pl");

        jButton7.setText("pogoda.onet.pl");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Kraj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Świat");

        jButton3.setText("Wiadomości dnia");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Sport");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton8.setText("Czyść");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jTextField1.setText("jTextField1");

        jTextField2.setText("jTextField1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton8))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 423, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                        .addComponent(jButton5)
                        .addGap(63, 63, 63)))
                .addGap(42, 42, 42))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton6)
                    .addComponent(jButton7)
                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 21, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
       RSS_UI reader = RSS_UI.getInstance();
        reader.writeNews();

}                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
}                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
}                                        

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
          System.exit(0);

    }                                        

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
                jTextArea1.setText("Shit!");
    
    }                                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RSS_UI().setVisible(true);
            }
        });
//RSS_UI reader = RSS_UI.getInstance();
//		reader.writeNews();

    
		
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration

}

0

Jest tak jak napisałem na początku, do innego JTextArea piszesz, a inne wyświetlasz. Zmień poniższą funkcje.funkcję

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
       //RSS_UI reader = RSS_UI.getInstance();
        //reader.writeNews();
        writeNews();

} 
0

Może wyjaśnię skąd się wzięły w Twoim programie dwa pola tekstowe. Otóż w twoim programie są dwa obiekty klasy RSS_UI. Jeden jest tworzony w funkcji main()

            public void run() {
                new RSS_UI().setVisible(true);
            }

i ten był wyświetlany. W szczególności jego pola tekstowe były widoczne. Drugi jest tworzony tu

                if(instance == null) {
                        instance = new RSS_UI();
                }

i właśnie do składowej jTextArea1 tego (niewidocznego) obiektu pisałeś.

1 użytkowników online, w tym zalogowanych: 0, gości: 1