Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: drawString and JTextField

  1. #1
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default drawString and JTextField

    ok i have two little questions i hope you can help me answer.

    1. if you have a class that extends applet and implements runnable with a borderlayout set and you create an object from another class that extends panel (to put in North), if all that class does is have a String that gets used by the object in the calling class how do i use drawString (or another method) to paint it on the Panel in North? can Panel use the paint() method?

    and...

    2. in the original class described above (the applet class) i want to use a JTextField over an area that has a background color of black, how do i make the text field (i guess) white so i can see where it is and what is typed in it.

    thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: drawString and JTextField

    Quote Originally Posted by that_guy View Post
    ok i have two little questions i hope you can help me answer.

    1. if you have a class that extends applet and implements runnable with a borderlayout set and you create an object from another class that extends panel (to put in North), if all that class does is have a String that gets used by the object in the calling class how do i use drawString (or another method) to paint it on the Panel in North? can Panel use the paint() method?
    What happened when you tried? And is there a reason you're using AWT and not Swing? Read this tutorial: Painting in AWT and Swing


    Quote Originally Posted by that_guy View Post
    2. in the original class described above (the applet class) i want to use a JTextField over an area that has a background color of black, how do i make the text field (i guess) white so i can see where it is and what is typed in it.
    Again, what did you try? But I would expect the JTextField to already have a white background.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: drawString and JTextField

    ok,

    im just learning Swing and i was taught in awt so if there is another way to draw a string to an applet that would be helpful. so...

    when the applet class calls the panel class (the one with the String i want displayed; the String is a class variable and not in the constructor as the constructor is blank cause i dunno what to put in it) via the object i made 'txt', i then put the object at North when added in the init() method.

    the run() and paint() methods in the applet class call the string specifically (the project im on says i must use animation or what not so the String is supposed to change colors every 500 miliseconds) but when i run the applet the component in Center uses up the space in Center and North as if the Panel isnt there, so therefore there is no String showed...

    before i changed the class into 2 classes (yes, it was all just one class before) the String was drawn using coordinates and it worked perfectly. now it doesnt show.



    2. I would expect it to have a white background as well but no, all i see is black and when i click on the textfield it will flicker white real quick and show the text then go back to black. never worked with Swing before........ should i change it from an applet to a JApplet?

    thanks

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: drawString and JTextField

    Quote Originally Posted by that_guy View Post
    ok,

    im just learning Swing and i was taught in awt so if there is another way to draw a string to an applet that would be helpful. so...

    when the applet class calls the panel class (the one with the String i want displayed; the String is a class variable and not in the constructor as the constructor is blank cause i dunno what to put in it) via the object i made 'txt', i then put the object at North when added in the init() method.

    the run() and paint() methods in the applet class call the string specifically (the project im on says i must use animation or what not so the String is supposed to change colors every 500 miliseconds) but when i run the applet the component in Center uses up the space in Center and North as if the Panel isnt there, so therefore there is no String showed...

    before i changed the class into 2 classes (yes, it was all just one class before) the String was drawn using coordinates and it worked perfectly. now it doesnt show.
    Well, my point was that you probably want to be using JApplet and a JPanel instead. But without seeing an SSCCE (that's a link), it's going to be hard to comment on your code.


    Quote Originally Posted by that_guy View Post
    2. I would expect it to have a white background as well but no, all i see is black and when i click on the textfield it will flicker white real quick and show the text then go back to black. never worked with Swing before........ should i change it from an applet to a JApplet?
    What happened when you tried? But yes, this definitely sounds like a side-effect of mixing Swing and Awt, which you should almost definitely not do.

    Here's a decent article explaining the issues: Mixing heavy and light components
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: drawString and JTextField

    k, sorry for not posting this earlier but the comp with the code currently has no way to get online.

    so here is the code:

    import java.applet.*;   // imports the applet package
    import javax.swing.*;   // imports the swing package
    import java.awt.*;      // imports the awt package
    import java.awt.event.*;
     
    public class OnlinePayroll extends JApplet implements Runnable,  //**  creates class OnlinePayroll
                                                 ActionListener,    //**  extends applet class
                                                 FocusListener      //**  implements Runnable interface
    {
        Thread th = new Thread(this);                                   // creates a new Thread named th
        int count[] = {0};                                              //**  initiates variable array
                                                                        //**  to count repetions
     
        Onlinetxt doc = new Onlinetxt();
        Onlineghg calc = new Onlineghg();
     
     
        private Image dbimage;  // creates a new Image object to doubleBuffer
        private Graphics dbg;   // creates a new Graphics object to doubleBuffer
     
        Font tfont = new Font ("Arial",Font.BOLD, 20); // initiates a font for the string calc
     
        public void init ()                 // start of init() method
        {
            setLayout(new BorderLayout()); //sets applets layout to gridlayout with 0 rows 1 column
            //setBackground(Color.black);     //sets background color to black
     
            add(calc, "North");
            calc.setVisible(true);
     
            add(doc, "Center");
            doc.setVisible(true);
        }
     
        public void start ()    // start of start() method
        {
            th.start();         // starts thread th
        }
     
        public void run ()          // start of run() method
        {
            while (true)            // enters forever while loop
            {
                repaint ();         // calls repaint method
                try                 // enters try
                {
                    th.sleep(100);  // tries to sleep th for 100 miliseconds
                }
                catch (Exception i) // catches exception if unable to sleep th thread
                {
                }
     
                count[0]++;                                                 //**  adds 1 to count
                if (count[0] == 4)                                          //**  every repetition
                    count[0] = 0;                                           //**  and resets to zero
                                                                            //**  once it reaches 4
     
                Thread.currentThread().setPriority(Thread.MAX_PRIORITY);    // sets threads priority to max
            }                       // forever while loop code stops here
        }
     
        public void paint (Graphics g)          //start of paint() method
        {
            g.setFont(tfont);                   //adds font tfont to graphics object
            //String s = new String(g.getFont());
     
            if (count[0] == 0)                  //**
            {                                   //**
                g.setColor(Color.red);          //**
                g.drawString(calc.calc, 180, 20);    //**
            }                                   //**
            else if (count[0] == 1)             //**
            {                                   //**
                g.setColor(Color.green);        //**
                g.drawString(calc.calc, 180, 20);    //**  Series of if statements
            }                                   //**  to decide which color to draw
            else if (count[0] == 2)             //**  string calc in: red, green, blue, or yellow
            {                                   //**
                g.setColor(Color.blue);         //**
                g.drawString(calc.calc, 180, 20);    //**
            }                                   //**
            else if (count[0] == 3)             //**
            {                                   //**
                g.setColor(Color.yellow);       //**
                g.drawString(calc.calc, 180, 20);    //**
            }                                   //**
        }
     
        public void update(Graphics g)
        {
            if(dbimage==null)                                                       //**
            {                                                                       //**
                dbimage = createImage(this.getSize().width, this.getSize().height); //**
                dbg = dbimage.getGraphics();                                        //**
            }                                                                       //**
            dbg.setColor(getBackground());                                          //**  Double Buffer
            dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);        //**
                                                                                    //**
            dbg.setColor(getForeground());                                          //**
            paint(dbg);                                                             //**
                                                                                    //**
            g.drawImage(dbimage, 0, 0, this);                                       //**
        }
     
        public void actionPerformed(ActionEvent e)
        {
     
        }
     
        public void focusLost(FocusEvent f)
        {
     
        }
     
        public void focusGained(FocusEvent f)
        {
     
        }
    }

    import java.applet.*;   // imports the applet package
    import javax.swing.*;   // imports the swing package
    import java.awt.*;      // imports the awt package
    
    public class Onlinetxt extends JPanel
    {
        Label Elbl = new Label("Enter emplyees department:");
        JTextField Etxt = new JTextField(20);
        Label Slbl = new Label("Salary or hourly?");
        JTextField Stxt = new JTextField(20);
        Label Hlbl = new Label("Enter the number of hours worked:");
        JTextField Htxt = new JTextField(20);
     
        public Onlinetxt()
        {
            setLayout(new GridLayout(0,2));
     
            add(Elbl);
            Elbl.setAlignment(Elbl.RIGHT);
            add(Etxt);
            Etxt.setBackground(Color.white);
            Etxt.setVisible(true);
     
            add(Slbl);
            Slbl.setAlignment(Slbl.RIGHT);
            add(Stxt);
            Stxt.setVisible(true);
     
            add(Hlbl);
            Hlbl.setAlignment(Hlbl.RIGHT);
            add(Htxt);
            Htxt.setVisible(true);
        }
    }

    import java.applet.*;   // imports the applet package
    import javax.swing.*;   // imports the swing package
    import java.awt.*;      // imports the awt package
    
    public class Onlineghg extends JPanel implements Runnable
    {
        String calc = new String("Calculate your pay issues here!!!!"); // creates new String named calc
     
        public Onlineghg()
        {
            //add(calc);
        }
     
        public void run()
        {
     
        }
    }

    the only changes i made was switching from applet and panel to Japplet and Jpanel respectively. at this point in time im only concerned with cosmetics, i am pretty sure i can handle the logic, so any help with this would be great.

    thanks

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: drawString and JTextField

    Well, an SSCCE should preferably fit inside one class and file (you can have nested classes if you absolutely have to, but you probably don't have to). And you didn't tell us whether switching to Swing changed anything.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: drawString and JTextField

    ok...

    well first off thanks for helping me with this and i apologize for any inconvenience my posts are...

    when i changed the classes to extend Japplet and Jpanel instead of their awt counterparts the animated string did appear (which is good) but BEHIND the label of "What department is the employee in?". the String can be seen partially which confirms that it is there. The labels (not a jlabel, should it?) and textfields in 'Center' are still taking up space in 'North' where i want the String to be but since i have to place the String using coordinates its not being placed on the jpanel and the jpanel is being wasted and its like its not even there even though its supposed to be in 'North'.

    The area around the labels are kinda off color and not white (again, could it be cause its not a jlabel?) and only when i click on or type in the textboxes does the actual box of the textbox appear, i dont know if that is normal however, when i do type in it the letters are visible and do not flicker which is a plus.


    thats what the program does when run

    thanks

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: drawString and JTextField

    Thanks for clarifying. Like I and the link I gave you said, mixing AWT and Swing will result in pretty much exactly the behavior you're describing. What happened when you changed it to JLabel? That should be an easy thing to check.

    As for things not showing up in the NORTH position- take a look at the BorderLayout tutorial. It says that using the String argument to specify the position is error prone. Also, do you set the size (or preferredSize) of that Component?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: drawString and JTextField

    hahahaha now its worse (or so i think)...

    i changed the labels to Jlabels which created errors cause apparently jlabels dont have the setAlignment() method so i changed those method calls to setHorizontalAlignment(Elbl.Right); which caused the program to compile correctly.

    then when i ran the program the labels were no longer visible, however the off color white shading around them was gone too (probably cause they are now Swing and not awt). however the string that changes colors, 'calc', is no longer blocked and is clearly visible and working correctly.

    no, i just create the String as a class variable (again, not sure how/if i should put it in the class constructor) with the set phrase and then post it with the font object i created... so im thinking i am painting it to the applet and not to the panel... how do i fix that?
    Last edited by that_guy; January 27th, 2011 at 10:26 PM.

  10. #10
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: drawString and JTextField

    btw, which borderlayout tutorial are you talking about? there was no link posted with the recommendation, so how would you go around the drawString situation?

Similar Threads

  1. Jtextfield Validation
    By nimishalex in forum AWT / Java Swing
    Replies: 8
    Last Post: December 11th, 2010, 02:42 AM
  2. [SOLVED] font size and other attributes for Graphics.drawString()
    By gib65 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 30th, 2010, 02:31 PM
  3. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 03:21 PM
  4. Click to start and drawString fonts
    By Campos in forum Java Applets
    Replies: 3
    Last Post: July 24th, 2009, 02:24 PM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM