+ Reply to Thread
Results 1 to 5 of 5

Thread: Repainting elements in a panel

  1. #1
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Repainting elements in a panel

    How can I repaint elements in a panel? I was told all modifications to an element have to be done before being added to a panel. How can you repaint an element after it has been added to panel, then added to a window? Example code bellow.

    Code:
    package keylogger;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class Main {
    
        static JLabel text;
        static JFrame window;
    
        public static void main(String[] args) {
            window = new JFrame("Keylogger");
            window.setSize(400, 100);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setLayout(new FlowLayout(FlowLayout.CENTER));
            text = new JLabel();
            window.add(new JScrollPane(text));
            window.addKeyListener(new Writter());
            window.setVisible(true);
        }
    
    }
    Code:
    package keylogger;
    
    import java.awt.event.*;
    
    public class Writter extends KeyAdapter {
    
        public void keyTyped(KeyEvent event) {
            Main.text.setText(Main.text.getText() + event.getKeyChar());
            Main.window.repaint();
        }
    
    }
    Outcome: The window appears without errors but the text typed does not appear in the window.

    It is acknowledged that this is not the most efficient way to do this, I am just practicing. Apparently it was worth it
    Last edited by Twinkie; 12-29-2009 at 02:23 AM.

  2. #2
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: Repainting elements in a panel

    Hi Twinkie,

    I checked the code. It is ok To know what exactly happening
    set some default text to the JLabel

    text = new JLabel("Hello");
    In the Writter
    change this to observe the change.

    Main.text.setText("" + event.getKeyChar());
    To see the changes i done this. Actually the size of the label is not changing so when you append the text, it is creating scroll bars So i am just adding the typed key to label
    Last edited by Gouri; 12-29-2009 at 02:55 AM.
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  3. #3
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Repainting elements in a panel

    How can I make it work the way it way intended, which was to changes size and scroll when it reached the end of the window?

    I think it has something to do with the revalidate() method of the JScrollPane although I am not sure how to work it...
    Last edited by Twinkie; 12-29-2009 at 03:17 AM.

  4. #4
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: Repainting elements in a panel

    Hi Twinkie,

    Just add the label don't create the scroll pane in Main class
    window.add(text);
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  5. #5
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Repainting elements in a panel

    Yes, but then the label can grow to a size beyond the window. When that happens, I would like it to have scroll bars. Overcomplicated, I know, but the point is for me to know how to do it.

+ Reply to Thread

Similar Threads

  1. Problem with Account management panel
    By Gouri in forum Free Hosting
    Replies: 0
    Last Post: 12-12-2009, 09:36 AM
  2. Replies: 6
    Last Post: 06-24-2009, 03:00 PM
  3. Can't get to my Control panel.
    By Marty7 in forum Free Hosting
    Replies: 15
    Last Post: 07-16-2008, 02:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers