Results 1 to 7 of 7
  1. #1
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Lightbulb Dynamically changing the TextArea.forground !

    but how do I change it dynamically???

    Java Code:
    import javax.swing.*;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.plaf.ColorUIResource;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import java.awt.Color;
    
    public class EasyToolTip extends JPanel {
    
        EasyToolTip() {
            UIManager.put("ToolTip.background", new ColorUIResource(Color.CYAN));
            UIManager.put("TextArea.foreground", new ColorUIResource(Color.CYAN));
            setToolTipText("This is also a way to change the color of the tooltip in your application!");
            b.addActionListener(
                    new ActionListener() {
    
                        public void actionPerformed(ActionEvent event) {
                            if (event.getSource() == b) {
                                UIManager.put("TextArea.foreground", new ColorUIResource(Color.RED));
                                t.setText("Syntax Error");
                            }
                        }
                    });
            t = new JTextArea(10, 10);
            add(b);
            add(t);
        }
        JTextArea t;
        JButton b = new JButton("Change Color");
    
        public static void main(String[] args) {
            javax.swing.JFrame frame = new javax.swing.JFrame("EasyToolTip");
            frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
            frame.add(new EasyToolTip());
            frame.setSize(1280, 770);
            frame.setVisible(true);
        }
    }
    :confused:

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Why are you playing with the UIManager? All Swing components have a setForeground() and setBackground() method.

  3. #3
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    playing to explore more and more.!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't use UIManager for doing this as the program is running (as camickr implies). Use the methods Rob gives you above instead.

  5. #5
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    Isn't there any way we can do this way?:confused:

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Read the section from the Swing tutorial on "Modifying the Look and Feel".

  7. #7
    ziaf is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Dear "Programmer",
    First of all you have to admit that you are coding against the standards.
    UIManager is actually helper file which is used to hold the defaults of each swing component properties.
    Here what u doing is for a particular Component(JTextArea) u need a seprate fore ground colour.
    By altering the property "TextArea.foreground" in UIManager u are changing the JVM value.
    i.e now onwards for all the JTextArea created in your screen the foreground colour will be new value(Color.RED).
    So as experts suggested use the direct method (setForeground()).

    Now coming to your answer.
    By changing property "TextArea.foreground" in UI Manager wont update the foreground colour of
    TextArea that you have already created.
    for that u need to invoke the method updateUI.

    i.e
    use
    Java Code:
    if (event.getSource() == b) {
        UIManager.put("TextArea.foreground", new ColorUIResource(Color.RED));
       [B] t.updateUI();   [/B]                         
        t.setText("Syntax Error");                            
    }

Similar Threads

  1. Replies: 3
    Last Post: 02-06-2010, 04:28 PM
  2. Replies: 1
    Last Post: 04-01-2009, 11:28 AM
  3. Dynamically changing the display
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-23-2008, 12:19 AM
  4. TextArea Bug?
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 01:37 PM
  5. textarea
    By ubuntu in forum AWT / Swing
    Replies: 4
    Last Post: 05-12-2007, 10:54 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
  •