由於某種原因,這被刪除。 這個問題已關閉,我不會閱讀它,因爲我已經向前移動並實施了更多方法。所以不需要在這裏發表犀利的評論,我不會閱讀它們。JTable ComboBox
好吧,所以這可能是一個相當簡單的問題,但是這裏有。我正在設置一個JTable進行編輯,我想要列1有一個組合框編輯器。我查了一下怎麼做,然後按照指示將其引導到下面的代碼,但它似乎並沒有真正更新到顯示器中。我在這裏誤解了什麼?先謝謝你。
//MainWindow class
public class MainWindow extends JFrame{
GridBagConstraints gbc;
JTable gridDisplay;
private AbstractTableModel tableModel;
JLabel statusBar;
MainWindow()
{
super("LayoutEditor");
setLayout(new BorderLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
gridDisplay = new JTable();
tableModel = new MyTableModel();
gridDisplay.setAutoCreateRowSorter(true);
gridDisplay.setColumnSelectionAllowed(true);
gridDisplay.setCellSelectionEnabled(true);
gridDisplay.setModel(tableModel);
TableColumn tc = gridDisplay.getColumnModel().getColumn(0);
JComboBox<String> cb = new JComboBox<String>();
cb.addItem("JLabel");
cb.addItem("JButton");
cb.addItem("JTextField");
cb.addItem("JTextArea");
cb.addItem("JCheckBox");
cb.addItem("JList");
cb.addItem("JComboBox");
cb.addItem("JSpinnerList");
cb.addItem("JSpinnerNumber");
cb.setSelectedIndex(0);
tc.setCellEditor(new DefaultCellEditor(cb));
MyDataModel temp= new MyDataModel();
MyTableModel table = (MyTableModel)gridDisplay.getModel();
table.append(temp);
JScrollPane gridScroll = new JScrollPane(gridDisplay);
mainPanel.add(toolBox, BorderLayout.NORTH);
mainPanel.add(gridScroll, BorderLayout.CENTER);
add(mb, BorderLayout.NORTH);
add(mainPanel, BorderLayout.CENTER);
setSize(1280,720);
setVisible(true);
}
]
//myTableModel class
public class MyTableModel extends AbstractTableModel {
ArrayList<MyDataModel> data;
String[] names;
MyTableModel()
{
names = new String[]{"Type","Variable name","Text","Row","Column","Rows","Columns","Fill","Anchor"};
data = new ArrayList<MyDataModel>();
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return names.length;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
switch (columnIndex)
{
case 0:
data.get(rowIndex).setType(aValue.toString());
case 1:
data.get(rowIndex).setName(aValue.toString());
case 2:
data.get(rowIndex).setText(aValue.toString());
case 3:
data.get(rowIndex).setRow((int)aValue);
case 4:
data.get(rowIndex).setColumn((int)aValue);
case 5:
data.get(rowIndex).setRows((int)aValue);
case 6:
data.get(rowIndex).setColumns((int)aValue);
case 7:
data.get(rowIndex).setFill((int)aValue);
case 8:
data.get(rowIndex).setAnchor((int)aValue);
}
fireTableCellUpdated(rowIndex, columnIndex);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex){
case 0:
return data.get(rowIndex).getType();
case 1:
return data.get(rowIndex).getName();
case 2:
return data.get(rowIndex).getText();
case 3:
return data.get(rowIndex).getRow();
case 4:
return data.get(rowIndex).getColumn();
case 5:
return data.get(rowIndex).getRows();
case 6:
return data.get(rowIndex).getColumns();
case 7:
return data.get(rowIndex).getFill();
case 8:
return data.get(rowIndex).getAnchor();
}
return null;
}
public void append(MyDataModel item)
{
data.add(item);
}
@Override
public String getColumnName(int column) {
return names[column];
}
}
//MyDataModel Class
public class MyDataModel {
String type, name, text;
int row, column, rows, columns, fill, anchor;
MyDataModel()
{
type = new String("");
name = new String("");
text = new String("");
row = 0;
column = 0;
rows = 0;
columns = 0;
fill = 0;
anchor = 0;
}
public MyDataModel(MyDataModel test) {
type = test.getType();
name = test.getName();
text = test.getText();
row = test.getRow();
column = test.getColumn();
rows = test.getRows();
columns = test.getColumns();
fill = test.getFill();
anchor = test.getAnchor();
}
public int getAnchor() {
return anchor;
}
public void setAnchor(int anchor) {
this.anchor = anchor;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getColumn() {
return column;
}
public void setColumn(int column) {
this.column = column;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getColumns() {
return columns;
}
public void setColumns(int columns) {
this.columns = columns;
}
public int getFill() {
return fill;
}
public void setFill(int fill) {
this.fill = fill;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
請考慮擴展這個代碼,使其符合[mcve]標準 - 這是一個完全獨立的小型可運行程序。請查看鏈接瞭解我們需要的詳細信息。 –
當然。給我2秒 –
好吧,它更新了你運行程序 –