java 如何将ArrayList注入JTable,在IntelliJ IDEA中

w7t8yxp5  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(180)

我开始使用IntelliJ IDEA(IDE),我在做一个 Swing 项目。目前我做了一个ArrayList来模拟数据库,看看是否一切正常。我试着像在NetBeans中那样将ArrayList连接到JTable,但是数据没有得到反映。我不知道是否有人能给予我一种方法来注入ArrayList。求你了
由于JTable的特性,我使用IntelliJ编辑器本身创建了它,也就是说,我没有使用任何代码。
我试着像在NetBeans中那样去做,但是没有用。我试着用教程来做,但是也没有用。
这就是ArrayList

  1. public class DatabaseLibros {
  2. public static ArrayList<Book> books = new ArrayList<>();
  3. void cargarLibrosDB() {
  4. Book book;
  5. // Libro 1
  6. book = new Book("El señor de los anillos", "J.R.R. Tolkien", "Fantasía", "978-84-450-7570-9");
  7. books.add(book);
  8. // Libro 2
  9. book = new Book("Cien años de soledad", "Gabriel García Márquez", "Realismo mágico", "978-84-376-0374-7");
  10. books.add(book);
  11. // Libro 3
  12. book = new Book("1984", "George Orwell", "Distopía", "978-84-670-5189-7");
  13. books.add(book);
  14. }
  15. // Método para obtener todos los libros de la base de datos
  16. public static ArrayList<Book> obtenerLibros() {
  17. return books;
  18. }
  19. // Método para agregar un nuevo libro a la base de datos
  20. public static void agregarLibro(Book nuevoBook) {
  21. books.add(nuevoBook);
  22. }
  23. }

字符串
下面是JFrame窗口的逻辑:

  1. public class PantallaPrincipal extends JFrame {
  2. private JPanel panel;
  3. private JTextField textField1;
  4. private JButton button1;
  5. private JTable jTable;
  6. private JButton salirButton;
  7. private JLabel txtLeft;
  8. public PantallaPrincipal() {
  9. DefaultTableModel tableModel = new DefaultTableModel(30, 0);
  10. jTable.setModel(tableModel);
  11. tableModel.addColumn("Título");
  12. tableModel.addColumn("Autor");
  13. tableModel.addColumn("Género");
  14. tableModel.addColumn("ISBN");
  15. // Configurar el contenido del JFrame
  16. add(panel);
  17. setSize(1280, 720);
  18. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. setLocationRelativeTo(null);
  20. salirButton.addActionListener(new ActionListener() {
  21. @Override
  22. public void actionPerformed(ActionEvent e) {
  23. dispose();
  24. Container principal = new Container();
  25. principal.setVisible(true);
  26. }
  27. });
  28. }
  29. }

fzwojiic

fzwojiic1#

我建议您从阅读How to Use Tables开始,您还可以从理解Dependency Injectionobserver pattern等概念中受益
就我个人而言,我会从创建一个自定义的TableModel开始,它将接受数据源的一个示例(DatabaseLibros的一个示例)。为此,我会创建一个从AbstractTableModel扩展的类,这样你就可以轻松地配置和重用模型。
例如...

  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import java.util.ArrayList;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.JTable;
  7. import javax.swing.table.AbstractTableModel;
  8. public class Main {
  9. public static void main(String[] args) {
  10. new Main();
  11. }
  12. public Main() {
  13. EventQueue.invokeLater(new Runnable() {
  14. @Override
  15. public void run() {
  16. JFrame frame = new JFrame();
  17. frame.add(new TestPane());
  18. frame.pack();
  19. frame.setLocationRelativeTo(null);
  20. frame.setVisible(true);
  21. }
  22. });
  23. }
  24. public class TestPane extends JPanel {
  25. public TestPane() {
  26. setLayout(new BorderLayout());
  27. add(new JTable(new BookTableModel(new DatabaseLibros())));
  28. }
  29. }
  30. public class BookTableModel extends AbstractTableModel {
  31. private String[] columnNames = new String[]{
  32. "Título",
  33. "Autor",
  34. "Género",
  35. "ISBN"
  36. };
  37. private DatabaseLibros database;
  38. public BookTableModel(DatabaseLibros database) {
  39. this.database = database;
  40. }
  41. @Override
  42. public int getRowCount() {
  43. return database.obtenerLibros().size();
  44. }
  45. @Override
  46. public int getColumnCount() {
  47. return columnNames.length;
  48. }
  49. @Override
  50. public String getColumnName(int column) {
  51. return columnNames[column];
  52. }
  53. @Override
  54. public Object getValueAt(int rowIndex, int columnIndex) {
  55. Book book = database.obtenerLibros().get(rowIndex);
  56. switch (columnIndex) {
  57. case 0: return book.getTitle();
  58. case 1: return book.getAuthor();
  59. case 2: return book.getPublisher();
  60. case 3: return book.getIsbn();
  61. }
  62. return null;
  63. }
  64. }
  65. public class Book {
  66. private String title;
  67. private String author;
  68. private String publisher;
  69. private String isbn;
  70. public Book(String title, String author, String publisher, String isbn) {
  71. this.title = title;
  72. this.author = author;
  73. this.publisher = publisher;
  74. this.isbn = isbn;
  75. }
  76. public String getTitle() {
  77. return title;
  78. }
  79. public String getAuthor() {
  80. return author;
  81. }
  82. public String getPublisher() {
  83. return publisher;
  84. }
  85. public String getIsbn() {
  86. return isbn;
  87. }
  88. }
  89. public class DatabaseLibros {
  90. private ArrayList<Book> books = new ArrayList<>();
  91. public DatabaseLibros() {
  92. // Libro 1
  93. books.add(new Book("El señor de los anillos", "J.R.R. Tolkien", "Fantasía", "978-84-450-7570-9"));
  94. // Libro 2
  95. books.add(new Book("Cien años de soledad", "Gabriel García Márquez", "Realismo mágico", "978-84-376-0374-7"));
  96. // Libro 3
  97. books.add(new Book("1984", "George Orwell", "Distopía", "978-84-670-5189-7"));
  98. }
  99. // Método para obtener todos los libros de la base de datos
  100. public ArrayList<Book> obtenerLibros() {
  101. return books;
  102. }
  103. // Método para agregar un nuevo libro a la base de datos
  104. public void agregarLibro(Book nuevoBook) {
  105. books.add(nuevoBook);
  106. }
  107. }
  108. }

字符串
作为一个旁注,我会小心使用static的方式,它很容易回来困扰你。

展开查看全部

相关问题