如何使用 for 循环创建表数组?

编程入门 行业动态 更新时间:2024-10-27 11:17:13
本文介绍了如何使用 for 循环创建表数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一种方法,该方法将从存在于一个类中的数组中获取数据项,然后使用 for 循环将该信息放入 JTable 中.这是我想出的:

I'm trying to make a method that will get data items from the array present in one class, and then put that information into a JTable using for loops. This is what I've come up with:

public void createLoginsTable() { String[] loginTableTitles = { "Username", "Password" }; //Creating arrays for my Table String[][] loginTableLogins = new String[100][1]; for(int i=0;list.nextLogin>i;i++) { loginTableLogins[i][0] = list.listOfLogins[i].username; loginTableLogins[i][1] = list.listOfLogins[i].password; } loginsScrollPane = new JScrollPane(loginsTable = new JTable(loginTableLogins, loginTableTitles)); loginsScrollPane.setBounds(400, 300, 200, 400); testPanel.add(loginsScrollPane); }

我为表格的列标题制作了普通数组,然后我尝试使用这些 for 循环来放入其他类中存在的常规数组中的用户名和密码.我对 java 很陌生,所以我认为 loginTableLogins[i][0] 意味着它将把详细信息放入第一列和i"行.但这是在一个按钮的动作监听器中,每次我按下按钮时,我都会弹出书中的每个错误.一些建议真的很有帮助.

I made the normal array for the column headings of the table, and then I tried to use these for loops to put in the usernames and passwords present in the regular array present in the other class. I'm quite new to java so I assumed that loginTableLogins[i][0] means it will put the details in to the first column and the 'i' row. But this is in an action lisener of a button and every time I press the button, I get every error in the book popping up. Some advice would really help.

推荐答案

出现了一些问题.

  • 您似乎已经拥有一个代表您要显示的数据的对象.与其在表模型之间来回转换它,不如创建一个能够管理/保存原始对象的表模型.

  • You seem to already have an object that represents the data you want to display. Rather then having to translate it backwards and forwards between the table model, you should create a table model capable of managing/holding the original objects.

    String[][] loginTableLogins = new String[100][1]; 被声明为错误的原因有两个.首先,如果您有超过 100 行或少于 100 行会发生什么?其次,您只声明了一个列,这显然不是您想要的.您已经拥有对象列表中包含的元素数量,因此您应该能够创建一个已经包含正确行数的数组

    String[][] loginTableLogins = new String[100][1]; is declared wrong for two reasons. Firstly, what happens if you have more then or less then 100 rows? Secondly, you've only declared a single column, which obviously isn't what you want. You already have the number of elements that are contained in the list of objects, so you should be able to create an array which already contains the correct number of rows

    就我个人而言,我会避免使用 DefaultTableModel,当您覆盖并实现您需要的所有功能时,您已经完成了与开始使用AbstractTableModel.

    Personally, I'd avoid DefaultTableModel, by the time you override and implement all the functionality you need to, you've would have done the same amount of work as if you have started with a AbstractTableModel.

    例如...

    import java.awt.BorderLayout; import java.awt.Component; import java.awt.EventQueue; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; public class SimpleTable { public static void main(String[] args) { new SimpleTable(); } public SimpleTable() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } LoginTableModel model = new LoginTableModel(); model.add(new Login("Elma Thud", "Shh, I'm hunting rabbits".toCharArray())); JTable table = new JTable(model); table.getColumnModel().getColumn(1).setCellRenderer(new PasswordCellRenderer()); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new JScrollPane(table)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class PasswordCellRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value instanceof char[]) { char[] pass = (char[]) value; StringBuilder sb = new StringBuilder(pass.length); while (sb.length() < pass.length) { sb.append("*"); } value = sb.toString(); } super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); return this; } } public class Login { private String userName; private char[] password; public Login(String userName, char[] password) { this.userName = userName; this.password = password; } public String getUserName() { return userName; } public char[] getPassword() { return password; } } public class LoginTableModel extends AbstractTableModel { private List<Login> logins; public LoginTableModel() { logins = new ArrayList<>(25); } public void add(Login... login) { int startAt = getRowCount(); logins.addAll(Arrays.asList(login)); int endAt = getRowCount(); fireTableRowsInserted(startAt, endAt - 1); } @Override public int getRowCount() { return logins.size(); } @Override public String getColumnName(int column) { return column == 0 ? "Username" : "Password"; } @Override public int getColumnCount() { return 2; } @Override public Object getValueAt(int rowIndex, int columnIndex) { Login login = logins.get(rowIndex); Object value = null; switch (columnIndex) { case 0: value = login.getUserName(); break; case 1: value = login.getPassword(); break; } return value; } } }

    查看如何使用表格了解更多详情

  • 更多推荐

    如何使用 for 循环创建表数组?

    本文发布于:2023-10-19 15:06:32,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1507888.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数组   如何使用

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!