Valhalla Legends Forums Archive | Java Programming | I'm trying to use a List but I get an error I can't debug!

AuthorMessageTime
bethra
Ok, I've spent a couple hours trying to figure out what this error I'm getting means and how to fix it.  I've surfed google trying to see if I can get any hints on what the problem might be but I've been unsuccessful!  Please help.

Here is the error message:
[QUOTE]
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint
        at java.awt.GridBagLayout.addLayoutComponent(GridBagLayout.java:608)
        at java.awt.Container.addImpl(Container.java:1058)
        at java.awt.Container.add(Container.java:899)
        at WindowLayout.makeList(WindowLayout.java:88)
        at WindowLayout.makeLayout(WindowLayout.java:101)
        at WindowLayout2.getWindow(WindowLayout2.java:60)
        at Tester.main(Tester.java:11)
[/QUOTE]

Here is the Code:
[code]
import javax.swing.*;
import java.awt.*;
import java.awt.BorderLayout;
import java.util.*;
import java.applet.Applet;


public class WindowLayout2 extends Applet
{
private DefaultListModel listModel;
private JList list;

public WindowLayout2(){
//
setLayout(new BorderLayout());
}

protected void makeList(GridBagLayout gridbag, GridBagConstraints c)
{
JList list = new JList();

gridbag.setConstraints(list, c);

                listModel = new DefaultListModel();
                listModel.addElement("Holy Light");
                listModel.addElement("Denial");
                listModel.addElement("Teleport");
                listModel.addElement("Chakra Magic");

                list = new JList(listModel);
                list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                list.setSelectedIndex(0);
                list.setVisibleRowCount(-1);
                JScrollPane listScrollPane = new JScrollPane(list);

                add(listScrollPane, BorderLayout.CENTER);
add(list);
}

public void makeLayout()
{
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

setFont(new Font("Helvetica", Font.PLAIN, 14));
setLayout(gridbag);

c.weightx = 1.0;
makeList(gridbag, c);

setSize(300, 100);
}

public JFrame getWindow()
{
JFrame f = new JFrame("List Test");
WindowLayout2 ex1 = new WindowLayout2();

                ex1.makeLayout();                        // Line 60

f.add("Center", ex1);
f.pack();
f.setSize(f.getPreferredSize());
f.show();

return f;
}
}
[/code]

Here is the Tester program:
[code]
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Tester{
    public static void main(String[] args)
    {
    WindowLayout2 windowLayout = new WindowLayout2();
    JFrame frame = new JFrame();
        frame = windowLayout.getWindow();          // Line 10
    }
}
[/code]


Please help.
September 7, 2005, 6:26 PM
Kp
According to the backtrace, your closing brace on main is causing a problem.  Have you tried disabling it? ;)
September 8, 2005, 1:27 AM
bethra
[quote author=Kp link=topic=12736.msg127351#msg127351 date=1126142832]
According to the backtrace, your closing brace on main is causing a problem.  Have you tried disabling it? ;)
[/quote]

wtf, I don't see how my closing brace on the main function in the tester would be causing a problem... I need that closing brace there.
September 16, 2005, 2:50 AM
Dynobird
You can't instantiate List, it's abstract. That's your error. Try ArrayList or LinkedList, they're some of its derived concrete classes. Or try using my classes for your lists, posted here .
October 12, 2005, 11:15 PM

Search