无法通过动态检索的数据在 JPanel 中显示 JTable
Unable to show JTable in JPanel from dynamically retrieved data
我无法通过动态生成的数据在 JPanel 中显示 JTable。我什至尝试添加一个布局管理器,这样我就不会得到一个空布局管理器,也没有 table。这是我正在使用的代码。
public void setReferencePanel(ArrayList<Item> items, String refFile) {
String[] columns = {"first", "last"};
String[][] data = {{"Adam", "Smith"}, {"Jon", "Bon Jovi"},{"John", "Doe"}};
JTable sample = new JTable(data, columns);
refListingPanel.add(sample);
refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File - " + refFile));
}
以及同一文件中的更早版本。
private JMenuBar menuBar;
private JPanel testListingPanel;
private JScrollPane testScroller;
private JPanel refListingPanel;
private JScrollPane refScroller;
private static Dimension listingDefault = new Dimension(350, 230);
private IDiffPresenter presenter;
private boolean allItems;
private boolean unChangedItems;
private boolean changedItems;
private JTable refTable;
private JTable testTable;
public MasterFrame (IDiffPresenter presenter) {
super("Magic Diff - Under Construction");
this.presenter = presenter;
menuBar = new JMenuBar();
setupFileMenu(presenter);
setupExportMenu(presenter);
setupDisplayChangedMenu();
setupAboutMenu();
setupReferencePanel();
setupTestPanel();
getContentPane().setLayout(new GridLayout(1,2));
setJMenuBar(menuBar);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize((int)listingDefault.getWidth() + 10, (int)listingDefault.getHeight() + 60);
Dimension DimMax = Toolkit.getDefaultToolkit().getScreenSize();
this.setMaximumSize(DimMax);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
...
}
private void setupReferencePanel() {
refListingPanel = new JPanel();
refListingPanel.setLayout(new BorderLayout());
refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File"));
refScroller = new JScrollPane(refListingPanel);
getContentPane().add(refScroller);
}
我错过了什么或没有做什么?我什至尝试了一些示例数据,代码中当前的内容,我遇到了同样的问题。
这是基于 cmickr 和 Andrew Thompson 发布的内容。我修改了以下功能。
public void setReferencePanel(ArrayList<Item> items, String refFile) {
DefaultTableModel model = new DefaultTableModel(vectorize(items), makeHeaderVector());
refTable.setModel(model);
refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File - " + refFile));
}
private Vector vectorize(ArrayList<Item> items) {
Vector results = new Vector();
for (int i = 0; i < items.size(); i++) {
results.addElement(items.get(i).vectorize());
}
return results;
}
private Vector makeHeaderVector() {
String[] cols = { ... }; // hid application specific string array
Vector<String> results = new Vector<String>(Arrays.asList(cols));
return results;
}
这是我对向量的基本理解,因为我用得不多。此外,这可能不是解决问题的最快方法,但我的首要目标是让它工作,然后改进它。重要的部分是使用 TableModel,在我的例子中是 DefaultTableModel,然后 setModel()
JTable 到新模型,就像被引用一样。
我无法通过动态生成的数据在 JPanel 中显示 JTable。我什至尝试添加一个布局管理器,这样我就不会得到一个空布局管理器,也没有 table。这是我正在使用的代码。
public void setReferencePanel(ArrayList<Item> items, String refFile) {
String[] columns = {"first", "last"};
String[][] data = {{"Adam", "Smith"}, {"Jon", "Bon Jovi"},{"John", "Doe"}};
JTable sample = new JTable(data, columns);
refListingPanel.add(sample);
refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File - " + refFile));
}
以及同一文件中的更早版本。
private JMenuBar menuBar;
private JPanel testListingPanel;
private JScrollPane testScroller;
private JPanel refListingPanel;
private JScrollPane refScroller;
private static Dimension listingDefault = new Dimension(350, 230);
private IDiffPresenter presenter;
private boolean allItems;
private boolean unChangedItems;
private boolean changedItems;
private JTable refTable;
private JTable testTable;
public MasterFrame (IDiffPresenter presenter) {
super("Magic Diff - Under Construction");
this.presenter = presenter;
menuBar = new JMenuBar();
setupFileMenu(presenter);
setupExportMenu(presenter);
setupDisplayChangedMenu();
setupAboutMenu();
setupReferencePanel();
setupTestPanel();
getContentPane().setLayout(new GridLayout(1,2));
setJMenuBar(menuBar);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize((int)listingDefault.getWidth() + 10, (int)listingDefault.getHeight() + 60);
Dimension DimMax = Toolkit.getDefaultToolkit().getScreenSize();
this.setMaximumSize(DimMax);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
...
}
private void setupReferencePanel() {
refListingPanel = new JPanel();
refListingPanel.setLayout(new BorderLayout());
refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File"));
refScroller = new JScrollPane(refListingPanel);
getContentPane().add(refScroller);
}
我错过了什么或没有做什么?我什至尝试了一些示例数据,代码中当前的内容,我遇到了同样的问题。
这是基于 cmickr 和 Andrew Thompson 发布的内容。我修改了以下功能。
public void setReferencePanel(ArrayList<Item> items, String refFile) {
DefaultTableModel model = new DefaultTableModel(vectorize(items), makeHeaderVector());
refTable.setModel(model);
refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File - " + refFile));
}
private Vector vectorize(ArrayList<Item> items) {
Vector results = new Vector();
for (int i = 0; i < items.size(); i++) {
results.addElement(items.get(i).vectorize());
}
return results;
}
private Vector makeHeaderVector() {
String[] cols = { ... }; // hid application specific string array
Vector<String> results = new Vector<String>(Arrays.asList(cols));
return results;
}
这是我对向量的基本理解,因为我用得不多。此外,这可能不是解决问题的最快方法,但我的首要目标是让它工作,然后改进它。重要的部分是使用 TableModel,在我的例子中是 DefaultTableModel,然后 setModel()
JTable 到新模型,就像被引用一样。