gpt4 book ai didi

java - 如何在两个 JTable 之间进行更改?

转载 作者:行者123 更新时间:2023-12-01 19:05:03 24 4
gpt4 key购买 nike

所以我在一个可以工作的框架内的 JScrollPane 中有一个 JTable。我想要做的是将正在显示的表更改为另一个显示一些其他信息并具有更多/更少列/行的表。我该怎么做?

我尝试过在 ScrollPane 中放置另一个表格之类的方法,但这不起作用,如果我将 ScrollPane 重新放入框架中,它就会消失。

编辑:

我认为这就是与此问题有关的所有代码。我只是调用函数来更改 View 。

    Object[][] userData = new Object[50][6];
userTable = new JTable(userData, new String[] { "Namn", "Adress",
"Telefon", "Personnummer", "PIN", "Antal cyklar" }) {
public boolean isCellEditable(int rowIndex, int colIndex) {
return false;
}
};
userTable.setSelectionMode(0);
userTable.getTableHeader().setReorderingAllowed(false);
Object[][] bikeData = new Object[50][7];
bikeTable = new JTable(bikeData, new String[] { "Ägare", "Streckkod",
"Färg", "Märke", "Ram-nummer", "Senast hämtad", "Senast lämnad" }) {
public boolean isCellEditable(int rowIndex, int colIndex) {
return false;
}
};
bikeTable.setSelectionMode(0);
bikeTable.getTableHeader().setReorderingAllowed(false);
JScrollPane tablePane = new JScrollPane(bikeTable);
frame.add(tablePane);

frame.setVisible(true);
}

public void displayUsers(){
tablePane.setViewportView(userTable);
}

public void displayBikes(){
tablePane.setViewportView(bikeTable);
}

最佳答案

你尝试过这个吗:

scrollPane.setViewportView(yourNewTable);

这应该替换滚动 Pane 中显示的组件。当然,您始终可以更改 JTable 及其列的模型,但这不是我的偏好。

编辑:这是演示这一点的片段:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class Test {

public static void main(String... args) {
JFrame frame = new JFrame();
final Vector<String> string = new Vector<String>();
final JTable table = new JTable(getTableModel("First", 1));
final JTable table2 = new JTable(getTableModel("Second", 3));
final JButton click = new JButton("Click me");
final JScrollPane scrollPane = new JScrollPane(table);
click.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if (table2.getParent() == null) {
scrollPane.setViewportView(table2);
} else {
scrollPane.setViewportView(table);
}
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(click, BorderLayout.EAST);
panel.add(scrollPane);
frame.getContentPane().add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}

private static TableModel getTableModel(final String prefix, final int colCount) {
return new TableModel() {

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
// TODO Auto-generated method stub

}

@Override
public void removeTableModelListener(TableModelListener l) {
// TODO Auto-generated method stub

}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return false;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return prefix + "Hello cell (" + rowIndex + "," + columnIndex + ")";
}

@Override
public int getRowCount() {
return 30;
}

@Override
public String getColumnName(int columnIndex) {
return "Column " + columnIndex;
}

@Override
public int getColumnCount() {
return colCount;
}

@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}

@Override
public void addTableModelListener(TableModelListener l) {
// TODO Auto-generated method stub

}
};
}

}

关于java - 如何在两个 JTable 之间进行更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10431729/

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