appending to JLabel's text


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: appending to JLabel's text

  1. #1
    Join Date
    Mar 2005
    Posts
    2

    appending to JLabel's text

    Hi,
    I'm trying to use a JLabel(java.swing) like an output console and need to know how to make sure the previous text is not overwriten and it would continue to scroll just like the output console. Like appending the previous text or something.

    Thank you

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Scroll a JLabel ?

    You can't do that... If you want to append text to a scrollable text
    component you should use a JTextArea contained in a JScrollpane.
    Since I think that seing is beleiving I have added a snippet here to show you.

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class TextScroller extends JFrame implements ActionListener {
      BorderLayout borderLayout1 = new BorderLayout();
      JPanel jPanel1 = new JPanel();
      JButton closeBtn = new JButton();
      JButton appendTextBtn = new JButton();
      JTextField inputTextField = new JTextField();
      JScrollPane jScrollPane1 = new JScrollPane();
      JTextArea scroller = new JTextArea();
      JButton clearBtn = new JButton();
    
      public TextScroller() {
        try {
          jbInit();
          this.closeBtn.addActionListener(this);
          this.appendTextBtn.addActionListener(this);
          this.clearBtn.addActionListener(this);
          addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
              System.exit(0);
            }
          });
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        TextScroller ts = new TextScroller();
        ts.setBounds(20,20,650,300);
        ts.setVisible(true);
      }
      /**
       * Set up GUI
       * @throws Exception
       */
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        closeBtn.setText("Close");
        appendTextBtn.setText("Append Text");
        clearBtn.setText("Clear");
        inputTextField.setText("");
        inputTextField.setColumns(25);
        jPanel1.add(closeBtn, null);
        jPanel1.add(appendTextBtn, null);
        jPanel1.add(inputTextField, null);
        jPanel1.add(clearBtn, null);
        scroller.setLineWrap(true);
        jScrollPane1.getViewport().add(scroller, null);
        this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
        this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
      }
      /**
       * Handle button clicks
       * @param e
       */
      public void actionPerformed(ActionEvent e) {
        if (e.getSource()==closeBtn) System.exit(0);
        else if (e.getSource()==appendTextBtn) {
          String s=inputTextField.getText().trim();
    
          if (s.length() > 0) {
            scroller.append(s);
            // append newline, you may leave that out. the textArea will
            // wrap the lines anyway (linWrap is set to true)
            scroller.append("\r\n");
          }
        } else if (e.getSource()==clearBtn) {
          scroller.setText("");
        }
      }
    }
    Last edited by sjalle; 03-02-2005 at 03:18 AM.
    eschew obfuscation

  3. #3
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    I would definately do what sjalle says as using a Label as a console is just a very bad idea. But if youe heart is struck on it you could do something like this:

    JLabel aLabel = new JLabel("Hello");
    String x = " World";
    aLabel.setText(aLabel.getText() + x);

    Now if your "console" never goes beyond one line, if it is really more like a status bar then i would say use a label. But if your question is how to keep appending than it sounds like its not. Anyways, good luck.
    Java has 99 problems but a pointer ain't one

  4. #4
    Join Date
    Mar 2005
    Posts
    2
    Thank you for you help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center