[英]How do I update a JPanel with information from a different class?
我創建了以下示例源,並且想知道如何使用JLabel更新JPanel,該JLabel的信息位於JTable中單擊的行中。
我還想指出,這只是一個簡單的示例,由於這里有一些SO成員,我對示例代碼進行了相當多的改進。 因此,我將發布這個簡單的示例作為學習的一種方式
SwingTesting(主)
public class SwingTesting {
private final JFrame frame;
private final TablePane tablePane;
private final JSplitPane splitPane;
private final JPanel infoPanel;
private final JLabel infoLabel;
public SwingTesting() {
tablePane = new TablePane();
infoPanel = new JPanel();
frame = new JFrame();
infoLabel = new JLabel(); //this is the panel i want to add the label to
infoPanel.add(infoLabel);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tablePane, infoPanel);
frame.add(splitPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingTesting();
}
});
}
}
桌板
public class TablePane extends JPanel {
private final JTable table;
private final TableModel tableModel;
private final ListSelectionModel listSelectionModel;
public TablePane() {
table = new JTable();
tableModel = createTableModel();
table.setModel(tableModel);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.add(table.getTableHeader(), BorderLayout.PAGE_START);
table.setFillsViewportHeight(true);
listSelectionModel = table.getSelectionModel();
table.setSelectionModel(listSelectionModel);
listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
table.setSelectionModel(listSelectionModel);
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 3;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.weightx = 1;
gbc.weighty = 1;
this.add(new JScrollPane(table), gbc);
}
private TableModel createTableModel() {
DefaultTableModel model = new DefaultTableModel(
new Object[] {"Car", "Color", "Year"}, 0
){
@Override public boolean isCellEditable(int row, int column) {
return false;
}
};
addTableData(model);
return model;
}
private void addTableData(DefaultTableModel model) {
model.addRow(new Object[] {"Nissan", "Black", "2007"});
model.addRow(new Object[] {"Toyota", "Blue", "2012"});
model.addRow(new Object[] {"Chevrolet", "Red", "2009"});
model.addRow(new Object[] {"Scion", "Silver", "2005"});
model.addRow(new Object[] {"Cadilac", "Grey", "2001"});
}
class SharedListSelectionHandler implements ListSelectionListener {
//When selection changes i want to add a label to the panel
//currently it just prints out the info from the selected row
@Override
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
String contents = "";
if(lsm.isSelectionEmpty()) {
System.out.println("<none>");
} else {
int minIndex = lsm.getMinSelectionIndex();
int maxIndex = lsm.getMaxSelectionIndex();
for(int i = minIndex; i <= maxIndex; i++) {
if(lsm.isSelectedIndex(i)) {
for(int j = 0; j < table.getColumnCount(); j++) {
contents += table.getValueAt(i, j) + " ";
}
}
}
System.out.println(contents);
}
}
}
}
所以我想知道如何從ListSelectionListener訪問該JPanel。 我是否應該將面板傳遞給TablePane類? 還是有更合適的方法來做到這一點?
另外,由於某種原因,我的ListSelectionListener兩次打印出行信息,是否使循環混亂了?
編輯
public class TablePane extends JPanel {
private final JTable table;
private final TableModel tableModel;
private final ListSelectionModel listSelectionModel;
private final displayPanel;
public TablePane() {
//removed code for reading purposes
}
//IDE says issue with thinking displayPanel may have already been initialized
public TablePane(JPanel panel) {
//this();
//displayPanel = panel;
}
//ListSelectionListener uses panel.add(jlabel)
}
它像起飛final
一樣簡單嗎?
您可以將JLabel
對象傳遞給TablePane
對象(在TablePane
的構造函數中,或者通過提供自定義setLabel()
方法)。 然后,您可以使用StringBuilder
創建需要在標簽上粘貼的文本,並使用StringBuilder
對象的常量(通過其toString()
方法)在標簽上調用setText()
)。
我相信您將兩次打印Everytihn,因為valueChanged
方法被調用了兩次:一次是在取消選擇當前行的通知中,一次是在選擇新行的通知中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.