11 Replies - 18818 Views - Last Post: 28 September 2005 - 02:57 PM Rate Topic: -----

#1 Fame Commeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-August 05

Setting Font in JTextArea

Posted 27 September 2005 - 03:20 PM

I need to be able to set the font in a JTextArea to something that uses same-width block lettering...so that all characters are equally wide. I can't find the correct syntax for .setFont(String f) anywhere, I mean, that's it, but it won't work for me. Any help on how to correct whatever it is that I'm [not] doing?
Is This A Good Question/Topic? 0
  • +

Replies To: Setting Font in JTextArea

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Setting Font in JTextArea

Posted 27 September 2005 - 03:27 PM

Can you post your code so we can see it in context. Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 Fame Commeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-August 05

Re: Setting Font in JTextArea

Posted 27 September 2005 - 09:18 PM

What I'm trying to accomplish is a text based game... simple for now until i get image handling figured out... Here's the code for the game world class DD_World:

public class DD_World
{

private String[][] World = new String[20][20];

DD_World()
{
//Initialize game world to 'blank' (spaces)
for(int i = 0; i < 20; i++)
for(int j = 0; j < 20; j++)
World[i][j] = " ";

int a;
//The '#' is to be a World border
for(a = 0; a < 20; a++)
World[a][0] = "#";
for(a = 0; a < 20; a++)
World[a][19] = "#";
for(a = 0; a < 20; a++)
World[0][a] = "#";
for(a = 0; a < 20; a++)
World[19][a] = "#";

}//Constructor End

//Return array of world
public String GetWorld()
{
String temp = "";

for(int i = 0; i < 20; i++ )
{
for(int j = 0; j < 20; j++)
temp += World [i][j];
temp += "\n";
}

return temp;
}
}

//////////////
Now this returns fine, I just need a way to change the font, preferably in swing because that's all I know, so that the ' ' (space), and all other characters will have the same width.
All the formatting is correct, with the exception of character size, which I want to change with the font.
Here's how the GetWorld Method is used:
//////////////

/*....
DD_World World1 = new DD_World();

JTextArea world = new JTextArea( "\n" + World1.GetWorld() );
JOptionPane.showMessageDialog(null, world,
"The World:", JOptionPane.PLAIN_MESSAGE);

......*/

/////////////
And that's in a controller class that instanciates the world. However, if you look at the output, it's crooked because the space is remarkably more skinny than the border '#' symbol. How do i set JTextArea Fonts using the .setFont(font here) method? I can's seem to find the syntax for it, rooting it off of JTextArea, nor the instance "world"... nor anything!

This is just a fraction of an hour or so of work at my first game programming, so if there's any weirdness to the code, please let me know too.

This post has been edited by Fame Commeth: 28 September 2005 - 08:08 AM

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Setting Font in JTextArea

Posted 28 September 2005 - 05:13 AM

Hmmm...that is odd...you should be able to access the method straight from the text area like so, and it overrides the same method name at the component level here.

Are you getting errors when you try it? Sorry to ask you to post the errors, but I'm not near a compiler at the moment.
Was This Post Helpful? 0
  • +
  • -

#5 Fame Commeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-August 05

Re: Setting Font in JTextArea

Posted 28 September 2005 - 06:23 AM

Here's my error:
"cannot find symbol method setFont(java.lang.String)"

I've tried so many syntax forms, and none of them work. How do you set it up? I'm sure I'm doing it right in at least one of them:

JTextArea.setFont("Helvetica");
world.setFont("Helvetica"); //world is the text area instance

...Then I've tried combining them too:

JTextArea world = new JTextArea( "\n" + World1.GetWorld()).setFont("helvetica");

Maybe I have the synatax wholly wrong or something, but this is like the only thing I'm stuck on.

This post has been edited by Fame Commeth: 28 September 2005 - 06:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Setting Font in JTextArea

Posted 28 September 2005 - 06:27 AM

What libraries are you importing?
Was This Post Helpful? 0
  • +
  • -

#7 Fame Commeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-August 05

Re: Setting Font in JTextArea

Posted 28 September 2005 - 06:30 AM

I'm only using
import javax.swing.*;
in this class, and in the World class, swing components are the only nonstandart thing that I am using...
Message panes and dialogue boxes work perfect, just setting the font, grrr.
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Setting Font in JTextArea

Posted 28 September 2005 - 06:43 AM

Just for the sake of trying, can you attempt including the following:
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JTextArea


Try it first without the last line, then with.
Was This Post Helpful? 0
  • +
  • -

#9 Fame Commeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-August 05

Re: Setting Font in JTextArea

Posted 28 September 2005 - 06:51 AM

I still get the same error... Is there a syntax other than what I'm trying to use for it to work?
Was This Post Helpful? 0
  • +
  • -

#10 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Setting Font in JTextArea

Posted 28 September 2005 - 01:10 PM

Im not sure what SDK are you using, but I've just checked JavaTM 2 Platform
Standard Ed. 5.0 API and there is no such method of JTextArea what you are trying to use - setFont(java.lang.String). I think you need: setFont(java.awt.Font). Amadeus copied the link to this method, also :)

A sample code looks like this:
jTextArea1.setFont(new java.awt.Font("Lucida Console", 0, 10));



I hope this helps!
Was This Post Helpful? 1
  • +
  • -

#11 Fame Commeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-August 05

Re: Setting Font in JTextArea

Posted 28 September 2005 - 02:42 PM

Thank you all a whole bunch, finally got it working. Thanks 1lacca, your snippit of code got it rolling for me. Output's finally lined up now too.

Thanks,
Fame
Was This Post Helpful? 0
  • +
  • -

#12 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Setting Font in JTextArea

Posted 28 September 2005 - 02:57 PM

Thanks 1lacca and Amadeus for helping out, you guys rock!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1