我想创建一个表,每次添加新行时,还会添加某个列中的新复选框。我没有参加我的研究,但我仍然无法找到正确的答案,有时我发现很难理解一些说明 所以这是我的问题:
我在我的表的列(“e”)中添加了一个复选框,但它没有显示。复选框仅显示我是否单击它。
package app.ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.DefaultCellEditor;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import app.dao.item.impl.ReadItemFromDB;
import app.model.Item;
public class Inventory {
private JFrame inventoryframe;
private JTable table;
private JTextField textField;
private JCheckBox checkbox;
/**
* Launch the application.
*/
public void InventoryWindow() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Inventory window = new Inventory();
window.inventoryframe.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Inventory() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
inventoryframe = new JFrame();
inventoryframe.setExtendedState(JFrame.MAXIMIZED_BOTH);
inventoryframe.getContentPane().setBackground(new Color(153, 204, 102));
inventoryframe.getContentPane().setForeground(new Color(255, 255, 255));
inventoryframe.getContentPane().setPreferredSize(new Dimension(1365, 747));
inventoryframe.pack();
inventoryframe.getContentPane().setLayout(null);
JLabel lblInventory = new JLabel("Inventory Management");
lblInventory.setBounds(56, 32, 234, 27);
lblInventory.setFont(new Font("Tahoma", Font.PLAIN, 22));
inventoryframe.getContentPane().add(lblInventory);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(56, 130, 479, 249);
inventoryframe.getContentPane().add(scrollPane);
table = new JTable();
table.setShowVerticalLines(false);
table.setShowHorizontalLines(false);
table.setShowGrid(false);
table.setFillsViewportHeight(true);
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"t", "e"
}
));
scrollPane.setViewportView(table);
checkbox = new JCheckBox("borrow");
checkbox.setHorizontalAlignment(SwingConstants.CENTER);
checkbox.setBounds(360, 63, 97, 23);
TableColumn sportColumn = table.getColumnModel().getColumn(1);
sportColumn.setCellEditor(new DefaultCellEditor(checkbox));
doIt();
}
public void doIt(){
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
dtm.getDataVector().removeAllElements();
dtm.getColumnClass(0);
ReadItemFromDB myReader = new ReadItemFromDB();
List<Item> newItemList = myReader.showItems();
@Override
public Class getColumnClass() {
return getValueAt(0, 1).getClass();
}
for (Item myNewItems : newItemList) {
Object[] rowData = new Object[1];
rowData[0] =myNewItems.getItemID();
dtm.addRow(rowData);
}
table.updateUI();
}
/*public Boolean getColumnClass(){
dtm.getValueAt(0, 1).getClass();
return null;
}*/
}
答案 0 :(得分:2)
“对不起,请详细说明?你能举例说明吗?”
无需自定义渲染器或编辑器。只需@Override
表格模型中的getColumnClass()
,即可使用DefaultTableModel
并仅使用Boolean
类型的列。
下面有一个正在运行的exmpple,这是重要的部分
DefaultTableModel model = new DefaultTableModel(data, cols) {
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable(model);
这是完整的代码
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class TestTableCheck {
private static JTable createTable() {
Object[][] data = {{true, true, true}, {false, false, false}};
String[] cols = {"Bibitty", "Boppity", "Boo"};
DefaultTableModel model = new DefaultTableModel(data, cols) {
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable(model);
return table;
}
public static void main(String[] args) {
JOptionPane.showMessageDialog(null,
new JScrollPane(createTable()),
"Table",
JOptionPane.PLAIN_MESSAGE);
}
}
这是你的代码。查看initialize()
方法,我在其中注释了一些代码,并在下面添加了我的代码。我也摆脱了doit()
方法。另外,您应pack()
作为方法的结尾,setVisible()
。我还添加了一个main
方法,因此它可以运行
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.DefaultCellEditor;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class Inventory {
private JFrame inventoryframe;
private JTable table;
private JTextField textField;
private JCheckBox checkbox;
/**
* Launch the application.
*/
public void InventoryWindow() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Inventory window = new Inventory();
window.inventoryframe.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Inventory() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
inventoryframe = new JFrame();
inventoryframe.setExtendedState(JFrame.MAXIMIZED_BOTH);
inventoryframe.getContentPane().setBackground(new Color(153, 204, 102));
inventoryframe.getContentPane().setForeground(new Color(255, 255, 255));
inventoryframe.getContentPane().setPreferredSize(new Dimension(1365, 747));
inventoryframe.getContentPane().setLayout(null);
JLabel lblInventory = new JLabel("Inventory Management");
lblInventory.setBounds(56, 32, 234, 27);
lblInventory.setFont(new Font("Tahoma", Font.PLAIN, 22));
inventoryframe.getContentPane().add(lblInventory);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(56, 130, 479, 249);
inventoryframe.getContentPane().add(scrollPane);
table = new JTable();
table.setShowVerticalLines(false);
table.setShowHorizontalLines(false);
table.setShowGrid(false);
table.setFillsViewportHeight(true);
/* table.setModel(new DefaultTableModel(
new Object[][]{},
new String[]{
"t", "e"
}
));*/
Object[][] data = {{true, true, true}, {false, false, false}};
String[] cols = {"Bibitty", "Boppity", "Boo"};
DefaultTableModel model = new DefaultTableModel(data, cols) {
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table.setModel(model);
scrollPane.setViewportView(table);
checkbox = new JCheckBox("borrow");
checkbox.setHorizontalAlignment(SwingConstants.CENTER);
checkbox.setBounds(360, 63, 97, 23);
TableColumn sportColumn = table.getColumnModel().getColumn(1);
sportColumn.setCellEditor(new DefaultCellEditor(checkbox));
inventoryframe.pack();
inventoryframe.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Inventory();
}
});
}
}
真的,这应该是另一个问题,但我今天感到很慷慨。
使用此测试类。运行。单击该按钮时,将打开Inventory
类。您可能没有在按钮的Inventory
中实例化actionPerformed
。确保此类文件与Inventory
类文件位于同一个包中。然后运行下面的课程。
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class InventoryTest extends JFrame {
public InventoryTest() {
setLayout(new GridBagLayout());
JButton show = new JButton("Show Inventory");
add(show);
show.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
new Inventory();
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new InventoryTest();
}
});
}
}