如何在JBable中使JButton可单击
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在JBable中使JButton可单击相关的知识,希望对你有一定的参考价值。
我有一个程序,我在大多数单元格中都有一个带文本的JTable,但是每行中的最后一个单元格需要有一个JButton。我正在使用自定义按钮渲染器以及编辑器(我不希望用户编辑表中任何内容)。我不确定如何让我的按钮可点击。我的主要代码是:
starting point.Java
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class StartingPoint {
public static void main(String[] args) {
String column_names[] = {"Text", "Button"};
DefaultTableModel dtm = new DefaultTableModel(column_names, 0);
JButton button1 = new JButton();
button1.setText("Button 1");
button1.setToolTipText("Button");
JButton button2 = new JButton();
button2.setText("Button 2");
button2.setToolTipText("Buttonnn");
Vector<Object> row1 = new Vector<Object>();
row1.add("Testing");
row1.add(button1);
Vector<Object> row2 = new Vector<Object>();
row2.add("More Testing");
row2.add(button2);
dtm.addRow(row1);
dtm.addRow(row2);
JTable table = new JTable(dtm);
JScrollPane scrolly = new JScrollPane(table);
table.setFillsViewportHeight(true);
JTableButtonRenderer buttonRenderer = new JTableButtonRenderer();
JTableButtonEditor buttonEditor = new JTableButtonEditor();
table.getColumn("Button").setCellRenderer(buttonRenderer);
table.getColumn("Button").setCellEditor(buttonEditor);
JFrame frame = new JFrame("Testing");
frame.getContentPane().add(scrolly);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
JTableButtonRenderer
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class JTableButtonRenderer implements TableCellRenderer {
JTableButtonRenderer() {}
@Override
public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int rows, int columns) {
JButton button = (JButton)value;
return button;
}
}
J table button editor.Java
import java.awt.Component;
import java.util.EventObject;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
public class JTableButtonEditor implements TableCellEditor {
@Override
public void addCellEditorListener(CellEditorListener arg0) {
// TODO Auto-generated method stub
}
@Override
public void cancelCellEditing() {
// TODO Auto-generated method stub
}
@Override
public Object getCellEditorValue() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isCellEditable(EventObject arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public void removeCellEditorListener(CellEditorListener arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean shouldSelectCell(EventObject arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean stopCellEditing() {
// TODO Auto-generated method stub
return false;
}
@Override
public Component getTableCellEditorComponent(JTable arg0, Object arg1,
boolean arg2, int arg3, int arg4) {
// TODO Auto-generated method stub
return null;
}
}
我是否需要在渲染器/编辑器类中更改某些内容?我还尝试在创建按钮时在我的StartingPoint类中添加actionlistener。
“从概念上讲”,这个想法非常简单,在How to Use Tables中有一些细节
您需要从定义编辑器(和渲染器)开始。为了简单起见,我选择将两者包装在一起,因为两者都重复了许多功能。
public class TableDeleteButtonEditor extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
private File source;
private JButton button;
public TableDeleteButtonEditor() {
button = new JButton();
button.addActionListener(new LoadActionListener());
}
@Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
protected JButton prepare(JTable table, Object value, boolean isSelected, int row, int column) {
if (!(value instanceof File)) {
source = null;
button.setEnabled(false);
return null;
}
source = (File) value;
button.setEnabled(true);
button.setText(source.getName());
button.setToolTipText(source.getPath());
return button;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
return prepare(table, value, isSelected, row, column);
}
@Override
public Object getCellEditorValue() {
return source;
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
return prepare(table, value, isSelected, row, column);
}
public class LoadActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent evt) {
// Here, you need to make some decisions about what to do...
// You have a reference to the File instance
System.out.println("You clicked " + source);
stopCellEditing();
}
}
}
因为你可能只有一个活动编辑器,所以当调用getTableCellEditorComponent
时,你需要获取对底层数据的引用(即File
引用)。
通常,编辑器会将值返回给模型,在这种情况下,我不确定这是否有意义。不是说你做不到,但我会质疑目的。
对于我的例子,我只需要引用File
本身,因此,从技术上讲,我只需要一个列。相反,我设计了一个需要两列的模型,但使用了File
的参考来填充两者。这是一个简洁的例子,它展示了将“简单”对象扩展为多个部分并以不同方式由模型表示的能力......
public class FileTableModel extends AbstractTableModel {
private List<File> files;
public FileTableModel(List<File> files) {
this.files = files;
}
@Override
public int getRowCount() {
return files.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 1: return File.class;
default: return String.class;
}
}
@Override
public Object getValueAt(int row, int col) {
File file = files.get(row);
switch (col) {
case 0: return file.getName();
case 1: return file;
}
return null;
}
}
现在,这里的重要部分是isCellEditable
和getColumnClass
方法。这些有助于确定哪些单元格可以编辑,并为JTable
提供查找渲染器/编辑器的入口点,这使我们进入下一步,您需要配置JTable
以支持您的自定义编辑器/渲染器
有几种方法可以做到这一点,但为了简单起见,setDefaultRenderer
和setDefaultEditor
应该工作得很好......
List<File> files = Arrays.asList(new File(".").listFiles());
FileTableModel model = new FileTableModel(files);
JTable table = new JTable(model);
table.setDefaultEditor(File.class, new TableDeleteButtonEditor());
table.setDefaultRenderer(File.class, new TableDeleteButtonEditor());
nb:你可以使用TableDeleteButtonEditor
的单个实例,我只是懒得用我的副本和粘贴
从那里,您现在应该能够在File
中表示JTable
s列表,其中最后一列是一个按钮(带有文件名),单击时,在此示例中,将打印File
参考
Runnable Example...
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Arrays;
import java.util.EventObject;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
List<File> files = Arrays.asList(new File(".").listFiles());
FileTableModel model = new FileTableModel(files);
JTable table = new JTable(model);
table.setDefaultEditor(File.class, new TableDeleteButtonEditor());
table.setDefaultRenderer(File.class, new TableDeleteButtonEditor());
add(new JScrollPane(table));
}
}
public class FileTableModel extends AbstractTableModel {
private List<File> files;
public FileTableModel(List<File> files) {
this.files = files;
}
@Override
public int getRowCount() {
return files.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 1:
return File.class;
default:
return String.class;
}
}
@Override
public Object getValueAt(int row, int col) {
File file = files.get(row);
switch (col) {
case 0:
return file.getName();
case 1:
return file;
}
return null;
}
}
public class TableDeleteButtonEditor extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
private File source;
private JButton button;
public TableDeleteButtonEditor() {
button = new JButton();
button.addActionListener(new LoadActionListener());
}
@Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
protected JButton prepare(JTable table, Object value, boolean isSelected, int row, int column) {
if (!(value instanceof File)) {
source = null;
button.setEnabled(false);
return null;
}
source = (File) value;
button.setEnabled(true);
button.setText(source.getName());
button.setToolTipText(source.getPath());
return button;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
return prepare(table, value, isSelected, row, co以上是关于如何在JBable中使JButton可单击的主要内容,如果未能解决你的问题,请参考以下文章