user4441930
user4441930

Reputation:

Why can't I setText this jTextField?

So I have been writting this code, but for 2 days I haven't been able to figure out the problem. This is the fragment of the code where the problem is:

public void updatee(String gatavs){
    lauks.setText(gatavs);
    System.out.println(gatavs+"?!");
}
public void setup(int garums, String vards){

    labul.setText(vards);
    int i=1;
    String a="-";
    while(i<=garums){
        lauks.setText(a);
        a=a+"-";
        i++;
    }
}

This is in a JFrame class. At the start I call the method setup from another class and it sets the text in lauks textfield just fine, but later when i call the method updatee it doesn't settext lauks to the other string. I even put the println so i could check if it has the string gatavs, and it printed to the console just fine. What could be the problem here?

EDIT:Here is the full JFrame class:

package oliverakaratavas;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MyFrame extends JFrame {
    int startif = 0;
    OliveraKaratavas par = new OliveraKaratavas();

    public MyFrame() {
        initComponents();
    }

    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lauks = new javax.swing.JTextField();
        a = new javax.swing.JButton();
        b = new javax.swing.JButton();
        c = new javax.swing.JButton();
        d = new javax.swing.JButton();
        e = new javax.swing.JButton();
        f = new javax.swing.JButton();
        g = new javax.swing.JButton();
        h = new javax.swing.JButton();
        i = new javax.swing.JButton();
        j = new javax.swing.JButton();
        k = new javax.swing.JButton();
        l = new javax.swing.JButton();
        m = new javax.swing.JButton();
        n = new javax.swing.JButton();
        o = new javax.swing.JButton();
        p = new javax.swing.JButton();
        r = new javax.swing.JButton();
        s = new javax.swing.JButton();
        t = new javax.swing.JButton();
        u = new javax.swing.JButton();
        v = new javax.swing.JButton();
        z = new javax.swing.JButton();
        labul = new javax.swing.JLabel();

        jLabel2.setText("jLabel2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Karatavas");

        lauks.setEditable(false);
        lauks.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        lauks.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        a.setText("A");
        a.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aActionPerformed(evt);
            }
        });

        b.setText("B");
        b.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bActionPerformed(evt);
            }
        });

        c.setText("C");
        c.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cActionPerformed(evt);
            }
        });

        d.setText("D");
        d.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dActionPerformed(evt);
            }
        });

        e.setText("E");
        e.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eActionPerformed(evt);
            }
        });

        f.setText("F");
        f.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fActionPerformed(evt);
            }
        });

        g.setText("G");
        g.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gActionPerformed(evt);
            }
        });

        h.setText("H");
        h.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hActionPerformed(evt);
            }
        });

        i.setText("I");
        i.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iActionPerformed(evt);
            }
        });

        j.setText("J");
        j.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jActionPerformed(evt);
            }
        });

        k.setText("K");
        k.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kActionPerformed(evt);
            }
        });

        l.setText("L");
        l.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lActionPerformed(evt);
            }
        });

        m.setText("M");
        m.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mActionPerformed(evt);
            }
        });

        n.setText("N");
        n.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nActionPerformed(evt);
            }
        });

        o.setText("O");
        o.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oActionPerformed(evt);
            }
        });

        p.setText("P");
        p.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pActionPerformed(evt);
            }
        });

        r.setText("R");
        r.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rActionPerformed(evt);
            }
        });

        s.setText("S");
        s.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sActionPerformed(evt);
            }
        });

        t.setText("T");
        t.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tActionPerformed(evt);
            }
        });

        u.setText("U");
        u.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uActionPerformed(evt);
            }
        });

        v.setText("V");
        v.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vActionPerformed(evt);
            }
        });

        z.setText("Z");
        z.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zActionPerformed(evt);
            }
        });

        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.TRAILING)
                                        .addComponent(i, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(a))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(
                                                layout.createSequentialGroup()
                                                        .addGroup(
                                                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                                        .addGroup(
                                                                                layout.createSequentialGroup()
                                                                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                        .addGap(127, 127, 127))
                                                                        .addGroup(
                                                                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                                        .addGroup(
                                                                                                layout.createSequentialGroup()
                                                                                                        .addComponent(j, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                        .addComponent(k, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                        .addComponent(l, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                        .addComponent(m)
                                                                                                        .addGap(4, 4, 4)
                                                                                                        .addComponent(n, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                        .addComponent(o, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                        .addComponent(p, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                                        .addGroup(
                                                                                                layout.createSequentialGroup()
                                                                                                        .addGroup(
                                                                                                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                                                                                        .addGroup(
                                                                                                                                layout.createSequentialGroup()
                                                                                                                                        .addComponent(b, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                                        .addPreferredGap(
                                                                                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                                        .addComponent(c, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                                        .addPreferredGap(
                                                                                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                                        .addComponent(d, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                                        .addPreferredGap(
                                                                                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                                        .addComponent(e, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                                        .addPreferredGap(
                                                                                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                                        .addComponent(f, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                                        .addPreferredGap(
                                                                                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                                        .addComponent(g, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                                                                        .addComponent(lauks))
                                                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                        .addComponent(h, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))).addContainerGap())
                                        .addGroup(
                                                layout.createSequentialGroup().addComponent(r, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(s, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(t, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(u, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(v, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(z, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(labul, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)))));

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, r, s, t, u, v, z });

        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(lauks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(a, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(b, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(c, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(d, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(e, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(f, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(g, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(h, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(
                                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(i, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(j, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(k, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(l, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(n, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(o, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(p, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(m, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(r, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(s, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(t, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(u, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(v, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(z, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(labul, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)));

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] { a, b, c, d, e, f, g, h, i, j, k, l, n, o, p, r, s, t, u, v, z });

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

    private void tActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'T';
        par.parbaud1(b, word, gatavs);
    }

    private void uActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'U';
        par.parbaud1(b, word, gatavs);
    }

    private void vActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'V';
        par.parbaud1(b, word, gatavs);
    }

    private void zActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'Z';
        par.parbaud1(b, word, gatavs);
    }

    private void aActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'A';
        par.parbaud1(b, word, gatavs);
    }

    private void bActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'B';
        par.parbaud1(b, word, gatavs);
    }

    private void cActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'C';
        par.parbaud1(b, word, gatavs);
    }

    private void dActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'D';
        par.parbaud1(b, word, gatavs);
    }

    private void eActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'E';
        par.parbaud1(b, word, gatavs);
    }

    private void fActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'F';
        par.parbaud1(b, word, gatavs);
    }

    private void gActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'G';
        par.parbaud1(b, word, gatavs);
    }

    private void hActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'H';
        par.parbaud1(b, word, gatavs);
    }

    private void iActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'I';
        par.parbaud1(b, word, gatavs);
    }

    private void jActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'J';
        par.parbaud1(b, word, gatavs);
    }

    private void kActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'K';
        par.parbaud1(b, word, gatavs);
    }

    private void lActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'L';
        par.parbaud1(b, word, gatavs);
    }

    private void mActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'M';
        par.parbaud1(b, word, gatavs);
    }

    private void nActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'N';
        par.parbaud1(b, word, gatavs);
    }

    private void oActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'O';
        par.parbaud1(b, word, gatavs);
    }

    private void pActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'P';
        par.parbaud1(b, word, gatavs);
    }

    private void rActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'R';
        par.parbaud1(b, word, gatavs);
    }

    private void sActionPerformed(java.awt.event.ActionEvent evt) {
        String word = labul.getText();
        String gatavs = lauks.getText();
        char b = 'S';
        par.parbaud1(b, word, gatavs);
    }

    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 (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrame().setVisible(true);
            }
        });
    }

    public void updatee(String gatavs) {
        lauks.setText(gatavs);
        System.out.println(gatavs + "?!");
    }

    public void setup(int garums, String vards) {
        labul.setText(vards);
        int i = 1;
        String a = "-";
        while (i <= garums) {
            lauks.setText(a);
            a = a + "-";
            i++;
        }
    }

    // Variables declaration - do not modify
    private javax.swing.JButton a;
    private javax.swing.JButton b;
    private javax.swing.JButton c;
    private javax.swing.JButton d;
    private javax.swing.JButton e;
    private javax.swing.JButton f;
    private javax.swing.JButton g;
    private javax.swing.JButton h;
    private javax.swing.JButton i;
    private javax.swing.JButton j;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton k;
    private javax.swing.JButton l;
    private javax.swing.JLabel labul;
    private javax.swing.JTextField lauks;
    private javax.swing.JButton m;
    private javax.swing.JButton n;
    private javax.swing.JButton o;
    private javax.swing.JButton p;
    private javax.swing.JButton r;
    private javax.swing.JButton s;
    private javax.swing.JButton t;
    private javax.swing.JButton u;
    private javax.swing.JButton v;
    private javax.swing.JButton z;
    // End of variables declaration
}

And here is the class with the main method that calls the method updatee:

package oliverakaratavas;

public class OliveraKaratavas {

    public static void main(String[] args) {
        Vards v = new Vards();
        v.nofaila();
    }

    public void parbaud1(char b, String word, String gatavs) {
        int k = 0;
        MyFrame j = new MyFrame();
        while (k < word.length()) {
            char ch = word.charAt(k);
            if (ch == b) {
                gatavs = gatavs.substring(0, k) + ch + gatavs.substring(k + 1);
            }
            k++;
        }
        j.updatee(gatavs);
        System.out.println(gatavs);

    }
}

Tell me if you need anything else!

FINAL EDIT: The problem was answered, thanks to tmarven!

Upvotes: 0

Views: 1993

Answers (2)

Ubica
Ubica

Reputation: 1051

You have a lot of design issues and you should consider starting your project from scratch. I am not an expert in Swing, but you might wanna consider something like this:

Start with a main class that will be used only for starting up your program and making some ground level configurations:

public class WPMAIN {

    // Entry point of your program (should be one and only)
    public static void main(String[] args) {
        // Creation of the frame and setting it visible
        WPFrame frame = new WPFrame();
        frame.setVisible(true);
    }

}

Then in the JFrame, try to minimize redundant code. If you have tons of buttons, that do the same thing, but just a little bit differently, make a method that contains all the code that remains the same:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

@SuppressWarnings("serial")
public class WPFrame extends JFrame {

    private JPanel contentPane;
    private JButton A;
    private JButton B;

    public WPFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        A = new JButton("A");
        setupButton(A);
        B = new JButton("B");
        setupButton(B);

        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(
                gl_contentPane.createSequentialGroup().addComponent(A, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.RELATED)
                        .addComponent(B, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addContainerGap(237, Short.MAX_VALUE)));
        gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(
                gl_contentPane
                        .createSequentialGroup()
                        .addGroup(
                                gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(A, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(B, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addContainerGap(227, Short.MAX_VALUE)));
        contentPane.setLayout(gl_contentPane);
    }

    public void setupButton(final JButton init) {
        init.setMargin(new Insets(2, 2, 2, 2));
        init.setSize(25, 25);
        init.setPreferredSize(new Dimension(25, 25));
        init.setMinimumSize(new Dimension(25, 25));
        init.setMaximumSize(new Dimension(25, 25));
        init.setBorder(new LineBorder(new Color(0, 0, 0)));

        // Action listener that is separate for each button you create,
        // but like this you control all the buttons' actions at once
        init.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(init.getText());
                JOptionPane.showMessageDialog(contentPane, "You pressed " + init.getText());
            }
        });
    }
}

Note that this is only a suggestion, I don't claim this is the right way to do it, or the best way to do it... Just that it will give you more control with the code you are writing. You can take these classes, and run them to see how it works. It's reasonably simple and maybe you can try to build on it.

Upvotes: 1

tmarwen
tmarwen

Reputation: 16374

Looking into your source code, it seems like you have a lot of issues mixed together which does not only concern the setup() method (note that I can't even see a call to this method in the main OP):

  • The major one for me is your parbaud1 signature and body, where you are creating an instance of JFrame (your own frame implementation) and calling the parbaud1 method on that instance object:

    JFrame j = new JFrame();
    //...
    jFrame.updatee(gatavs);
    

This will almost result on a new object created each time one of you action listeners delegate the event handling logic to this method having no effective result on your main frame since your code is applied on a different object from you visual one.

The soltution would then be to change the method signature to hold a JFrame instance, and pass the current JFrame instance in each call in your action listener logic:

public void parbaud1(JFrame jFrame, char b, String word, String gatavs)
{
  int k = 0;
  //    JFrame j = new JFrame();
  while (k < word.length())
  {
    char ch = word.charAt(k);
    if (ch == b)
    {
      gatavs = gatavs.substring(0, k) + ch + gatavs.substring(k + 1);
    }
    k++;
  }
  jFrame.updatee(gatavs); // Update the parmaeter argument and don't create a new one
  System.out.println(gatavs);
}

Then calling it as follows:

private void aActionPerformed(java.awt.event.ActionEvent evt)
{
  String word = labul.getText();
  String gatavs = lauks.getText();
  char b = 'A';
  par.parbaud1(this, b, word, gatavs); // Use the current JFrame instance
}
  • The second issue may be that no text is appended to the gatavs object since the main condition k < word.length() is never reached, why?.

Simply said because following the method chaining the word will reference the labul object which is never filled with text (word.lenght() will be always 0), so you may need to initialize that label with some text following your needs.

Fixing both upper listing would help wordarounding the updatee method issue, but you still will fall into others reading your code such as StringOutOBoundExceptions, e.g:

gatavs = gatavs.substring(0, k) + ch + gatavs.substring(k + 1); // If the first character is matched gatavs.substring(k+1) will fail because you gatavs String which value come from `lauks` is already empty because the text field is not even editable.

Here you can a working copy of your code.

Upvotes: 0

Related Questions