我正在制作一个程序,该程序将为从Excel工作表中提取的名称生成电子邮件。 我遇到的问题是,当我尝试将Arraylist数据传输到Jlist ListModel时,Gui中没有任何显示。
我知道发布整个代码是过分的,但是id而不是过度共享,而不是人们要求其他部分以弄清楚发生了什么。问题完全在Main和Window类之间(很确定)。
这是用于创建人物对象的人物类
public class person {
private String fName, lName, serialNum, location, isDone;
// default constructor
public person() {
fName = "default";
lName = "default";
serialNum = "0";
location = "nowhere";
isDone = "";
}
public person(String serialNum1, String fName1, String lName1, String
location1, String isDone1) {
this.fName = fName1;
this.lName = lName1;
this.serialNum = serialNum1;
this.location = location1;
this.isDone = isDone1;
}
// set first name
public void setFirst(String newFirst) {
this.fName = newFirst;
}
// get first name
public String getFirst() {
return fName;
}
////////
// set last name
public void setLast(String newLast) {
this.lName = newLast;
}
// get last name
public String getLast() {
return lName;
}
/////////
// set serial number
public void setSerial(String newSerial) {
this.serialNum = newSerial;
}
// get serial number
public String getSerial() {
return serialNum;
}
/////////
// set location
public void setLocation(String newLocation) {
this.location = newLocation;
}
// get location
public String getLocation() {
return location;
}
// set done
public void setDone(String newDone) {
this.isDone = newDone;
}
// get done
public String getDone() {
return isDone;
}
@Override
public String toString() {
return ("serial: " + serialNum + " | Name: " + fName + " " + lName + " | Location: " + location + " | Completed: "+isDone+ "\n");
}
public String label() {
return " Name: " + fName + " " + lName + " done: "+isDone+ "\n";
}
///////////
// script///
///////////
public String printScript() {
return "Hello " + fName + " " + lName + "\n\n" +
"script"+
"Serial #: " + serialNum + "\n\n" +
"Location: " + location + "\n\n" +
;
}
}
主类读取excel文件并从中提取名称/数据。然后将数据放入Arr a。 'a'成功地将对象存储在arraylist中,但是当尝试使用'w.fill(a);'将'a'的内容移动到GUI上时它什么也没做。 gui会显示一个空白的jList。
public class Main extends JFrame {
/**
* Launch the application.
*/
// connect program to desired excel file
private static final String fileName = "C:\\bigList.xlsx";
public static Arr a = new Arr();
public static Window w = new Window();
public static void main(String[] args) throws URISyntaxException {
String subject = "title";
String body = "See%20it";
// temporary variables to store excel data for 1 row.
// probably some way to make it more efficient than using nextCell. go by column
// # or something
String serial = "";
String first = "";
String last = "";
String location = "";
// could be changed to boolean
String done = "";
int jump;
// create arraylist to store excel file
try {
// initialize reading of excel file
FileInputStream excelFile = new FileInputStream(new File(fileName));
// opens workbook for java to read
Workbook workbook = new XSSFWorkbook(excelFile);
// gets workbook sheet, usually 0, can be other values in case of
multiple
// sheets
Sheet datatypeSheet = workbook.getSheetAt(0);
// initialize iterator for new rows
Iterator<Row> iterator = datatypeSheet.iterator();
// while there is a row with data it will keep going
while (iterator.hasNext()) {
// increment var reset. stores iterator position
jump = 0;
// adds temp data to arraylist
a.getList().add(new person(serial, first, last, location,done));
// since only some columns of 'completed' are populated, the 'done' variable has
// to be reset to null
// otherwise the status will remain as yes, after the first yes, since that is
// the only other variable state
done = "";
// initialize row iterator
Row currentRow = iterator.next();
// initialize cell iterator
Iterator<Cell> cellIterator = currentRow.iterator();
// while there is data in a cell, it will keep iterating to the right to the
// next cell
while (cellIterator.hasNext()) {
// increments column/cell value
jump++;
// create cell object
Cell currentCell = cellIterator.next();
// if cell contains string & isnt null value, will jump to subgroup of if
// statements
if (currentCell.getCellType() == CellType.STRING &&
currentCell.getCellType() != null) {
// depending on position, store in corresponding temp value
// probably a better way to do this
// System.out.println(jump);
if (jump == 1) {
serial = currentCell.getStringCellValue();
}
if (jump == 2) {
first = currentCell.getStringCellValue();
}
if (jump == 3) {
last = currentCell.getStringCellValue();
}
if (jump == 4) {
location = currentCell.getStringCellValue();
}
if (jump == 5) {
done = currentCell.getStringCellValue();
}
} else if (currentCell.getCellType() == CellType.NUMERIC) {
// no numeric values so nothing needed
}
// if cellIncrementer encounters empty cell it breaks loop and moves down to
// next row
else if (currentCell.getCellType() == null) {
break;
}
}
}
// removes column headers row from list
a.getList().remove(0);
a.getList().remove(0);
// error catch messages
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/////////////////////////////////////////
System.out.println(a.getList());
// ISSUE
// 'a' arraylist in is not getting transferred to gui
w.fill(a);
w.run();
/////////////////////////////////////////////
}
// create outlook email.
// Desktop.getDesktop().mail( new URI(
// "mailto:prrout@inautix.co.in?subject="+subject+"&body="+body) );
}
生成GUI的窗口类
public class Window extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
/**
* Create the frame.
*/
DefaultListModel<person> listModel = new DefaultListModel<>();
public Window() {
JList<person> list = new JList<person>(listModel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
getContentPane().setLayout(new BorderLayout(0, 0));
JButton button = new JButton("Generate Email");
button.setFont(new Font("Tahoma", Font.PLAIN, 12));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
getContentPane().add(button, BorderLayout.SOUTH);
JScrollPane scrollPane = new JScrollPane(list);
getContentPane().add(scrollPane, BorderLayout.CENTER);
JLabel lblSelectPeople = new JLabel("Select Person(s)");
lblSelectPeople.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblSelectPeople.setHorizontalAlignment(SwingConstants.CENTER);
scrollPane.setColumnHeaderView(lblSelectPeople);
}
// needs to be variable 'a' from Main class
public void fill(Arr a) {
for (int i = 0; i < a.getList().size(); i++) {
listModel.addElement(a.getList().get(i));
}
}
public void run() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window frame = new Window();
frame.setVisible(true);
frame.getContentPane().setSize(800, 400);
frame.setBounds(200, 50, 630, 500);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}}
my arraylist class
public class Arr {
// create arraylist instance
ArrayList<person> arr = new ArrayList<person>();
// create getter unnecessary
public Arr() {
}
public ArrayList<person> getList() {
return this.arr;
}}
答案 0 :(得分:0)
您要创建多个Window变量,如果您在上面的代码中搜索new Window()
,就很容易证明这一点。它应该出现一次,并且您要调用两次。您在fill(...)
方法上调用run()
并为其提供数据,而在fill(...)
方法中创建的另一个窗口,您不要在其上调用import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
public class Main extends JFrame {
public static Arr a = new Arr();
public static Window w = new Window();
public static void main(String[] args) {
// fill the gui with dummy data for our purposes without need of
// outside excel libraries
for (int i = 0; i < 20; i++) {
String fName1 = "first name " + i;
String lName1 = "last name " + i;
a.getList().add(new Person(fName1, lName1));
}
a.getList().remove(0);
a.getList().remove(0);
System.out.println(a.getList());
w.fill(a);
w.run();
}
}
而是不显示数据。解决方案:不要这样做。创建一个对象,填充并显示它。
我的MCVE证明了这一点:
class Window extends JFrame {
private JPanel contentPane;
DefaultListModel<Person> listModel = new DefaultListModel<>();
public Window() {
JList<Person> list = new JList<Person>(listModel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
getContentPane().setLayout(new BorderLayout(0, 0));
JButton button = new JButton("Generate Email");
button.setFont(new Font("Tahoma", Font.PLAIN, 12));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
getContentPane().add(button, BorderLayout.SOUTH);
JScrollPane scrollPane = new JScrollPane(list);
getContentPane().add(scrollPane, BorderLayout.CENTER);
JLabel lblSelectPeople = new JLabel("Select Person(s)");
lblSelectPeople.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblSelectPeople.setHorizontalAlignment(SwingConstants.CENTER);
scrollPane.setColumnHeaderView(lblSelectPeople);
}
public void fill(Arr a) {
for (int i = 0; i < a.getList().size(); i++) {
listModel.addElement(a.getList().get(i));
}
}
public void run() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
// ******Don't create a new Window object!!!!!!!!!!!! **********
// Window frame = new Window();
// frame.setVisible(true);
// frame.getContentPane().setSize(800, 400);
// frame.setBounds(200, 50, 630, 500);
Window frame = Window.this;
frame.setVisible(true);
frame.getContentPane().setSize(800, 400);
frame.setBounds(200, 50, 630, 500);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
class Arr {
ArrayList<Person> arr = new ArrayList<Person>();
public ArrayList<Person> getList() {
return this.arr;
}
}
// Should be named Person, not person
class Person {
private String fName, lName;
public Person() {
fName = "default";
lName = "default";
}
public Person(String fName1, String lName1) {
this.fName = fName1;
this.lName = lName1;
}
public void setFirst(String newFirst) {
this.fName = newFirst;
}
public String getFirst() {
return fName;
}
public void setLast(String newLast) {
this.lName = newLast;
}
public String getLast() {
return lName;
}
@Override
public String toString() {
return (" | Name: " + fName + " " + lName);
}
public String label() {
return " Name: " + fName + " " + lName + " done: "
+ "\n";
}
public String printScript() {
return "Hello " + fName + " " + lName + "\n\n";
}
}
setPrototypeCellValue(...)
其他无关的问题:
person
以设置其宽度Person
类重命名为ArrayList<Person>
。您的代码应遵循{{3}} -变量名都应以小写字母开头,而类名应以大写字母开头。学习并遵循此规则将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。public ICommand Grabproxies { get; set; } = new RelayCommand(CreateProxy, true);
private void CreateProxy(object param)
{
Proxies.Add(pds.GrabProxy());
}
对象无法实现的有用目的。摆脱此类。