1

I have this java code:

Editor()
{
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
        public void run() 
        {
            // Create Menu
            JMenuBar menuBar = drawMenu();
            frame.setJMenuBar(menuBar);

    frame.setUndecorated(true);
    frame.add(desktop, BorderLayout.CENTER);
            frame.add(toolbar,BorderLayout.NORTH);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.getRootPane().setWindowDecorationStyle(JRootPane.INFORMATION_DIALOG);
            frame.setTitle("JIL");
            frame.pack();
            frame.setVisible(true);
        }
});
  }

public JMenuBar drawMenu() 
{
    // Create the menu bar
    JMenuBar menuBar = new JMenuBar();
    // Create a menu
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    JMenu run = new JMenu("Build");
    JMenu help = new JMenu("Help");
    menuBar.add(file);
    menuBar.add(edit);
    menuBar.add(run);
    menuBar.add(help);

    JMenu _new = new JMenu("New");
    file.add(_new);

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    JButton clsbutton=new JButton(new ImageIcon (classLoader.getResource ("img/cls.jpg")));
    clsbutton.setSize(2, 2);
    clsbutton.setToolTipText("New Class");
    toolbar.add(clsbutton);
    clsbutton.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent event) 
        {   
            new newClass();
        }
    });
       }

newClass()
    {
contentPanelC.removeAll(); 
getContentPane().removeAll();
setTitle("New Class");
setBounds(100, 100, 470, 400);
getContentPane().setLayout(new BorderLayout());
contentPanelC.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanelC, BorderLayout.CENTER);
contentPanelC.setLayout(null);

JLabel lblNewLabel = new JLabel("Class Name");
lblNewLabel.setBounds(50, 51, 120, 17);
contentPanelC.add(lblNewLabel);
contentPanelC.revalidate();
contentPanelC.repaint();

textField = new JTextField();
textField.setBounds(170, 51, 228, 20);
contentPanelC.add(textField);
contentPanelC.revalidate();
contentPanelC.repaint();
textField.setColumns(10);
textField.setText(null);
textField.revalidate();
textField.repaint();

String fnam=textField.getText();
System.out.println("Initial value TextField: "+fnam);

JLabel lblNewLabel_1 = new JLabel("Location");
lblNewLabel_1.setBounds(50, 111, 120, 17);
contentPanelC.add(lblNewLabel_1);
contentPanelC.revalidate();
contentPanelC.repaint();

textField_1 = new JTextField();
textField_1.setBounds(170, 111, 131, 20);
contentPanelC.add(textField_1);
contentPanelC.revalidate();
contentPanelC.repaint();
textField_1.setColumns(10);
textField_1.setText(null);
textField_1.revalidate();
textField_1.repaint();

final JButton btnNewButton = new JButton("Browse");
btnNewButton.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent arg0) 
    {
        String workingdir = System.getProperty("user.dir");
        chooser = new JFileChooser(new File(workingdir));
        chooser.setDialogTitle("Select Location");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
        {
          File selectedFile = chooser.getSelectedFile();
          System.out.println(selectedFile.getName());
          textField_1.setText(selectedFile.getPath());
        } 
        else 
        {
          System.out.println("No Selection ");
        }
        btnNewButton.revalidate();
    }
});

btnNewButton.setBounds(320, 111, 89, 20);
contentPanelC.add(btnNewButton);
contentPanelC.revalidate();
contentPanelC.repaint();


{

    JPanel buttonPaneC = new JPanel();
    buttonPaneC.removeAll();
    buttonPaneC.setLayout(new FlowLayout(FlowLayout.RIGHT));
    getContentPane().add(buttonPaneC, BorderLayout.SOUTH);
    {
        final JButton okButton = new JButton("Ok");
        okButton.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                {
                // some file operations
                }
                textField.setText("");
                textField.revalidate();
                textField.repaint();

                textField_1.setText("");
                textField_1.revalidate();
                textField_1.repaint();

                dispose();

            }
        });
        okButton.setActionCommand("Ok");
        buttonPaneC.add(okButton);
        buttonPaneC.revalidate();
        buttonPaneC.repaint();
        getRootPane().setDefaultButton(okButton);

    }
    {
        final JButton cancelButton = new JButton("Cancel");
        cancelButton.setActionCommand("Cancel");
        buttonPaneC.add(cancelButton);
        buttonPaneC.revalidate();
        buttonPaneC.repaint();

        contentPanelC.validate();
        contentPanelC.repaint();
        cancelButton.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent arg0) 
            {
                dispose();
            }
        });

    }
}
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
contentPanelC.validate();
contentPanelC.repaint();}}

All time this code results in the following exception:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable.
at java.awt.Frame.setUndecorated(Frame.java:825)
at Editor$1.run(Editor.java:93)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:673)
at java.awt.EventQueue.access$300(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:634)
at java.awt.EventQueue$2.run(EventQueue.java:632)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:643)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

I have tried my best. What is this? Even this exception is shown in console the output window appears. But this troubles when I'm trying to debug the code. I have goggled about this. But no solution found. Somebody please help me.

Salini
  • 11
  • 1
  • 2
  • 8

4 Answers4

5

Try calling

this.setUndecorated(true);

at the JDialog constructor:

Example:

public JDialog() {
    this.setUndecorated(true);
    initComponents();
}

It worked here.

Felipe Carminati
  • 297
  • 1
  • 5
  • 16
3

I had the same problem trying to call .setUndecorated(true) after .pack(). So it should be not only before .setVisible(true), but also before .pack().

Alexander Samoylov
  • 2,358
  • 2
  • 25
  • 28
2

You must call

setUndecorated(true);

before you set that frame visible.

wangxt
  • 77
  • 7
1

What I did for making it go fullscreen after being visible, I made a method just for initializing and making the frame visible, with a boolean parameter for fullscreen. In it, there was an if statement for if the boolean was true, to setUndecorated(true) and setExtendedState(JFrame.MAXIMIZED_BOTH) inside it. In a fullscreen method, I use frame.dispose() and then re-initialize it by calling the method with a true parameter, that way it will go fullscreen. This worked for me.

Garhoogin
  • 91
  • 2
  • 9