Proszę o pomoc w zrozumieniu dlaczego TexArea nie chce wyświetlić tekstu jeśli jest wywoływany z odrębnej klasy. Napisałem prosty przykład z 4 przyciskami:
- Ustawia TexArea - i jest ok.
- Wywołuje metodę wyswietl, która wyświetla przekazany jej tekst - i jest ok.
- Uruchamia metodę wywolajIWyswietl która wywołuje metodę wyswietl... - i jest ok.
- Robi to samo co trzeci tylko tylko wywoluje metodę wywolajIWyswietl należącą do innej klasy.
public class niedziala extends javax.swing.JFrame {
public niedziala() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("jButton3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("jButton4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
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()
.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)
.addContainerGap(80, Short.MAX_VALUE))
.addComponent(jScrollPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButton4))
.addGap(28, 28, 28))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.append("tekst wywołany z przycisku 1\n");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
wyswietl("przekazany tekst");
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
wywolajIWyswietl();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
ZewnetrznaKlasa zewnKlas = new ZewnetrznaKlasa();
zewnKlas.wywolajIWyswietl();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new niedziala().setVisible(true);
}
});
}
// 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.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
void wyswietl(String txt){
jTextArea1.append(txt+" - tekst wywołany ze zmiennej 2\n");
System.out.println(txt);
}
void wywolajIWyswietl(){
wyswietl("przekazany z METODY Wewnetrznej");
}
}//**** KONIEC KLASY niedziala
class ZewnetrznaKlasa {
void wywolajIWyswietl(){
niedziala wstaw= new niedziala();
wstaw.wyswietl("tekst z poza klasy");
}
}
czy ktoś może mi powiedzieć dlaczego po wciśnięciu czwartego przycisku nie pojawia się tekst na TextArea, bo na System.out.print(); się pojawia jak trzeba.