需要帮助将链表实现到TableViewjavaFX中并将其保存到文件中



我要做的一项任务涉及保存和写入文件(不一定是.XML文件,但最好是将其作为.XML文件(,最重要的是,我们不能使用任何现有的Java集合或数据结构类(例如ArrayList、LinkedList或任何其他实现Collection接口或其任何子级的类(。所以我尝试在项目中实现一个双重链接列表,但我不理解它的概念,我也不知道我是否正确地实现了它,因为我正在查找论坛和其他指南以寻求帮助。我还需要将链接列表保存到文件中的帮助或建议。我已经尝试过通过封送和解组列表来使用JAXB maven插件,但我需要首先有一个可工作的链表才能做到这一点。除非使用XStream插件会更好吗?最后是使用filewriter,但无论如何,这就是我的知识的终点。请记住,我希望表视图显示列表,例如用户输入所需的文本和要在表视图上显示的文本(我已经这样做了(,但文本可以保存到列表中,稍后从中读取。XML文件。

这是我的节目FXML文件


<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.control.TextField?>
<Pane fx:id="showPane" fx:controller="Controllers.showController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TableView fx:id="showTableView" layoutX="50.0" prefHeight="275.0" prefWidth="550.0">
<columns>
<TableColumn fx:id="setShowName" prefWidth="85.0" text="Name" />
<TableColumn fx:id="setShowMinutes" prefWidth="59.0" text="Minutes" />
<TableColumn fx:id="setStartDate" prefWidth="75.0" text="Start Date" />
<TableColumn fx:id="setEndDate" prefWidth="67.0" text="End Date" />
<TableColumn fx:id="setStallsTickets" prefWidth="79.0" text="Stalls Prices" />
<TableColumn fx:id="setCirclesTickets" prefWidth="81.0" text="Circles Prices" />
<TableColumn fx:id="setBalconyPrices" prefWidth="105.0" text="Balcony Prices" />
</columns>
</TableView>
<AnchorPane fx:id="homeAnchorPane" prefHeight="50.0" prefWidth="50.0">
<children>
<Button fx:id="homeButton" layoutX="11.0" layoutY="13.0" mnemonicParsing="false" onAction="#goHome" text="🏠" />
</children>
</AnchorPane>
<AnchorPane fx:id="addShowAnchorPane" layoutY="275.0" prefHeight="125.0" prefWidth="600.0">
<children>
<TextField fx:id="showTitle" layoutY="14.0" promptText="Show Title" />
<TextField fx:id="showMinutes" layoutX="226.0" layoutY="14.0" promptText="Minutes" />
<TextField fx:id="startDate" layoutX="437.0" layoutY="14.0" promptText="Start Date" />
<TextField fx:id="endDate" layoutY="50.0" promptText="End Date" />
<TextField fx:id="setStallsTicketPrices" layoutX="226.0" layoutY="50.0" promptText="Stalls Ticket Prices" />
<TextField fx:id="setCirclesTicketPrices" layoutX="437.0" layoutY="50.0" promptText="Circles Ticket Prices" />
<TextField fx:id="setBalconyTicketPrices" layoutY="86.0" promptText="Balcony Ticket Prices" />
<Button fx:id="saveAndFinishButton" layoutX="485.0" layoutY="86.0" mnemonicParsing="false" onAction="#addSave" text="Add 💾" />
</children>
</AnchorPane>
<Label layoutX="11.0" layoutY="62.0" text="West" />
<Label layoutX="15.0" layoutY="79.0" text="End" />
<Label layoutX="4.0" layoutY="96.0" text="Theatre" />
<Label layoutY="113.0" text="Management">
<font>
<Font name="System Bold" size="8.0" />
</font>
</Label>
<Label layoutX="5.0" layoutY="125.0" text="System" />
</children>
</Pane>

这是我的表演类

package sample;
import API.LocalDateAdapter;
import javafx.beans.property.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.time.LocalDate;
@XmlRootElement(name = "show")
@XmlType(propOrder = {"showTitle", "minutes", "startDate", "endDate", "stallsTicketPrices", "circlesTicketPrices", "balconyTicketPrices"})
public class Show {
private SimpleStringProperty showTitle;
private SimpleIntegerProperty minutes;
private ObjectProperty<LocalDate> startDate;
private ObjectProperty<LocalDate> endDate;
private SimpleDoubleProperty stallsTicketPrices;
private SimpleDoubleProperty circlesTicketPrices;
private SimpleDoubleProperty balconyTicketPrices;
public Show() {
}
@XmlAttribute
public void setShowTitle (SimpleStringProperty showTitle) {
this.showTitle = showTitle;
}
@XmlAttribute
public void setMinutes (SimpleIntegerProperty minutes) {
this.minutes = minutes;
}
@XmlJavaTypeAdapter(value = LocalDateAdapter.class)
public void setStartDate (ObjectProperty<LocalDate> startDate) {
this.startDate = startDate;
}
@XmlJavaTypeAdapter(value = LocalDateAdapter.class)
public void setEndDate (ObjectProperty<LocalDate> endDate) {
this.endDate = endDate;
}
@XmlAttribute
public void setStallsTicketPrices (SimpleDoubleProperty stallsTicketPrices) {
this.stallsTicketPrices = stallsTicketPrices;
}
@XmlAttribute
public void setCirclesTicketPrices (SimpleDoubleProperty circlesTicketPrices) {
this.circlesTicketPrices = circlesTicketPrices;
}
@XmlAttribute
public void setBalconyTicketPrices (SimpleDoubleProperty balconyTicketPrices) {
this.balconyTicketPrices = balconyTicketPrices;
}

/*public Show(){
this.showTitle = "";
this.minutes = 0;
this.startDate = LocalDate.now();
this.endDate = LocalDate.now();
this.stallsTicketPrices = 0;
this.circlesTicketPrices = 0;
this.balconyTicketPrices = 0;
}
public Show(int minutes, LocalDate startDate, LocalDate endDate, int stallsTicketPrices, int circlesTicketPrices, int balconyTicketPrices) {
this.minutes = minutes;
this.startDate = startDate;
this.endDate = endDate;
this.stallsTicketPrices = stallsTicketPrices;
this.circlesTicketPrices = circlesTicketPrices;
this.balconyTicketPrices = balconyTicketPrices;
}*/
public Show(String showTitle, int minutes, LocalDate startDate, LocalDate endDate, double stallsTicketPrices, double circlesTicketPrices, double balconyTicketPrices) {
this.showTitle =  new SimpleStringProperty(showTitle);
this.minutes = new SimpleIntegerProperty(minutes);
this.startDate = new SimpleObjectProperty<>(startDate);
this.endDate = new SimpleObjectProperty<>(endDate);
this.stallsTicketPrices = new SimpleDoubleProperty(stallsTicketPrices);
this.circlesTicketPrices = new SimpleDoubleProperty(circlesTicketPrices);
this.balconyTicketPrices = new SimpleDoubleProperty(balconyTicketPrices);
}
public String getShowTitle() {
return showTitle.get();
}
public SimpleStringProperty showTitleProperty() {
return showTitle;
}
public void setShowTitle(String showTitle) {
this.showTitle.set(showTitle);
}
public int getMinutes() {
return minutes.get();
}
public SimpleIntegerProperty minutesProperty() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes.set(minutes);
}
public LocalDate getStartDate() {
return startDate.get();
}
public ObjectProperty<LocalDate> startDateProperty() {
return startDate;
}
public void setStartDate(LocalDate startDate) {
this.startDate.set(startDate);
}
public LocalDate getEndDate() {
return endDate.get();
}
public ObjectProperty<LocalDate> endDateProperty() {
return endDate;
}
public void setEndDate(LocalDate endDate) {
this.endDate.set(endDate);
}
public double getStallsTicketPrices() {
return stallsTicketPrices.get();
}
public SimpleDoubleProperty stallsTicketPricesProperty() {
return stallsTicketPrices;
}
public void setStallsTicketPrices(double stallsTicketPrices) {
this.stallsTicketPrices.set(stallsTicketPrices);
}
public double getCirclesTicketPrices() {
return circlesTicketPrices.get();
}
public SimpleDoubleProperty circlesTicketPricesProperty() {
return circlesTicketPrices;
}
public void setCirclesTicketPrices(double circlesTicketPrices) {
this.circlesTicketPrices.set(circlesTicketPrices);
}
public double getBalconyTicketPrices() {
return balconyTicketPrices.get();
}
public SimpleDoubleProperty balconyTicketPricesProperty() {
return balconyTicketPrices;
}
public void setBalconyTicketPrices(double balconyTicketPrices) {
this.balconyTicketPrices.set(balconyTicketPrices);
}
}

这是我的显示控制器类

package Controllers;
import API.Save;
import Lists.ShowList;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import sample.Show;
import javax.xml.bind.JAXBException;
import java.io.*;
import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;
public class showController implements Initializable {
public Pane showPane;
public TableView<Show> showTableView;
public TableColumn<Show,String> setShowName;
public TableColumn<Show,Integer> setShowMinutes;
public TableColumn<Show,LocalDate> setStartDate;
public TableColumn<Show,LocalDate> setEndDate;
public TableColumn<Show,Double> setStallsTickets;
public TableColumn<Show,Double> setCirclesTickets;
public TableColumn<Show,Double> setBalconyPrices;
public AnchorPane homeAnchorPane;
public Button homeButton;
public AnchorPane addShowAnchorPane;
public TextField showTitle;
public TextField showMinutes;
public TextField startDate;
public TextField endDate;
public TextField setStallsTicketPrices;
public TextField setCirclesTicketPrices;
public TextField setBalconyTicketPrices;
public Button saveAndFinishButton;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
setShowName.setCellValueFactory(new PropertyValueFactory<>("showTitle"));
setShowMinutes.setCellValueFactory(new PropertyValueFactory<>("minutes"));
setStartDate.setCellValueFactory(new PropertyValueFactory<>("startDate"));
setEndDate.setCellValueFactory(new PropertyValueFactory<>("endDate"));
setStallsTickets.setCellValueFactory(new PropertyValueFactory<>("stallsTicketPrices"));
setCirclesTickets.setCellValueFactory(new PropertyValueFactory<>("circlesTicketPrices"));
setBalconyPrices.setCellValueFactory(new PropertyValueFactory<>("balconyTicketPrices"));
}
//final ObservableList<Shows> shows = FXCollections.observableList();
public void goHome(ActionEvent actionEvent) throws IOException {
Pane pane = FXMLLoader.load(getClass().getResource("../FXML/mainMenu.fxml"));
showPane.getChildren().setAll(pane);
}
public void addSave(ActionEvent actionEvent) throws IOException, JAXBException {
Show show = new Show(showTitle.getText(), Integer.parseInt(showMinutes.getText()), java.time.LocalDate.parse(startDate.getText()), java.time.LocalDate.parse(endDate.getText()), Double.parseDouble(setStallsTicketPrices.getText()), Double.parseDouble(setCirclesTicketPrices.getText()), Double.parseDouble(setBalconyTicketPrices.getText()));
showTableView.getItems().add(show);
clearText(actionEvent);
}
public void clearText (ActionEvent actionEvent) {
showTitle.setText("");
showMinutes.setText("");
startDate.setText("");
endDate.setText("");
setStallsTicketPrices.setText("");
setCirclesTicketPrices.setText("");
setBalconyTicketPrices.setText("");
}
}

我为JAXB使用了以下资源:

  • https://howtodoinjava.com/jaxb/write-object-to-xml/
  • https://howtodoinjava.com/jaxb/jaxb-exmaple-marshalling-and-unmarshalling-list-or-set-of-objects/
  • https://code.makery.ch/library/javafx-tutorial/part5/

EDIT:我正在从给出的示例更改showList,但现在我有一个集合,我现在需要自己实现。

package Lists;
import java.util.*;
class ShowIterator<Show> implements Iterator<Show> {
private ShowNode<Show> pos; //Current position
public ShowIterator(ShowNode<Show> fnode) { pos=fnode; }
@Override
public boolean hasNext() {
return pos!=null;
}
@Override
public Show next() {
ShowNode<Show> temp=pos;
pos=pos.next;
return temp.getShows();
}
}
//ShowNode unchanged but removed as inner class from ShowList
class ShowNode<Show> {
public ShowNode<Show> next=null;
private Show shows; //ADT reference!
public Show getShows() { return shows; }
public void setShows(Show s) { shows=s; }
}
public class ShowList<Show> implements List<Show> {
public ShowNode<Show> head=null;
public boolean add(Show show) { //Add element to head of list
ShowNode<Show> fn=new ShowNode<>();
fn.setShows(show);
fn.next=head;
head=fn;
return false;
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public boolean addAll(Collection<? extends Show> c) {
return false;
}
@Override
public boolean addAll(int index, Collection<? extends Show> c) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
}
@Override
public void clear() {
}
@Override
public Show get(int index) {
return null;
}
@Override
public Show set(int index, Show element) {
return null;
}
@Override
public void add(int index, Show element) {
}
@Override
public Show remove(int index) {
return null;
}
@Override
public int indexOf(Object o) {
return 0;
}
@Override
public int lastIndexOf(Object o) {
return 0;
}
@Override
public ListIterator<Show> listIterator() {
return null;
}
@Override
public ListIterator<Show> listIterator(int index) {
return null;
}
@Override
public List<Show> subList(int fromIndex, int toIndex) {
return null;
}
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public Iterator<Show> iterator() {
return new ShowIterator<Show>(head);
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
public <T> T[] toArray(T[] a) {
return null;
}
}

这是我为另一个列表编写的代码,但这次是为性能列表编写的,但它使用了数组而不是链表。

package Lists;
public class PerformanceList<Performance> {
@SuppressWarnings("unchecked")
private Performance[] data=(Performance[]) new Object [10]; //init capacity of 10
private int index=0;
public void add(Performance performance) {
if(index>=data.length) { //Array is full!
@SuppressWarnings("unchecked")
Performance[] temp=(Performance[]) new Object [(int)(data.length*1.5+1)];
for(int i=0;i<data.length;i++)
temp[i]=data[i];
data=temp;
}
data[index]=performance;
index++;
}
public Performance get(int i) {
return data[i];
}
public void delete(int i) {
for(int x=i;x<data.length-1;x++)
data[x]=data[x+1];
index--;
}
public int size() {
return index;
}
}

我需要在另外两节课上这样做,但一旦我完成了这项工作,我就可以去了。任何帮助都将是有益的

谢谢。

我做到了,也许这不是最好的方法,但我使用了xStream进行保存,就像去年一样,它很有效。我将SimpleStringProperty更改为只有String,对于该列表,我使用了这个列表。

//Saves the shows list to a .XML file
public void save() throws IOException {
//XStream xstream = new XStream(new StaxDriver());
XStream xstream = new XStream(new DomDriver());
ObjectOutputStream out = xstream.createObjectOutputStream(new FileWriter("shows.xml"));
out.writeObject(showsList);
out.close();
}

很抱歉,如果这仍然没有聚焦,但这是我第一次在stackOverflow上提问。谢谢。

最新更新