I want to make new string to show under the first ("Verificiranje na akauntot !") text but i dont know what to do in the "jLabel3MouseClicked" class, please help me how do i paint the graphic after i press the label ? here is some code:

@Override
    public void paint(final Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Font font = new Font("Serif", Font.PLAIN, 17);
        g2.setFont(font);
        g2.setColor(Color.GREEN);
        g2.drawString("Verificiranje na akauntot !", 20, 130);
    }

    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // what should i do here ?
    }

Recommended Answers

All 11 Replies

Java will call your paint method when necessary. You can use the repaint() method to let Java know that a new paint is needed.
Instead of the literal on line 9, use a String variable. Then you can update that variable in your mouse clicked method, and call repaint()

How do i add string variable please give me some example ?

If you don't yet know how to create String variable then there's no way you should even think about Swing events or painting. You need to go back to the start and learn these topics in the correct order.

Im trying to make it like this but it gives me error

String asd = g2.drawString("Verificiranje na akauntot !", 20, 130);

sigh...
No, more like

String whatToDraw = "Verificiranje na akauntot !";

public void paint(final Graphics g) {
   ...
   g2.drawString(whatToDraw, 20, 130);
}

private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) { 
   whatToDraw = "This is the new text";
   repaint();
}

Ohh, but i meaned to appear under the first line and all the way down till the end of the "virtual display"

You could try adding the new text to whatToDraw, instead of replacing its previous value. Use a newline character "\n" to start a new line.

I'm trying like this:

String lozinka = "Vnesete ja lozinkata: \n";

    @Override
    public void paint(final Graphics g) {
        super.paintComponents(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Font font = new Font("Serif", Font.PLAIN, 17);
        g2.setFont(font);
        g2.setColor(Color.red);
        g2.drawString(lozinka+"\n", 20, 130);
    }

    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {                                     
        repaint();
        lozinka = "ASDFGHJKL";
    }

so after i click the label there is just a "repaint" and no text under the first line

That's because you replaced the original text - you didn't add it like I said

lozinka = lozinka + "\n" + "ASDFGHJKL";
commented: Only if we were listening to you more careful right? ;) +4

This code not working its just adding in the same line "ASDFGHJKL"

Oops! My mistake... I forgot that \n doesn't work in the drawString method. Sorry about that.

The simplest solution is to keep the \n characters separating the lines, and add code to the paint method to split the lines up and draw them one at a time at increasing y positions. This code illustrates that solution, you just need to adapt it to your exact situation...

int lineNo = 1;
for (String line : lozinka.split("\n")) {  
    g.drawString(line, 10, 15 * (lineNo++));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.