我發現一個雙JList
示例代碼,但我需要兩列Jlist
。 如何在Jlist
中添加兩列?如何在Jlist中添加兩列?
我試圖使用ListCellRenderer
,但我沒有添加元素到模型中。
Here's該代碼在具有圖像的網頁中顯示。
我發現一個雙JList
示例代碼,但我需要兩列Jlist
。 如何在Jlist
中添加兩列?如何在Jlist中添加兩列?
我試圖使用ListCellRenderer
,但我沒有添加元素到模型中。
Here's該代碼在具有圖像的網頁中顯示。
你的意思是這樣的嗎?
我通過具有通過How to Use Tables
主框架一讀取開始...
public class DualTableFrame extends JFrame {
private JTable leftTable;
private JTable rightTable;
private JButton addButton;
private JButton removeButton;
public DualTableFrame() {
setTitle("Dual wielding tables");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
leftTable = new JTable(new SimpleColorTableModel());
rightTable = new JTable(new SimpleColorTableModel());
setupTable(leftTable);
setupTable(rightTable);
populate((SimpleColorTableModel) leftTable.getModel());
addButton = new JButton("Add >>");
removeButton = new JButton("<< Remove");
JPanel pnlActions = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
pnlActions.add(addButton, gbc);
gbc.gridy++;
pnlActions.add(removeButton, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.33;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JLabel("Available Choices"), gbc);
gbc.gridx++;
add(new JPanel(), gbc);
gbc.gridx++;
add(new JLabel("Your Choices"), gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.weighty++;
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(leftTable), gbc);
gbc.gridx++;
add(pnlActions, gbc);
gbc.gridx++;
add(new JScrollPane(rightTable), gbc);
setSize(400, 600);
addButton.setEnabled(false);
removeButton.setEnabled(false);
leftTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int count = leftTable.getSelectedRowCount();
addButton.setEnabled(count > 0);
}
});
rightTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int count = rightTable.getSelectedRowCount();
removeButton.setEnabled(count > 0);
}
});
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveSelectedRow(leftTable, rightTable);
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveSelectedRow(rightTable, leftTable);
}
});
}
protected void moveSelectedRow(JTable from, JTable to) {
SimpleColorTableModel fromModel = (SimpleColorTableModel) from.getModel();
SimpleColorTableModel toModel = (SimpleColorTableModel) to.getModel();
for (int index : from.getSelectedRows()) {
Vector rowValue = (Vector) fromModel.getDataVector().get(index);
toModel.addRow(rowValue);
}
int selectedRow = -1;
while ((selectedRow = from.getSelectedRow()) != -1) {
fromModel.removeRow(selectedRow);
}
from.clearSelection();
}
protected void populate(SimpleColorTableModel model) {
model.addRow(new Object[]{"Black", Color.BLACK});
model.addRow(new Object[]{"Blue", Color.BLUE});
model.addRow(new Object[]{"Cyan", Color.CYAN});
model.addRow(new Object[]{"Dark Gray", Color.DARK_GRAY});
model.addRow(new Object[]{"Gray", Color.GRAY});
model.addRow(new Object[]{"Green", Color.GREEN});
model.addRow(new Object[]{"Light Gary", Color.LIGHT_GRAY});
model.addRow(new Object[]{"Mangenta", Color.MAGENTA});
model.addRow(new Object[]{"Orange", Color.ORANGE});
model.addRow(new Object[]{"Pink", Color.PINK});
model.addRow(new Object[]{"Red", Color.RED});
model.addRow(new Object[]{"White", Color.WHITE});
model.addRow(new Object[]{"Yellow", Color.YELLOW});
}
protected void setupTable(JTable table) {
table.setFillsViewportHeight(true);
table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
}
}
表格渲染
public class ColorTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setText(null);
if (value instanceof Color) {
setOpaque(true);
setBackground((Color)value);
}
return this;
}
}
表模型
public class SimpleColorTableModel extends DefaultTableModel {
public SimpleColorTableModel() {
addColumn("Name");
addColumn("Color");
}
@Override
public Class<?> getColumnClass(int columnIndex) {
Class clazz = String.class;
switch (columnIndex) {
case 1:
clazz = Color.class;
break;
}
return clazz;
}
}
是的,這正是我想要的。你能否給我這個示例代碼以供參考? – user1589684 2012-08-11 07:21:05
當然,但我可能要殺了你;) – MadProgrammer 2012-08-11 07:34:09
謝謝!它幫助我很多!減少我的學習時間。 :D – user1589684 2012-08-11 07:38:18
或者我應該放棄這個示例代碼並使用Jtable重新開始? – user1589684 2012-08-11 05:22:01
您確定您正在尋找JList?我想這是JTable你需要什麼。行! – Kowser 2012-08-11 05:25:25
行!讓我放棄這個示例代碼並從JTable開始。謝謝! – user1589684 2012-08-11 05:31:27