Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By Fubarable

Thread: How to print arrays to JFrames

  1. #1
    milkywave1 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Post How to print arrays to JFrames

    As the title says, how to you print arrays to JFrames. I tried methods like using JLabel to print the arrays, but it just prints the bottom line
    Please help!
    Heres my code if its neccesary:
    Java Code:
    package net.thegoldenkey.martin;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class Core extends JFrame
    {
    	private static final long serialVersionUID = 1L;
    	private static final int WIDTH = 1366;
    	private static final int HEIGHT = 768;
    	
    	private static JFrame j = new JFrame();
    	
    	public static void main(String[] args)
    	{
    		frame();
    	}
    	
    	public static void frame()
    	{
    		System.out.println("Loading JFrame..."); 
    		
    		//JFrame Windows
    		j.pack();
    		j.setTitle("The Golden Key");
    		j.setSize(WIDTH, HEIGHT);
    		j.setVisible(true);
    		j.setResizable(false);
    		j.setLocationRelativeTo(null);
    		j.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		//Graphics
    		for(String row : Maps.Island1)
    		{
    			JLabel l = new JLabel(row);
    			j.add(l);
    		}
    	}
    }
    Maps
    Java Code:
    package net.thegoldenkey.martin;
    
    public class Maps
    {
    	//Starting area
    	public static String[] Island1 = 
    		{
    			" _________________   _______",
    			"/                         \\"
    		};
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to print arrays to JFrames

    Myself, I'd use a JTextArea, set its Font to Font.MONOSPACED, and then append the Strings into the JTextArea via a for loop.
    DarrylBurke likes this.

  3. #3
    milkywave1 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to print arrays to JFrames

    Ok, I tried this and nothing happened
    Java Code:
    			for(String row : Maps.Island1)
    			{
    				JTextArea map = new JTextArea(row);
    				j.add(map);
    			}
    Oh yea, how do you set the fonts to monospaced

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Re: How to print arrays to JFrames

    JTextArea a = new JTextArea();
    a.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to print arrays to JFrames

    Adding each row to a new, different text area that isn't added itself to any visible component hierarchy isn't going to put anything on your screen.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    milkywave1 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to print arrays to JFrames

    Quote Originally Posted by DarrylBurke View Post
    Adding each row to a new, different text area that isn't added itself to any visible component hierarchy isn't going to put anything on your screen.

    db
    Then how do you fix it?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to print arrays to JFrames

    Use one JTextArea, declared and initialized before the loop (possibly as an instance field, if you require access from another method).

    Don't forget to append a newline "\n" after each row.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default Re: How to print arrays to JFrames

    You can add it to a JList.

    JList l=new JList();
    DefaultListModel d=new DefaultListModel();

    for(int i=0;i<a.length;i++)
    {
    // supposed if the array is an int[]
    d.addElement(new Integer(a[i]));
    }

    l.setModel(d);

  9. #9
    milkywave1 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to print arrays to JFrames

    How do you do that?(Sorry i'm very new to JTextArea)

  10. #10
    milkywave1 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to print arrays to JFrames

    Quote Originally Posted by gowthamgutha View Post
    You can add it to a JList.

    JList l=new JList();
    DefaultListModel d=new DefaultListModel();

    for(int i=0;i<a.length;i++)
    {
    // supposed if the array is an int[]
    d.addElement(new Integer(a[i]));
    }

    l.setModel(d);
    I tried that but did not work

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to print arrays to JFrames

    Stating that something "does not work" without showing what you tried makes it *very* hard to know what you may be doing wrong, makes it very hard to help you.

  12. #12
    milkywave1 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to print arrays to JFrames

    Quote Originally Posted by Fubarable View Post
    Stating that something "does not work" without showing what you tried makes it *very* hard to know what you may be doing wrong, makes it very hard to help you.
    Oh ok :/ Heres what i tried
    Java Code:
    			JTextArea map = new JTextArea();
    			for(String row : Maps.Island1)
    			{
    				j.add(map);
    			}

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to print arrays to JFrames

    Quote Originally Posted by milkywave1 View Post
    Oh ok :/ Heres what i tried
    Java Code:
    			JTextArea map = new JTextArea();
    			for(String row : Maps.Island1)
    			{
    				j.add(map);
    			}
    What is j? Re-adding the same JTextArea to a component multiple times will not likely do anything useful.

    Why not try what has already been suggested several times above: Appending Strings to the JTextArea inside of the for loop?

  14. #14
    milkywave1 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to print arrays to JFrames

    Ok, I tried appending strings and now it prints it only in a line

    If you looked at the top of the post, 'j' is the variable for JFrame.

    Heres what i tried,
    Java Code:
    			JTextArea map = new JTextArea();
    			for(String row : Maps.Island1)
    			{
    				map.append(row);
    			}
    			j.add(map);
    EDIT:Ok, Ive tried doing this,
    JTextArea map = new JTextArea();
    for(String row : Maps.Island1)
    {
    map.append(row);
    map.append("\n");
    }
    j.add(map);
    and it worked!

    EDIT2: I played with it for a while, and sometimes the application works and sometimes it doesn't? Im confused,
    Last edited by milkywave1; 01-01-2013 at 06:23 AM.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to print arrays to JFrames

    Quote Originally Posted by milkywave1 View Post
    Ok, I tried appending strings and now it prints it only in a line

    If you looked at the top of the post, 'j' is the variable for JFrame.

    Heres what i tried,
    Java Code:
    			JTextArea map = new JTextArea();
    			for(String row : Maps.Island1)
    			{
    				map.append(row);
    			}
    			j.add(map);
    EDIT:Ok, Ive tried doing this,
    JTextArea map = new JTextArea();
    for(String row : Maps.Island1)
    {
    map.append(row);
    map.append("\n");
    }
    j.add(map);
    and it worked!
    good deal!

    EDIT2: I played with it for a while, and sometimes the application works and sometimes it doesn't? Im confused,
    We'll need more to be able to help you. Consider creating and posting a minimal example that compiles, runs, contains no code unrelated to the problem or essential to allow it to compile and that reproduces your problem, an SSCCE. Post that and we'll be better able to understand what your problem is and how to help you.

  16. #16
    milkywave1 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: How to print arrays to JFrames

    We'll need more to be able to help you. Consider creating and posting a minimal example that compiles, runs, contains no code unrelated to the problem or essential to allow it to compile and that reproduces your problem, an SSCCE. Post that and we'll be better able to understand what your problem is and how to help you.
    Oh nvm I solved it... How do you make it so that you can't type on the JFrame?

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to print arrays to JFrames

    Quote Originally Posted by milkywave1 View Post
    Oh nvm I solved it... How do you make it so that you can't type on the JFrame?
    And you will share your solution, seeing as this is a forum and not an online coaching center?

    A JFrame isn't a text component, so you can't type on it at all.

    Quote Originally Posted by Fubarable View Post
    Consider creating and posting a minimal example that compiles, runs, contains no code unrelated to the problem or essential to allow it to compile and that reproduces your problem, an SSCCE. Post that and we'll be better able to understand what your problem is and how to help you.
    So where's that SSCCE?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: How to print arrays to JFrames

    How do you make it so that you can't type on the JFrame?
    For the JTextArea, you call setEditable(false).

    So where's that SSCCE?
    Here it is. I did it myself.

    So where's that SSCCE?
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    
    public class TextAreaFrame extends JFrame {
    	
    	private JTextArea textArea;
    	
    	public TextAreaFrame() {
    		textArea = new JTextArea();
    		textArea.setEditable(false);
    		this.add(textArea);
    		setSize(500,500);
    		setVisible(true);
    	}
    
    	private void add(String[] array) {
    		for(String row : array)
    		{
    			textArea.append(row);
    			textArea.append("\n");
    		}
    		repaint();
    	}
    	
    	public static void main(String[] args) {
    		TextAreaFrame textAreaFrame = new TextAreaFrame();
    		String[] array = { "This is a test", "of the emergency broadcast", "system."};
    		textAreaFrame.add(array);
    	}
    
    }
    To the original poster, the above code is what's meant as a SSCCE. It is supposed to have you reduce your code down in size and complexity and show the problem, and be compilable. Many times when you are making an SSCCE, you figure out how to fix your own problem and if you don't, you have a much shorter listing that other people can help you with.

Similar Threads

  1. jframes
    By javazers in forum AWT / Swing
    Replies: 9
    Last Post: 10-19-2012, 09:54 PM
  2. Need help with JFrames
    By chassconcept in forum New To Java
    Replies: 3
    Last Post: 06-12-2012, 11:18 PM
  3. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 12:14 AM
  4. jframes
    By ddj in forum AWT / Swing
    Replies: 0
    Last Post: 03-24-2009, 03:15 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 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
  •