当然可以!在Java Swing中,您可以通过自定义JTable的单元格渲染器(CellRenderer)和单元格编辑器(CellEditor)来实现没有边框的JTable列。以下是一个简单的示例:
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
public class NoBorderCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setBorder(BorderFactory.createEmptyBorder());
setOpaque(false);
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
import javax.swing.*;
import javax.swing.table.DefaultCellEditor;
import java.awt.*;
public class NoBorderCellEditor extends DefaultCellEditor {
public NoBorderCellEditor(JTextField textField) {
super(textField);
textField.setBorder(BorderFactory.createEmptyBorder());
}
}
import javax.swing.*;
import javax.swing.table.TableColumn;
public class NoBorderJTableExample {
public static void main(String[] args) {
JFrame frame = new JFrame("No Border JTable Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] columnNames = {"Column 1", "Column 2"};
Object[][] data = {
{"Row 1, Column 1", "Row 1, Column 2"},
{"Row 2, Column 1", "Row 2, Column 2"}
};
JTable table = new JTable(data, columnNames);
table.setRowHeight(20);
NoBorderCellRenderer noBorderCellRenderer = new NoBorderCellRenderer();
NoBorderCellEditor noBorderCellEditor = new NoBorderCellEditor(new JTextField());
TableColumn column = null;
for (int i = 0; i< table.getColumnCount(); i++) {
column = table.getColumnModel().getColumn(i);
column.setCellRenderer(noBorderCellRenderer);
column.setCellEditor(noBorderCellEditor);
}
frame.add(new JScrollPane(table));
frame.setSize(400, 200);
frame.setVisible(true);
}
}
这个示例将创建一个没有边框的JTable,您可以根据需要进行进一步的自定义。希望这个答案对您有帮助!
领取专属 10元无门槛券
手把手带您无忧上云