Results 1 to 18 of 18
Thread: How to print arrays to JFrames
- 12-30-2012, 02:50 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
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
:
MapsJava 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); } } }
Java Code:package net.thegoldenkey.martin; public class Maps { //Starting area public static String[] Island1 = { " _________________ _______", "/ \\" }; }
-
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.
- 12-31-2012, 06:23 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: How to print arrays to JFrames
Ok, I tried this and nothing happened
Oh yea, how do you set the fonts to monospacedJava Code:for(String row : Maps.Island1) { JTextArea map = new JTextArea(row); j.add(map); }
- 12-31-2012, 07:55 AM #4
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Re: How to print arrays to JFrames
JTextArea a = new JTextArea();
a.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
- 12-31-2012, 08:31 AM #5
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-31-2012, 10:23 AM #6
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
- 12-31-2012, 11:41 AM #7
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-31-2012, 12:12 PM #8
Member
- Join Date
- Sep 2012
- Location
- Guntur, India
- Posts
- 27
- Rep Power
- 0
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);
- 12-31-2012, 12:59 PM #9
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: How to print arrays to JFrames
How do you do that?(Sorry i'm very new to JTextArea)
- 12-31-2012, 12:59 PM #10
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
-
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-31-2012, 03:34 PM #12
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
-
Re: How to print arrays to JFrames
- 01-01-2013, 06:15 AM #14
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
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,
EDIT:Ok, Ive tried doing this,Java Code:JTextArea map = new JTextArea(); for(String row : Maps.Island1) { map.append(row); } j.add(map);
JTextArea map = new JTextArea();
for(String row : Maps.Island1)
{
map.append(row);
map.append("\n");
}
j.add(map);
and it worked!.gif)
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.
-
Re: How to print arrays to JFrames
good deal!
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.EDIT2: I played with it for a while, and sometimes the application works and sometimes it doesn't? Im confused,
- 01-03-2013, 11:25 AM #16
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: How to print arrays to JFrames
Oh nvm I solved it... How do you make it so that you can't type on the JFrame?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.
- 01-03-2013, 01:00 PM #17
Re: How to print arrays to JFrames
Why do they call it rush hour when nothing moves? - Robin Williams
- 01-04-2013, 09:03 AM #18
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: How to print arrays to JFrames
For the JTextArea, you call setEditable(false).How do you make it so that you can't type on the JFrame?
Here it is. I did it myself.So where's that SSCCE?
So where's that SSCCE?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.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); } }
Similar Threads
-
jframes
By javazers in forum AWT / SwingReplies: 9Last Post: 10-19-2012, 09:54 PM -
Need help with JFrames
By chassconcept in forum New To JavaReplies: 3Last Post: 06-12-2012, 11:18 PM -
Javax Print Attribute for Selection Print Range
By rsawatzky in forum AWT / SwingReplies: 0Last Post: 04-26-2012, 12:14 AM -
jframes
By ddj in forum AWT / SwingReplies: 0Last Post: 03-24-2009, 03:15 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks