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 16 of 16

Thread: GUI elements description text box

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default GUI elements description text box

    So, its very simple thing, but just cant figure it out ... You know, there is buttons and other gui elements, and in many programs, when you move your mouse over any button or icon or other element, then text box appears with that elements description, like you move mouse over new tab button in your internet browser, and text box appears with text "new tab". How to do this in java, i.e., for javax.swing.JButton, or any other element ?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,464
    My Mood
    Mellow
    Thanks
    87
    Thanked 482 Times in 473 Posts

    Default Re: GUI elements description text box

    That's called a ToolTip. You can find info on how to customize those and make them appear in the Java Tutorials.

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: GUI elements description text box

    Ok, that worked, thanks. Now i have another question : i have three jTextField elements in my GUI program : jTextField1, jTextField2, jTextField3. I have three threads, that updates very fast these text fields with double type variables, converted to strings, every few milliseconds by doing this :
    "textField3.setText(new DecimalFormat("#.000").format(cc.getCurrentCookies ()));"
    So, it works, numbers gets updated really fast, but when i try to select these really fast changing numbers with my mouse, GUI and the whole program just freezes, sometimes freezes just the GUI, but program runs fine, i.e. i still get messages with system.out.println(text). Those freezes are probably because selecting text is interrupting my threads somehow, so is there a way to remove the ability to select the text in jTextField, or some other similar GUI element, that has what i need ?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,464
    My Mood
    Mellow
    Thanks
    87
    Thanked 482 Times in 473 Posts

    Default Re: GUI elements description text box

    It'd be interesting to see the code behind what you've described, because I'm having trouble visualizing it, but don't bother. If you haven't heard it before, you're hearing it here now, "Swing is not thread safe." You can find many articles about why this is and how to overcome it as a limitation, if it is, but in your case, I think you've just gone too far.

    It's important to have the Swing app running on the Event Dispatch Thread, or EDT. If your app is not, then search for explanations on why and how to do that.

    There's no reason to sample the contents of 3 JTextFields at some frequency as you've described, at least not a reason that can't be satisfied some other way. If you want to catch changes to the JTextFields as they occur, then I suggest you use a change listener on each of the JTextFields to capture the changes as they occur and react to them as needed.

    If you could post an SSCCE we might be able to give you some pointer on how to improve it.

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: GUI elements description text box

    Well, for now it is just a big mess in netbeans with generated code, but it seems that "jTextField3.setHighlighter(null)" method works.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,464
    My Mood
    Mellow
    Thanks
    87
    Thanked 482 Times in 473 Posts

    Default Re: GUI elements description text box

    I recommend you tame the "big mess" by understanding what's going on. If you'd like help, ask here.

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: GUI elements description text box

    Yeah, i know, gonna fix that mess later, now i'm trying to get all my program elements and logic together.
    BTW, i noticed, that freezes happens only with very low update time, like 10 ms, but with i.e. 40 ms, everything is fine, i can select text in text boxes.

  8. #8
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: GUI elements description text box

    Is it possible to replace ToolTip with some other multi-line text box, or at least to make ToolTip multi-line ?

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,464
    My Mood
    Mellow
    Thanks
    87
    Thanked 482 Times in 473 Posts

    Default Re: GUI elements description text box

    You can create your own plain text box or dialog as you've described. I haven't done it, so I don't know exactly what is involved, but it's possible.

    You might also explore the options to modify the ToolTip to do what you want. Here's a place to start, then do your own searching.

  10. #10
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: GUI elements description text box

    Ok, i found a way to use multiple lines in tooltip using html tags. I use this method to update my tooltip text every time i press a button. Now, i want to align just a part of this text to the right side, but it seems that "<p> ... </p>" also creates new lines after and before itself, but i want to have a text at the same lane to be aligned to the left and to the right, i.e.
    Untitled.jpg

    public String getDescriptionForToolTip() {
            return "<html>" + this.Name + "<p align=right>" + "" + new DecimalFormat("#.000").format(this.CurrentCost) + "</p>" + "[owned :" + new DecimalFormat("#.0").format(this.Amount) + "]" + "<br>" + this.Description + "</html>";
        }

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: GUI elements description text box

    Did you try <center>
    Another useful tag is <span>

  12. #12
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: GUI elements description text box

    Well, center is center, but there is no <right>, well, i will leave it that way for now, not a big deal. How to create event listeners ? I dont know what excatly i need, but i have many variables, some of them are simple, like double, string, some of them are objects with a few simple variables, and i need to listen to quite a few of those variables all the time, and if value of variable meets my requirements, i.e. int var1; if(var1 >= 36457){change other variables}, then do some actions.

  13. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: GUI elements description text box

    Quote Originally Posted by startas View Post
    Well, center is center, but there is no <right>
    Sometimes my brain just makes me see something, I do not know how I decided you wanted center.
    How about <style> with text-align:right

    As far as the event listeners, that is a very general topic with many solutions. If you need more specific help than a search engine provides, post some code and/or a more specific example of what you are trying to do

  14. #14
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: GUI elements description text box

    Well, i have a simple GUI program with many buttons, variables and a few text fields. Lets take this little example :
    private Building b1 = new Building("Name", 15.0, 15.0, 0.1, "Description"); //String, double, double, double, String.
    private int howMuch;
    //------------------------------------------
    private int total;
     
    public void updateTotal() {
        //some formula to update variable total;
    }
     
    private javax.swing.JTextField myTextField;
    myTextField.setText(/*total converted to String*/);

    So, here i have Building b1, and in my program i have many others, and i need to listen to, i.e., the first double value of b1 and to variable "howMuch", and if these variables meets my requirements, i.e. "if (howMuch > 3000)", then to execute another piece of code.

    Another thing, i have variable "total", and i have method "updateTotal()", which updates my variable "total". I need regularly to call method "updateTotal()" to update regularly "total", and i need to do this every x amount of time, whatever i will specify, i.e. every 50 milliseconds.

    And the last thing, i have a GUI element JTextField "myTextField", i can set its text via method "setText()", and i need the same - regularly to update myTextFields text every x amount of time. Well, i could just call method "setText()" from method "updateTotal()", but i wonder if that would be the right way to do this and if there is another way to do that ?

    As for now, i have done this in a bad, but working way (see example below), and i would like to rewrite these things in the right way. Again, it is a bad way to do this, but it WORKS pretty good, no malfunctioning and nothing bad happened after a few hours of testing, everything is stable.

    //Some file
    public class MyThread extends Thread {
        private long updateTime;
        private int jobNumber;
        private static MyVariable handler;
        private static JTextField textFieldPerSecond;
        private static JTextField textFieldTotal;
        private static JTextField textFieldCurrent;
        .............................................................
    }
     
    // In main another file, in public class NewJFrame extends javax.swing.JFrame
    public NewJFrame() {
            initComponents();
     
            // Thread MyThread has 2 constructors :
            // MyThread(long sleepTime, int jobNumber, MyVariable c, JTextField t1, JTextField t2, JTextField t3);
            // MyThread(long sleepTime, int jobNumber);
     
            // First thread sets 3 JTextField objects for MyThread, and is calling jTextField2.setText(handler.getValue3().toString()) every 40 ms.
            new MyThread(40, 2, variable1, jTextField3, jTextField2, jTextField1).start();
     
            // This thread is calling jTextField3.setText(handler.getValue().toString()) every 200 ms.
            new MyThread(200, 1).start();
     
            // this thread is updating handler's variables every 40 ms.
            new MyThread(40, 3).start();
     
            // This thread is calling jTextField1.setText(handler.getValue2().toString()) every 40 ms.
            new MyThread(40, 4).start();
        }
     
    //The logic in MyThread is this :
    run() {
        while(true) {
            switch(jobNumber) {
                case number x:
                     //call method i need, i.e.
                     textFieldTotal.setText(handler.getValue2().toString());
                     break;
                     // and so on...
            }
            here is Thread.sleep(updateTime) with all try sentences and so on.
        }
    }

  15. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,464
    My Mood
    Mellow
    Thanks
    87
    Thanked 482 Times in 473 Posts

    Default Re: GUI elements description text box

    i would like to rewrite these things in the right way.
    Hooray!
    So, here i have Building b1, and in my program i have many others, and i need to listen to, i.e., the first double value of b1 and to variable "howMuch", and if these variables meets my requirements, i.e. "if (howMuch > 3000)", then to execute another piece of code.
    I assume that your Building class has text associated with it that is parsed to a double so that the howMuch comparison can be made. Correct me if wrong.

    Assuming I'm correct, most (all?) components that contain text have an underlying document to which a listener can be added to detect any changes made, similar to as I did below for a search field in an application I'm working on (slowly):
            // add a document listener to the text field's underlying document
            // to notify the controller of all changes made to the text field
            searchField.getDocument().addDocumentListener( new DocumentListener()
            {
     
                // Called when text is inserted into the listened-to document.
                @Override
                public void insertUpdate( DocumentEvent e )
                {
                    // notify the controller of the changes to the search field
                    searchFieldChanges( searchField.getText() );
     
                } // end method insertUpdate()
     
                // Called when text is removed from the listened-to document.
                @Override
                public void removeUpdate( DocumentEvent e )
                {
     
                } // end method removeUpdate()
     
                /* Called when the style of some of the text in the listened-to
                 * document changes. This sort of event is fired only from a
                 * StyledDocument — a PlainDocument does not fire these events.
                 */
                @Override
                public void changedUpdate( DocumentEvent e )
                {
                    // not used
     
                } // end method changedUpdate()
     
            } );
    You can use this approach to immediately detect any changes to the text field and react accordingly.
    Another thing, i have variable "total", and i have method "updateTotal()", which updates my variable "total". I need regularly to call method "updateTotal()" to update regularly "total", and i need to do this every x amount of time, whatever i will specify, i.e. every 50 milliseconds.
    For this and other recurring/timed tasks in a Swing application, you should be using a javax.swing.Timer. The little bit of your code I've seen, I think the Timer provides everything you need, BUT if you need the functionality of a Thread, then you should be using SwingWorker. I've included a link to the Java Swing Concurrency Trail below which includes the use of SwingWorker. You should study that Trail and incorporate its suggestions into your new, improved code.

    One of the quickest ways to discover the meaning of "Swing is not Thread safe," is to start other threads from the Swing application and then pause them. If the pauses are significant (which aren't?) the GUI will appear to freeze up and become unresponsive, possibly even crashing, depending on what the Threads are doing. Also, it's not clear that you're running the Swing application on the EDT. The link to my favorite article on the topic is currently broken, so please find your own while I look for another. If in your study you don't find mention of the EDT or a clear explanation of what it is or how to use it (unlikely), then let me know.

    Java Concurrency Trail

  16. #16
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: GUI elements description text box

    Well, as i know from studies, pausing threads is unsafe anyway, you dont need to do that in EDT, and it should not be used. Is there anything really bad that can happed, if i just run threads the way i showed above ?

Similar Threads

  1. Getting Selected Text From Combo Box
    By tommyacton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 6th, 2013, 07:14 PM
  2. GUI not showing its J-elements???
    By dynamix24 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 3rd, 2013, 12:07 AM
  3. how to draw on text box in j2me
    By iraqy2010 in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: June 20th, 2011, 09:50 AM
  4. [SOLVED] Gap between GUI elements
    By petemyster in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2011, 11:44 AM
  5. Adding data to a text box on another class
    By Qualitystreet in forum AWT / Java Swing
    Replies: 3
    Last Post: April 5th, 2011, 02:38 AM