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

Thread: Help with my java applet - displaying output with text

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my java applet - displaying output with text

    H


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

    Default Re: Help with my java applet - displaying output with text

    When posting code, please use the highlight tag to preserve formatting. Also, code should be in the form of an SSCCE. For example, I'm not sure what's giving you trouble- does the algorithm work? Does the applet show up? Start over and post only the code that's giving you trouble, hard-coding the rest (if your problem is displaying a String on a gui, write code that displays a hard-coded String on a gui first).
    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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Help with my java applet - displaying output with text

    Well, this is the section of code we really care about:

    String mystring="";
    for(int i=0;i<intArray.length;i++){
       if(intArray[i]>0)
      {
          mystring+=String.valueOf(intArray[i]); 
          mystring+=", ";
       }
    }
    result.setText(mystring);

    Notice how you're appending the String so it contains the counts, separated by commas.

    You say you want that String to contain more information. Why don't you just append that information the same way you append the comma?

    Also, System.out.println() is your best friend. Don't be afraid to use it to check the value of the String as you go through the loop!
    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!

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my java applet - displaying output with text

    I understand what you mean by this but I am not sure on how to lay it out so it appears how i need it too? .

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

    Default Re: Help with my java applet - displaying output with text

    Quote Originally Posted by AdamLuke18 View Post
    I understand what you mean by this but I am not sure on how to lay it out so it appears how i need it too? .
    There are a couple ways to format text. You can either use html or escape characters such as \n for a newline and \t for a tab.
    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!

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

    Default Re: Help with my java applet - displaying output with text

    I just realized that you're using AWT instead of Swing. I'm not sure how AWT deals with escape characters and html. Is there a reason you aren't using Swing?

    Also, I was wrong about escape characters working in components, at least for JLabels. Use html instead. Here's an example:

    import javax.swing.JFrame;
    import javax.swing.JLabel;
     
    public class Test{
     
    	public static void main(String[] args) {
     
    		JFrame frame = new JFrame("Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		JLabel label = new JLabel("<html>one<br/>two</html>");
     
    		frame.add(label);
     
    		frame.setSize(100, 100);
    		frame.setVisible(true);
     
    	}
    }

    Alternatively, you could split this up into separate JLabels, one for each line.

    As for setting your JLabel's size, do you mean the font size (if so, use html or the setFont() method if you aren't using html) or the size of the colored box around your text (if so, use a layout)?
    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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Help with my java applet - displaying output with text

    Quote Originally Posted by AdamLuke18 View Post
    So it has the right format now i just need to change the number at the start of the sentence and have it on a new line
    any ideas?
    For the number at the start, you're appending the value of the array at index i. I'm not sure how your algorithm works, but you need to make sure you're filling the array correctly. For the number at the end, you shouldn't just use a hard-coded "1" in your String. Again, you're looking at the value of the array at index i. It's up to you how that value and that index map to your output.

    As for the multiple lines, I've given you several suggestions. Either use html (not sure if it works in AWT) or split the lines up into multiple JLabels.

    --- Update ---

    This thread has been cross posted here:

    http://www.java-forums.org/awt-swing/85701-help-my-java-applet-displaying-output-text.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    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!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Help with my java applet - displaying output with text

    Why were all the posts deleted?

    Here's some of it:

    Yeah sorry about that this is my first post. The algorithm works fine i just need to display the output as

    There are:
    2 words of length 1
    1 word of length 2
    1 word of length 3

    whereas mine just outputs the numbers 2,1,1

    I am not sure how to change this. I think I have to use a variable from the array and display it but i am not too sure. The array code is
    String array[]=text.split(" ");
               int counter=0;
               for(int i=0;i<array.length;i++)
                   if(counter<array[i].length())
                       counter=array[i].length();
               int intArray[]=new int[counter];
               for(int i=0;i<intArray.length;i++){
                   intArray[i]=0;
               }
               for(int i=0;i<array.length;i++){
                   intArray[array[i].length()-1]++;
               }
               String mystring="";
               for(int i=0;i<intArray.length;i++){
                   if(intArray[i]>0)
                   {
                    mystring+=String.valueOf(intArray[i]); 
                    mystring+=", ";
                   }
               }
               result.setText(mystring);
               // set label here to display text
    The result button is currently displaying the 2,1,1 output.
    Thanks
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Help with my java applet - displaying output with text

    Quote Originally Posted by Norm View Post
    Why were all the posts deleted?
    I would guess because OP was getting flak for crossposting, or maybe he found the answer and wants to get rid of any evidence of getting homework help online?
    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!

Similar Threads

  1. How to sort rectangles on Java applet from an array in a text file?
    By BWPop95 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 6th, 2013, 01:51 PM
  2. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  3. Applet Help: Parsing and displaying labels
    By katyprim in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 11th, 2010, 09:32 AM
  4. Why output is displaying 0 for calculation in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2009, 01:18 PM
  5. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM

Tags for this Thread