gpt4 book ai didi

java - 从 Java 中的哈希表填充 JTable

转载 作者:行者123 更新时间:2023-12-04 07:11:19 24 4
gpt4 key购买 nike

我有一个函数,它从用户那里获取一个键并生成一个哈希表(在键指定的模式上)。创建 Hashtable 后,我想填充一个 JTable,以便每一列代表一个键,每一行代表与键关联的值。我尝试了一切,但无法完成这项工作。我不是从构造函数中创建表,因为我需要从用户那里获取输入。

最佳答案

How to Use Tables: Creating a Table Model .

The JTable constructor used by SimpleTableDemo creates its table model with code like this:


new AbstractTableModel() {
public String getColumnName(int col) {
return columnNames[col].toString();
}
public int getRowCount() { return rowData.length; }
public int getColumnCount() { return columnNames.length; }
public Object getValueAt(int row, int col) {
return rowData[row][col];
}
public boolean isCellEditable(int row, int col)
{ return true; }
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value;
fireTableCellUpdated(row, col);
}
}

您基本上必须以上述方式包装您的哈希表。这是一个例子。
package eed3si9n.hashtabletable;

import java.awt.BorderLayout;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.JButton;
import java.awt.Dimension;

public class MainForm extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null; // @jve:decl-index=0:visual-constraint="23,38"
private JScrollPane m_scrollPane = null;
private JTable m_table = null;
private Hashtable<String, String> m_hash = null;
private JButton m_btnAdd = null;

/**
* This is the default constructor
*/
public MainForm() {
super();
initialize();
m_hash = new Hashtable<String, String>();
m_hash.put("Dog", "Bow");
}

private void onButtonPressed() {
m_hash.put("Cow", "Moo");
m_table.revalidate();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(409, 290);
this.setTitle("JFrame");
this.setContentPane(getJContentPane());
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.setSize(new Dimension(500, 500));
jContentPane.setPreferredSize(new Dimension(500, 500));
jContentPane.add(getM_scrollPane(), BorderLayout.NORTH);
jContentPane.add(getM_btnAdd(), BorderLayout.SOUTH);
}
return jContentPane;
}

/**
* This method initializes m_scrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getM_scrollPane() {
if (m_scrollPane == null) {
m_scrollPane = new JScrollPane();
m_scrollPane.setViewportView(getM_table());
}
return m_scrollPane;
}

/**
* This method initializes m_table
*
* @return javax.swing.JTable
*/
private JTable getM_table() {
if (m_table == null) {
m_table = new JTable();
m_table.setModel(new AbstractTableModel(){
private static final long serialVersionUID = 1L;

public int getColumnCount() {
return 2;
}

public int getRowCount() {
return m_hash.size();
}

public String getColumnName(int column) {
if (column == 0) {
return "Animal";
} else {
return "Sound";
}
}

public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return getKey(rowIndex);
} else {
return m_hash.get(getKey(rowIndex));
} // if-else

}

private String getKey(int a_index) {
String retval = "";
Enumeration<String> e = m_hash.keys();
for (int i = 0; i < a_index + 1; i++) {
retval = e.nextElement();
} // for

return retval;
}

});
}
return m_table;
}

/**
* This method initializes m_btnAdd
*
* @return javax.swing.JButton
*/
private JButton getM_btnAdd() {
if (m_btnAdd == null) {
m_btnAdd = new JButton();
m_btnAdd.setPreferredSize(new Dimension(34, 30));
m_btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
onButtonPressed();
}
});
}
return m_btnAdd;
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainForm frame = new MainForm();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
});
}
} // @jve:decl-index=0:visual-constraint="10,10"

关于java - 从 Java 中的哈希表填充 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/292279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com