Java. Jak coś napisać w jTextArea z innej klasy. NetBeens.

0

Uczę się dopiero 2 tyg. javy. W funkcji main utworzyłem wątek:

        Channels thr = new Channels();
        thr.start();

W pliku Channels.java mam:


public class Channels extends Thread 
{

    @Override
    public void run() {

        getChannels();  

    }

 void getChannels()
   {  

       String text = "";

       for(int i  = 1; i <= 2; i++)
           text += get_page(i);

   }

    String get_page(int page)
    {   
        getHtml w = new getHtml(); // getHtml - pobiera kod strony

        String url = "https://looknij.in/telewizja-online/strona" + page;
        return w.getPage(url);
    }

}

Jak wydrukować zmienną text z funkcji getChannels W jTextArea1? jTextArea1 jest stworzony za pomocą designera.

0

Musisz do tego swojego wątku przekazać referencje (np. w konstruktorze) do tej kontrolki albo przynajmniej do okienka które ja zawiera.

0

Ale jak to zrobić dokładnie?

0

Przenieś tworzenie wątku do konstruktora klasy z oknem

        Channels thr = new Channels(this);
        thr.start();
public class Channels extends Thread 
{
    private KlasaZOknem owner = null;
    public Channesl(KlasaZOknem owner)
    {
        this.owner = owner;
0

Jak wypisać teraz text?

owner.jTextArea1.setText(text);

nie przechodzi.

0

Co znaczy "nie przechodzi"?
Może brak dostępu bo pole jTextArea1 jest private?

0

Dokładnie tak. Napisałem funkcję

  public JTextArea getJTextArea()
    {
        return this.jTextArea1;
    }

w klasie z oknem, ale

owner.getTextArea().setText(Text);

też nie przechodzi.

0

Powtórzę pytanie:
Co znaczy "nie przechodzi"?

0

Nie może znaleźć symbolu metody getJTextArea() w zmiennej owner typu JFrameForm.

0

Funkcję getJTextArea() umieściłem poza funkcją main.

0

Porobiłeś literówki, metodę nazywasz czasami getTextArea , a czasami getJTextArea.

0

Tylko tutaj. W programie mam dobrze.
JFrameForm:

public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
              
            public void run() {
                JFrameForm jframe = new JFrameForm();
                jframe.getContentPane().setBackground(Color.BLACK);
                jframe.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("app.png")));
                
                jframe.setVisible(true);
                              
            }
         
        });
        
        Channels thr = new Channels(this);
        thr.start();

     
    }
    public JTextArea getJTextArea()
    {
        return this.jTextArea1;
    }

Channels:

public class Channels extends Thread 
{
    private JFrameForm owner = null;
    
    public void run(JFrameForm jf) {
       
        this.owner = jf;
        getChannels();  
        
    }

   
    void getChannels()
   {  
       
       String text = "";
       
       for(int i  = 1; i <= 2; i++)
           text += get_page(i);
       
       owner.getJTextArea().setText(text);
   }
   
0

Przecież napisałem, że masz przenieś wiersz

Channels thr = new Channels(this);

z metody main do konstruktora klasy z oknem

0

Jakby wyglądał taki konstruktor?

0

Z konstruktora nie wystartuje wątku.

0

Ostatnie pytanie. Gdzie dać wywołanie funkcji getChannels()?

0

Może jako obsługę kliknięcia w przycisk.

0

Chciałbym, aby funkcja wywołała się automatycznie po starcie wątku. Coś jak funkcja FormCreate w delphi.

0

W metodzie main zostaw to

        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                new JFrameForm();
            }

        });

i dopisz konstruktor

    public JFrameForm()
    {
        getContentPane().setBackground(Color.BLACK);
                //jframe.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("app.png")));
        jTextArea1 = new JTextArea(8,60);
        add(jTextArea1);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);        
        Channels thr = new Channels(this);
        thr.start();
    }

I przepisz kod dokładnie, poprzednio wprowadzałeś bezsensowne modyfikacje.

0

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at JFrameForm.<init>(JFrameForm.java:15)

0

W metodzie run przy newJFrameForm pisze New instance ignored.

0

A czy Twoja klas JFrameForm dziedziczy po JFrame?

0

public class JFrameForm extends javax.swing.JFrame {

0

Użyłem NetBeens designera.

0

Designer w NetBeans to samo zło.
Prosta, działająca wersja wygląda tak:

import javax.swing.*;
import java.awt.*;
public class JFrameForm extends JFrame
{
    JTextArea jTextArea1 = new JTextArea(8,60);
    public static void main(String args[])
    {
        try 
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
            {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } 
        catch(Exception ex) 
        {}

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                new JFrameForm();
            }

        });

    }
    public JFrameForm()
    {
        getContentPane().setBackground(Color.BLACK);
                //jframe.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("app.png")));
        jTextArea1 = new JTextArea(8,60);
        add(jTextArea1);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);        
        Channels thr = new Channels(this);
        thr.start();
    }
    public JTextArea getJTextArea()
    {
        return this.jTextArea1;
    }
}

public class Channels extends Thread 
{
    private JFrameForm owner = null;
    public Channels(JFrameForm owner)
    {
        this.owner = owner;
    }

    public void run() 
    {

        getChannels();  

    }

    void getChannels()
    {  

        String text = "";

        for(int i  = 1; i <= 2; i++)
           text += getPage(i) + "\n";
        owner.getJTextArea().setText(text);
    }
    String getPage(int page)
    {   
        return "dupa " + page;
    }   
}```
0

Skoro nie netbeens, to co? JBuilder? Dzięki za poświęcony mi czas. Przyda się innym, bo widzę w googlach, że wielu ma ten sam problem.

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