import javax.swing.*; import java.awt.*; import javax.swing.border.*; import jav
ID: 3630442 • Letter: I
Question
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.text.DecimalFormat;
public class WorkshopPanel extends JPanel
{
//instance variables
protected final String [] WORKSHOP_TYPE = {"IT Trends",
"Creating a Dream Career",
"Advanced Java Programming",
"Ethics: The Challenge Continues"};
protected final double [] WORKSHOP_COST ={295, 295, 395, 395};
protected ConferenceGUI gui;
protected JList listOptions;
protected JScrollPane listPane;
//Constructor
public WorkshopPanel()
{
setLayout(new FlowLayout());
//creates Jlist
JList listOptions = new JList(WORKSHOP_TYPE);
for(int i = 0; i < WORKSHOP_TYPE.length; i++)
{
listOptions[i] = WORKSHOP_TYPE[i];
add(listOptions[i]);
}
listOptions.setVisibleRowCount(4);
listOptions.setSelectionMode(4);
listPane = new JScrollPane(listOptions);
//sets window title
setBorder(BorderFactory.createTitledBorder("Workshops"));
}
public double getWorkshopCost()
{
for(int i = 0; i < listOptions.length; i++)
{
if(listOptions[i].isSelected())
{
workshopCost += WORKSHOP_COST[i];
}
}
return workshopCost;
}
public String getWorkshopList()
{
String workshopList;
workshopList += WORKSHOP_TYPE;
return workshopList;
}
}
Explanation / Answer
I'm not sure what exactly you're trying to do, but the modified code below compiles fine now. You had some illegal usage of the JList object and forgot to initialize and declare some variables. import javax.swing.*; import java.awt.*; import javax.swing.border.*; import java.text.DecimalFormat; public class WorkshopPanel extends JPanel { //instance variables protected final String [] WORKSHOP_TYPE = {"IT Trends", "Creating a Dream Career", "Advanced Java Programming", "Ethics: The Challenge Continues"}; protected final double [] WORKSHOP_COST ={295, 295, 395, 395}; //protected ConferenceGUI gui; protected JList listOptions; protected JScrollPane listPane; protected DefaultListModel listModel; //Constructor public WorkshopPanel() { setLayout(new FlowLayout()); //creates Jlist listModel = new DefaultListModel(); for(int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.