JTable mit Buttons in Tabellenzellen
Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springenDieses Beispiel zeigt, wie man Buttons innerhalb von Zellen einer JTable benutzen kann.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
/**
* @author André Uhres
*/
class ButtonHandler {
private JTable table;
private Action action;
private Border originalBorder;
private Border focusBorder;
private final Editor buttonEditor;
private final Renderer buttonRenderer;
private final Object[] buttonPositions;
/**
* Creates a ButtonHandler for the JTable passed as parameter.
* @param table
* @param action the action to be performed by the buttons
* @param buttonPositions an array of int[] row-column pairs representing the button positions
*/
ButtonHandler(final JTable table, final Action action, final Object[] buttonPositions) {
this.table = table;
this.action = action;
buttonRenderer = new Renderer();
buttonEditor = new Editor();
this.buttonPositions = buttonPositions;
}
/**
* @param row
* @param column
* @return the TableCellEditor for the cell at row-column
*/
public TableCellEditor getEditor(final int row, final int column) {
TableColumn tableColumn = table.getColumnModel().getColumn(column);
TableCellEditor editor = tableColumn.getCellEditor();
if (editor == null) {
for (int i = 0; i < buttonPositions.length; i++) {
int[] pos = (int[]) buttonPositions[i];
if (pos[0] == row && pos[1] == column) {
return buttonEditor;
}
}
Class c = table.getColumnClass(column);
if (c.equals(Object.class)) {
Object o = table.getValueAt(row, column);
if (o != null) {
c = table.getValueAt(row, column).getClass();
}
}
editor = table.getDefaultEditor(c);
}
return editor;
}
/**
* @param row
* @param column
* @return the TableCellRenderer for the cell at row-column
*/
public TableCellRenderer getRenderer(final int row, final int column) {
TableColumn tableColumn = table.getColumnModel().getColumn(column);
TableCellRenderer renderer = tableColumn.getCellRenderer();
if (renderer == null) {
for (int i = 0; i < buttonPositions.length; i++) {
int[] pos = (int[]) buttonPositions[i];
if (pos[0] == row && pos[1] == column) {
return buttonRenderer;
}
}
Class c = table.getColumnClass(column);
if (c.equals(Object.class)) {
Object o = table.getValueAt(row, column);
if (o != null) {
c = table.getValueAt(row, column).getClass();
}
}
renderer = table.getDefaultRenderer(c);
}
return renderer;
}
private class Renderer implements TableCellRenderer {
private JButton renderButton = new JButton();
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
renderButton.setForeground(table.getSelectionForeground());
renderButton.setBackground(table.getSelectionBackground());
} else {
renderButton.setForeground(table.getForeground());
renderButton.setBackground(UIManager.getColor("Button.background"));
}
if (hasFocus) {
renderButton.setBorder(focusBorder);
} else {
renderButton.setBorder(originalBorder);
}
if (value == null) {
renderButton.setText("");
renderButton.setIcon(null);
} else if (value instanceof Icon) {
renderButton.setText("");
renderButton.setIcon((Icon) value);
} else {
renderButton.setText(value.toString());
renderButton.setIcon(null);
}
return renderButton;
}
}
private class Editor extends AbstractCellEditor implements TableCellEditor {
private JButton editButton;
private Object editorValue;
private Editor() {
editButton = new JButton();
editButton.setFocusPainted(false);
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
int row = table.convertRowIndexToModel(table.getEditingRow());
int column = table.convertColumnIndexToModel(table.getEditingColumn());
buttonEditor.stopCellEditing();
ActionEvent event = new ActionEvent(table,
ActionEvent.ACTION_PERFORMED,
row + "," + column);
action.actionPerformed(event);
}
});
originalBorder = editButton.getBorder();
focusBorder = new LineBorder(Color.BLUE);
editButton.setBorder(focusBorder);
}
@Override
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
if (value == null) {
editButton.setText("");
editButton.setIcon(null);
} else if (value instanceof Icon) {
editButton.setText("");
editButton.setIcon((Icon) value);
} else {
editButton.setText(value.toString());
editButton.setIcon(null);
}
this.editorValue = value;
return editButton;
}
@Override
public Object getCellEditorValue() {
return editorValue;
}
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
/**
* A JTable type, drawing JButtons.
* @author André Uhres
*/
class MyTable extends JTable {
private ButtonHandler btHandler;
MyTable() {
super(10, 4);
String[] buttonNames = {"Button1", "Button2", "Button3"};
Object[] buttonPositions = new Object[]{new int[]{0, 0}, new int[]{0, 1}, new int[]{0, 2}};
for (int i = 0; i < buttonPositions.length; i++) {
int[] pos = (int[]) buttonPositions[i];
setValueAt(buttonNames[i], pos[0], pos[1]);
}
Action action = new AbstractAction() {
public void actionPerformed(final ActionEvent e) {
String[] buttonPosition = e.getActionCommand().split(",");
int row = Integer.parseInt(buttonPosition[0]);
int column = Integer.parseInt(buttonPosition[1]);
System.out.println("row = " + row + ", column = " + column);
}
};
btHandler = new ButtonHandler(this, action, buttonPositions);
}
public TableCellRenderer getCellRenderer(final int row, final int column) {
return btHandler.getRenderer(row, column);
}
public TableCellEditor getCellEditor(final int row, final int column) {
return btHandler.getEditor(row, column);
}
}
import javax.swing.*;
/**
* A Main-Class, starting the demo-application
* @author: L-ectron-X
*/
public class Main {
private static void createAndShowGUI() {
JFrame f = new JFrame("ButtonHandler");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(new MyTable()));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}