• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems using GridLayout  RSS feed

 
Fergal Crawley
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just recently started learning Java. I've been looking at GUI's and everytime I try to use a GridLayout in a program, I get an error message like the one below;

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: javax.swing.JFrame.setLayout
at GridLayout.main(GridLayout.java:10)
Java Result: 1


Below is an example of some code that is throwing up the error above;



The program runs, when I comment out line 10.

Can anyone please tell me why I am getting this error?

Thanks!
 
Ralph Cook
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You import java.awt.*; this means that GridLayout, used by itself, means java.awt.GridLayout. But you also define your own class as GridLayout. You can't have both.

'import' just means 'treat the imported class name as the one I mean when I use it without full qualification'. So the un-fully-qualified name must be unique. (full qualification means the entire package name plus the class name.

Rename your class to another name ("GLayout", or "TestGridLayout"). It runs when I do that.

rc
 
Bill Krieg
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following line:


No doubt what you're trying to do is set the layout to a new java.awt.GridLayout object. What you're actually doing is setting the layout to a new GridLayout object where GridLayout is the class you yourself are writing. You could change that line to be



But that would be wrong. The lesson to be learned is never give one of your classes the same name as a standard java class. Especially if you're planning to use objects of that class. Rename your class to GridLayoutTest or anything else that makes it easy to distinguish between your class and the standard java.awt.GridLayout.
 
Fergal Crawley
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies Ralph and Bill.

Bill Krieg wrote:...You could change that line to be


....


That fixes the issue on two different programs that I'm currently working on. However, changing the name of the class does not fix the issue - The error is still thrown up with the code below;



Not sure if it makes a difference but I'm using NetBeans.

Thanks!
 
Sagar Dabas
Ranch Hand
Posts: 47
C++ Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




[code=java]import javax.swing.*;
import java.awt.*;

public class GridTest {

public static void main(String[] args) {

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2, 3));
for (int i=1; i<=6; i++) {
frame.add(new JButton("Buton " + i));
}

frame.setTitle("Grid Layout");
frame.setSize(new Dimension(200, 200));


frame.setVisible(true);
}

Not sure if it makes a difference but I'm using NetBeans.

Thanks!


You missed a curly bracket " } "..... Otherwise its working fine...
 
Fergal Crawley
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Dabas wrote:

...You missed a curly bracket " } "..... Otherwise its working fine...


Thanks Sagar, sorry but I missed that curly bracket in my copy and paste, I have it in the program but it's still not working. I've been trying a couple of other GUI programs and I can only use frame.setLayout(new GridLayout(2, 3)); when I put java.awt. with it as in:
frame.setLayout(new java.awt.GridLayout(2, 3));

Thanks!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to delete your own GridLayout.class file that's still lying around somewhere on the classpath.
 
Fergal Crawley
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Darryl, that worked. I've a lot to learn
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!