Reputation: 45
I am having a lot of trouble getting this Jlist to work. The problem is occuring when I attempt to construct the JList. When I compile I get a message saying:
"error: constructor JList in class JList cannot be applied to given types; list = new JList(model);
required: no arguments
found: DefaultListModel
reason: actual and formal argument lists differ in length"
Also, I am having trouble adding elements to the DefaultListModel. If you un-comment the code where I attempt to add strings to the model, I get a message saying:
JList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Below I have copied my entire program so if you would like you can copy this and compile it yourself. Thank you!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
import java.io.*;
public class JList
{
public static void main (String args[])
{
new MyFrameClass();
}
}
class MyFrameClass extends JFrame implements ActionListener
{
JMenuBar menuBar;
JMenu fileMenu, itemMenu;
JMenuItem loadMenuItem, saveMenuItem, saveAsMenuItem, newMenuItem, deleteMenuItem, deleteAllMenuItem;
JPanel menuBarPanel;
JFileChooser fc;
File file;
JList list;
DefaultListModel model;
MyFrameClass()
{
Container cp;
fc = new JFileChooser();
menuBar = new JMenuBar();
menuBar.setLayout(new FlowLayout());
fileMenu = new JMenu("File");
itemMenu = new JMenu("Item");
loadMenuItem = new JMenuItem("Load");
loadMenuItem.addActionListener(this);
saveMenuItem = new JMenuItem("Save");
saveMenuItem.addActionListener(this);
saveAsMenuItem = new JMenuItem("SaveAs");
saveAsMenuItem.addActionListener(this);
newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(this);
deleteMenuItem = new JMenuItem("Delete");
deleteMenuItem.addActionListener(this);
deleteAllMenuItem = new JMenuItem("DeleteAll");
deleteAllMenuItem.addActionListener(this);
fileMenu.add(loadMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
itemMenu.add(newMenuItem);
itemMenu.add(deleteMenuItem);
itemMenu.add(deleteAllMenuItem);
menuBar.add(fileMenu);
menuBar.add(itemMenu);
menuBarPanel = new JPanel();
menuBarPanel.setLayout(new FlowLayout());
menuBarPanel.add(menuBar);
model = new DefaultListModel();
list = new JList(model);
//model.addElement("USA");
/*model.addElement("India");
model.addElement("Vietnam");
model.addElement("Canada");
model.addElement("Denmark");
model.addElement("France");
model.addElement("Great Britain");
model.addElement("Japan");
*/
cp = getContentPane();
cp.add(menuBarPanel);
setupMainFrame();
}
void setupMainFrame()
{
Toolkit tk;
Dimension d;
tk = Toolkit.getDefaultToolkit();
d = tk.getScreenSize(); // Get screen resolution.
setSize(d.width/2, d.height/2); // Set size and location based
setLocation(d.width/4, d.height/4); // on the resolution.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JList Project"); // For the title bar
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == loadMenuItem )
{
System.out.print("load");
int decision = fc.showOpenDialog(this);
if (decision == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
System.out.print("Opening: " + file.getName() + "." + "\n");
}
}
else if( e.getSource() == saveMenuItem )
{
System.out.print("save");
int decision = fc.showSaveDialog(this);
if (decision == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
}
}
else if( e.getSource() == saveAsMenuItem )
{
System.out.print("save as");
int decision = fc.showSaveDialog(this);
if (decision == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
}
}
else if( e.getSource() == newMenuItem )
{
System.out.print("new");
}
else if( e.getSource() == deleteMenuItem )
{
System.out.print("delete");
}
else if( e.getSource() == deleteAllMenuItem )
{
System.out.print("delete all");
}
}
}
Upvotes: 0
Views: 1570
Reputation: 285430
Sigh....
You've named your own class JList
:
public class JList
{
public static void main (String args[])
{
new MyFrameClass();
}
}
and so have created a name class for the compiler. Because of this, when you create a JList variable it is of your JList type, not javax.swing.JList
, and when you call a constructor, the compiler looks for one for your own JList class, a constructor which does not exist.
Solution: Don't do this, don't give your classes names that clash with those of critical core Java classes, but instead change the name of your class to something different, e.g.,
public class JListTest
{
public static void main (String args[])
{
new MyFrameClass();
}
}
Upvotes: 2