I have done this many times before and now I am getting a weird error. Say "Type is not generic", I have all the necessary imports, I do not get it...???
I am just messing around with new better ways to make buttons.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I have done this many times before and now I am getting a weird error. Say "Type is not generic", I have all the necessary imports, I do not get it...???
I am just messing around with new better ways to make buttons.
This code doesn't make a lot of sense. Your buttons variable is a List, so why are you using the array index operator [i] on it?
Can you show us an MCVE (note: NOT your whole project!) that demonstrates the error, instead of this disconnected snippet?
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
It is a very small program so I will post the whole thing. The error says "Type is not generic" when I take my mouse and put it under the red line that is under List.
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parameterized with arguments <JButton> at testButtonsLoop.TestButton.numberButton(TestButton.java:34) at testButtonsLoop.TestButton.<init>(TestButton.java:24) at testButtonsLoop.TestButton.main(TestButton.java:48)
package testButtonsLoop; import java.awt.FlowLayout; import java.awt.List; import java.util.ArrayList; import javax.swing.*; public class TestButton { JFrame frame; TestButton() { frame=new JFrame(); frame.setSize(250,350); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); //gets panel from numberButton method panel = numberButton(panel); frame.add(panel); frame.setVisible(true); } //creates a matrix of buttons and sets them into a list. JPanel numberButton(JPanel panel) { List<JButton> buttons = new ArrayList<JButton>(12); for(int i=0;i<12;i++) { buttons[i] = i; buttons.add(new JButton()); panel.add(buttons[i]); } return panel; } public static void main(String[] args) { new TestButton(); } }
I am experimenting here, I am trying to find a good way to make buttons name them 1-12 and set there values to be displayed 1-12. Then I was going to string names...This code doesn't make a lot of sense. Your buttons variable is a List, so why are you using the array index operator [i] on it?
As I wrote this it just dawned on me that I should create a string array and loop through it assigning its values to the button...
like this:
//creates a matrix of buttons and sets them into a list. JPanel numberButton(JPanel panel) { String[] names = {"1","2","3","4","5","6"}; List<JButton> buttons = new ArrayList<JButton>(6); //Error on this line for(int i=0;i<6;i++) { buttons[i] = names[i]; buttons.add(new JButton()); panel.add(buttons[i]); } return panel; }
Ah, I see. you've imported java.awt.List, which is an AWT component. It is indeed not generic, hence the error.
You're looking for java.util.List, which is an interface. It *is* generic.
However, once you fix that, you'll see the other problems I mentioned above. A List is not an array.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Your real code might be doing more than this, but note that the code you posted doesn't really need the ArrayList at all.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
I thought about what you said and I changed a few things... Now I am having a different issue as well. I read the API and it appears I am implementing it correctly.
frame.add(panel,FlowLayout.CENTER);
Gives me the following error
Exception in thread "main" java.lang.IllegalArgumentException: illegal component position at java.awt.Container.addImpl(Container.java:1080) at java.awt.Container.add(Container.java:998) at javax.swing.JFrame.addImpl(JFrame.java:562) at java.awt.Container.add(Container.java:460) at testButtonsLoop.TestButton.<init>(TestButton.java:29) at testButtonsLoop.TestButton.main(TestButton.java:55)
package testButtonsLoop; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*; public class TestButton { JFrame frame; TestButton() { frame=new JFrame(); frame.setSize(250,350); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); //gets panel from numberButton method panel = numberButton(panel);// //centers panel onto middle of frame frame.add(panel,FlowLayout.CENTER);//error here but fine is I remove FlowLayout.CENTER frame.setVisible(true); } //creates a matrix of buttons and sets them into a list. JPanel numberButton(JPanel panel) { String[] names = {"1","2","3","4","5","6"}; for(int i=0;i<6;i++) { JButton[] b = new JButton[6]; b[i] = new JButton(names[i]); b[i].setPreferredSize(new Dimension(50,25)); panel.add(b[i]); } panel.setLayout(new GridLayout(3,3)); panel.setBackground(Color.BLACK); return panel; } public static void main(String[] args) { new TestButton(); } }
Error is coming from line 29
What exactly do you think FlowLayout.CENTER is doing?
Recommended reading: http://docs.oracle.com/javase/tutori...yout/grid.html
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Centering the panel onto the frame... Obviously it's not doing what I thought
I am even trying to use BorderLayout and this is my issue. The buttons are huge despite me setting there size. They are the correct size when I use FLowLayout(). However when I try to center them I get errors as I already stated in post #7. Now I am using BorderLayout and the whole whole covered by huge buttons.
package testButtonsLoop; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*; public class TestButton { JFrame frame; TestButton() { frame=new JFrame(); frame.setSize(250,350); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JPanel textPanel = new JPanel(); //gets panel from numberButton method panel = numberButton(panel); textPanel = textBox(textPanel); //get a JTextField from method and sets it to top of JFrame frame.add(textPanel,BorderLayout.NORTH); //centers panel onto middle of frame frame.add(panel,BorderLayout.CENTER); frame.setVisible(true); } //creates a matrix of buttons and sets them into a list. JPanel numberButton(JPanel panel) { String[] names = {"1","2","3","4","5","6"}; for(int i=0;i<6;i++) { JButton[] b = new JButton[6]; b[i] = new JButton(names[i]); b[i].setPreferredSize(new Dimension(50,25)); panel.add(b[i]); } panel.setLayout(new GridLayout(3,3)); panel.setBackground(Color.BLACK); return panel; } JPanel textBox(JPanel panel) { JTextField textBox = new JTextField(12); textBox.setPreferredSize(new Dimension(15,22)); panel.add(textBox); return panel; } public static void main(String[] args) { new TestButton(); } }
You use the GridLayout. The GridLayout makes the components as big as fits the grid.
You should probably use a different layout within your button panel if you want the buttons to have a different size.
By the way, setting PreferredSize is a very ugly way of doing things and is not recommended.
I used flowLayout inside the Panel and just resized the Frame in the constructor so the buttons are where I want. I was just thought or hoped there was a better way to make a matrix of buttons.
Of course there is, get a better layout.