• Welcome to Valhalla Legends Archive.
 

I'm trying to use a List but I get an error I can't debug!

Started by Sorc.Polgara, September 07, 2005, 01:26 PM

Previous topic - Next topic

Sorc.Polgara

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)

Here is the 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;
}
}


Here is the Tester program:

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



Please help.

Kp

According to the backtrace, your closing brace on main is causing a problem.  Have you tried disabling it? ;)
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Sorc.Polgara

Quote from: Kp on September 07, 2005, 08:27 PM
According to the backtrace, your closing brace on main is causing a problem.  Have you tried disabling it? ;)

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.

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 .