gpt4 book ai didi

java - jTable 开头的复选框

转载 作者:行者123 更新时间:2023-12-02 05:06:21 25 4
gpt4 key购买 nike

我是 Swing 新手,在这里寻求帮助。我需要在 jTable 中显示 .xls 文件中的数据。下面是我从 here 遵循的代码:-

jbClick.addActionListener(new ActionListener() { 

public void actionPerformed(ActionEvent arg0) {
jChooser.showOpenDialog(null);
File file = jChooser.getSelectedFile();
if(!file.getName().endsWith("xls")){
JOptionPane.showMessageDialog(null, "Please select only Excel file.", "Error",JOptionPane.ERROR_MESSAGE);
}
else {
fillData(file);
model = new DefaultTableModel(data, headers);
tableWidth = model.getColumnCount() * 150;
tableHeight = model.getRowCount() * 25;
table.setPreferredSize(new Dimension( tableWidth, tableHeight));
table.setModel(model);
jbClick.setVisible(false);
jbText.setVisible(true);
}
}
});

JPanel chooserPanel = new JPanel();
JPanel filterPanlel = new JPanel();
//filterPanlel.add(jbText,"OLa");

final Color alternate = new Color(186,246,244);
table = new JTable();
table.setAutoCreateRowSorter(true);
model = new DefaultTableModel(data, headers);
table.setModel(model);
table.setBackground(alternate);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setEnabled(false);
table.setRowHeight(25);
table.setRowMargin(4);
tableWidth = model.getColumnCount() * 200;
tableHeight = model.getRowCount() * 25;
table.setPreferredSize(new Dimension( tableWidth, tableHeight));
scroll = new JScrollPane(table);
scroll.setBackground(alternate);
scroll.setPreferredSize(new Dimension(500, 500));
scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(buttonPanel, BorderLayout.NORTH);
getContentPane().add(scroll, BorderLayout.CENTER);
//setSize(600, 600);
setResizable(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
//setUndecorated(true);
setVisible(true);
}


/** * Fill JTable with Excel file data. * * @param file * file :contains xls file to display in jTable */
@SuppressWarnings({ "unchecked", "rawtypes" })
void fillData(File file) {
Workbook workbook = null;
try {
try {
workbook = Workbook.getWorkbook(file);
}
catch (IOException ex) {
Logger.getLogger( excelTojTable.class. getName()).log(Level.SEVERE, null, ex);
}
Sheet sheet = workbook.getSheet(0);
headers.clear();
for (int i = 0; i < sheet.getColumns(); i++)
{
Cell cell1 = sheet.getCell(i, 0);
headers.add(cell1.getContents());
}
data.clear();
for (int j = 1; j < sheet.getRows(); j++)
{
Vector d = new Vector();
for (int i = 0; i < sheet.getColumns(); i++)
{
Cell cell = sheet.getCell(i, j);
d.add(cell.getContents());
}
d.add("\n");
data.add(d);
}
}
catch (BiffException e) {
e.printStackTrace();
}
}

问题:- 现在我需要在表格中每一行的前面添加一个可编辑的复选框。

请帮忙。

最佳答案

I need to add a editable checkbox in-front of each row in the table.

在创建“header”和“d” vector 的循环之前,您需要添加 boolean 信息。

因此对于标题,您可能会这样做:

headers.clear(); 
header.add("Select");

对于每一行:

Vector d = new Vector(); 
d.add( Boolean.FALSE );.

i got a new column in my table but its not a checkbox

然后,您需要使用 Boolean 类的默认渲染器更新 TableColumnModelTableColumn 来设置表格第一列的默认渲染器:

TableColumn tc = table.getColumnModel().getColumn(0);
tc.setCellRenderer( table.getDefaultRenderer( Boolean.class ) );

i am new to swing

从 [如何使用表格] 的 Swing 教程开始( http://docs.oracle.com/javase/tutorial/uiswing/components/table.html ) 了解渲染器的基础知识。

编辑:

演示解决方案的简单 SSCCE:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.table.*;

public class SSCCE extends JPanel
{
public SSCCE()
{
Vector<String> header = new Vector<String>();
header.add("Select");
header.add("Column1");
header.add("Column2");
header.add("Column3");

Vector<Vector<Object>> data = new Vector<Vector<Object>>();

for (int row = 0; row < 5; row++)
{
Vector<Object> d = new Vector<Object>();
d.add( Boolean.FALSE );

for (int column = 0; column < 3; column++)
{
d.add(row + " : " + column);
}

data.add(d);
}

DefaultTableModel model = new DefaultTableModel(data, header);
JTable table = new JTable( model );
table.setPreferredScrollableViewportSize(table.getPreferredSize());
TableColumn tc = table.getColumnModel().getColumn(0);
tc.setCellRenderer( table.getDefaultRenderer( Boolean.class ) );
tc.setCellEditor( table.getDefaultEditor( Boolean.class ) );

JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}

private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new SSCCE() );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

关于java - jTable 开头的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27757089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com