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.

Results 1 to 13 of 13

Thread: List with ArrayList

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default List with ArrayList

    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...???

    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;
    	}

    I am just messing around with new better ways to make buttons.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: List with ArrayList

    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!

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: List with ArrayList

    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();
     
    	}
     
    }

    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?
    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...

    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;
    	}

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: List with ArrayList

    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!

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: List with ArrayList

    Ahhhh.. Thank you

    JPanel numberButton(JPanel panel)
    	{
    		String[] names = {"1","2","3","4","5","6"};
    		List<JButton> buttons = new ArrayList<JButton>(6);
     
    		for(int i=0;i<6;i++)
    		{
    			buttons.add(new JButton(names[i]));
    			panel.add(buttons.get(i));
    		}
     
    		return panel;
    	}

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: List with ArrayList

    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!

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: List with ArrayList

    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

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: List with ArrayList

    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!

  9. #9
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: List with ArrayList

    Centering the panel onto the frame... Obviously it's not doing what I thought

  10. #10
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: List with ArrayList

    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();
     
    	}
     
    }

  11. #11
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: List with ArrayList

    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.

  12. #12
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: List with ArrayList

    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.

  13. #13
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: List with ArrayList

    Of course there is, get a better layout.

Similar Threads

  1. [SOLVED] ArrayList<node> list
    By jocdrew21 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 3rd, 2014, 04:22 PM
  2. getting data from arraylist into a drop down list
    By nallamchaitanya in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2013, 04:42 AM
  3. Arraylist of averages of two arrays list
    By Hazmat210 in forum Collections and Generics
    Replies: 11
    Last Post: April 5th, 2012, 07:38 PM
  4. Array List; How to pass objects into another arraylist
    By Melvrick in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 05:55 AM
  5. Need help outputting sub-list of an ArrayList
    By Allusive in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2010, 09:20 AM