2

I'm re doing a specific application, just a basic text editor and I remember I had tabs and a JMenu so if you went File --> New it would add or 'Open' another tab on the JTabbedPane. But this time it's not doing it for me, could someone help? Here is how im doing it:

newFile.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        tabs.addTab("new file", text);
                    }
                }
        );

So when it's clicked it should add another tab but it's not for some reason... If it matters there is a default tab open at the beginning and when you click new it wipes out the old one. Thanks for any help! (Please ask if you need anymore explanation) Here I uploaded my code here since the editor here kept saying I the way I was putting it in wasnt formatted correctly:
http://nardcake.com/java
There is 2 files there, one initializes it and the other is everything else thanks!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
NardCake
  • 130
  • 2
  • 9

1 Answers1

2

try:

tabs.revalidate();
tabs.repaint();

I have removed these two lines (those two are anyhow called in the end by addTab() method), and rewritten your init.java like this:

 public static void main(String[] args) {
 System.out.println(SwingUtilities.isEventDispatchThread()); // 1
 SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            System.out.println(SwingUtilities.isEventDispatchThread()); //2
            EBLFWE window = new EBLFWE();
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setExtendedState( window.getExtendedState()|JFrame.MAXIMIZED_BOTH );
            window.setSize(1024, 728);
            window.setVisible(true);
        }
    });

It works now. To quote myself:

Every usage of Swing components must be done thorugh the Event Dispatch Thread (abbreviated EDT) or you will probably get unwanted visual effects. See here for explanation.

EDIT:

All the GUI related code must be executed on the EDT. You can test if some part of your code is run by EDT like this:

 System.out.println(SwingUtilities.isEventDispatchThread());

If it prints true you are safe to do a GUI update (e.g. call methods on Swing components instances) - like in 1 or anywhere in EBLFWE class. However 2 will print false - it is because the thread that runs your program is not EDT.

When calling SwingUtilities.invokeLater() you are actually placing that code to be executed (at some appropriate time the EDT sees fit) in the Event dispatch thread.

EDT does the actual painting, and a lot of other tasks, so when you call GUI update code from another thread you can mess up the order and get unwanted visual apperance.

Community
  • 1
  • 1
linski
  • 5,046
  • 3
  • 22
  • 35
  • 1
    so in the action listener do `tabs.revalidate();`? I did but it didn't do anything... Somewhere else? – NardCake Oct 13 '12 at 20:55
  • yes, just below the addTab add *both* tabs.revalidate(); tabs.repaint(); If it doesn't work you ll have to post [all the relevant code](http://sscce.org/) – linski Oct 13 '12 at 20:58
  • Nope, Ok I post it all in the question here – NardCake Oct 13 '12 at 21:00
  • the interesting thing is though, that it works even as you posted, that's why you must know swing well before using it - cause it ll look spuroius and not even always :) pls feedback. – linski Oct 13 '12 at 21:18
  • Well thanks for helping but im not quite sure exactly what you did still, a bit more explanation please? – NardCake Oct 13 '12 at 21:26