I'm trying to convert a list to an array, but I get an error and I can't figure out why. I'm taking the current hour and in a for loop I get the rest of the hours of today and put it in a list. When i try to change it to an array I get an error. I try to do this because later I use that array in a JCombobox
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.GregorianCalendar;
import java.util.Calendar;
public class Frame extends JFrame implements ActionListener{
//JFrame elements
private JButton btnSetTime;
private JLabel lblTitle;
private JComboBox comboTime;
//Standard elements
private Timer tillPopup, tillShutdown;
Calendar calendar = new GregorianCalendar();
int hour = (calendar.get(Calendar.HOUR_OF_DAY));
List times = createDropdown(hour);
// Convert ArrayList to array which can be used in the combobox
String[] dropdownElements = times.toArray();
String[] a = {"a","b"};
public Frame(){
setLayout(new FlowLayout());
//Labels
lblTitle = new JLabel("Deze applicatie sluit u computer automatisch af om het energieverbruik te verminderen.");
//Combobox
comboTime = new JComboBox(a);
comboTime.setSelectedIndex(0);
//Button
btnSetTime = new JButton("Zet afsluittijd");
//Timers
//tillPopup = new Timer(this);
//tillShutdown = new Timer(this);
//Add elements to Frame
add(lblTitle);
add(comboTime);
add(btnSetTime);
//Add actionListeners
btnSetTime.addActionListener(this);
setSize(500,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private List createDropdown(int hour){
List availableHours = new List();
for(int i = hour; i <=24; i++){
if (i != 24){
availableHours.add(i + ":00");
}
else if(i == 24){
availableHours.add("00:00");
}
}
return availableHours;
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == btnSetTime){
Object popupTime = comboTime.getSelectedItem();
System.out.println(popupTime);
}
}
}
I get the following error:
Frame.java:21: error: cannot find symbol
String[] dropdownElements = times.toArray();
^
symbol: method toArray()
location: variable times of type List
How do I change the list I get back from the method createDropdown to an array and why is what I did wrong?