所以我在一个可以工作的框架内的JScrollPane中有一个JTable。我想要做的是将显示的表更改为另一个表,该表显示一些其他信息并具有更多/更少的列/行。我该怎么做?
我尝试过在ScrollPane中添加另一个表但不起作用的事情,如果我将ScrollPane重新放入框架中,它就会消失。
编辑:
我认为这是与此问题有关的所有代码。我只是打电话给改变观点的功能。
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);
}
答案 0 :(得分:2)
如果您更改基础模型,它应该可以正常工作。
table.setModel(newModel)
如果您对某些单元格/列进行特殊处理,则可能还需要更新渲染器。
答案 1 :(得分:2)
你试过这个:
scrollPane.setViewportView(yourNewTable);
这应该替换滚动窗格中显示的组件。当然,您可以随时更改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
}
};
}
}
答案 2 :(得分:0)
如果你要维护两个jTables(有两个型号),那么最简单的方法就是简单地设置滚动窗格的视口:
jscrollpane.setViewportView(mySecondJTable);
然后切换回来:
jscrollpane.setViewportView(myFirstJTable);