1

The following code works when the font line is commented out, and no GUI appears at all when the line is included. From what I can tell its formatted properly but its crashing the GUI. What could cause this?

public class TestCode extends JFrame{
JTextArea jta;
public TestCode(){
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    JPanel content = new JPanel();
    jta = new JTextArea(20, 30);
    jta.setFont(new Font("Courier New", Font.PLAIN, 12));  // This line crashes
    content.add(jta);
    add(content);         
    pack();
    setVisible(true);
}

public static void main (String [] args){

    TestCode run = new TestCode();

    }
}

I'm beginning to suspect that it has something to do with my system fonts? I have installed extra fonts, perhaps that affects Java's ability to retrieve them?

EDIT:

Just to clarify, there are no errors when I run this program. The GUI never opens and the IDE gets slow and buggy as if I were running an infinite loop. The program must be terminated via the IDE (because no GUI shows to close).

leigero
  • 3,233
  • 12
  • 42
  • 63
  • There are no errors. The GUI just never opens. It behaves as if I've somehow executed an infinite loop and it just never opens the GUI. The program has to be terminated from the IDE – leigero Sep 13 '13 at 15:27
  • Is it possibly opening the window behind the IDE? – Kylar Sep 13 '13 at 15:30
  • Nope, I've checked. the window just isn't there. – leigero Sep 13 '13 at 15:30
  • Also consider `deriveFont()`. – trashgod Sep 13 '13 at 15:32
  • If you remove the setFont() it works fine? – Kylar Sep 13 '13 at 15:32
  • Yes, when the setFont() line is gone everything works as expected. – leigero Sep 13 '13 at 15:34
  • While it's running in the debugger, hit the pause button and take a look at where the threads are. That might give you some insight. – Kylar Sep 13 '13 at 15:38
  • This code runs absolutely fine on my PC. No lag,.. nothing. I would suspect it is something other than your code which is at fault here. Perhaps try some other fonts... maybe there is an issue with that font on your machine. – ThePerson Sep 13 '13 at 15:40

2 Answers2

1

It works fine for me using 1.6 and 1.7.

Some suggestions:

1) Force the EDT for your Swing app as follows:

public static void main(String[] args)
{ 
  SwingUtilities.invokeLater(new Runnable()
  {
    public void run()
    {
      TestCode run = new TestCode();
    }
  });
}

Further reading:Concurreny in Swing

2) Place your JTextArea in a JScrollPane, and add the scroll pane to the panel, not the text area itself:

content.add(new JScrollPane(jta));
splungebob
  • 5,357
  • 2
  • 22
  • 45
0

The problem was caused by an excessive number of downloaded fonts on my system. I had previously downloaded a Font package which contained a few thousand additional fonts which caused the IDE to spend an unnecessarily long amount of time trying to find the right font I presume.

Deleting the unused additional fonts solved the problem and now this code works fine.

leigero
  • 3,233
  • 12
  • 42
  • 63
  • I'm curious to know if you had chance to try `deriveFont()` before deleting, for [example](http://stackoverflow.com/a/18328489/230513). – trashgod Sep 14 '13 at 00:20
  • @trashgod I did not, sorry. I would have attempted that had I not been on a quest to clean up the garbage fonts. I'd rather not re-install them to check either but you can feel free! – leigero Sep 14 '13 at 00:22