TableLayout: Unterschied zwischen den Versionen
Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springenZeile 60: | Zeile 60: | ||
* TableLayout.java | * TableLayout.java | ||
*/ | */ | ||
+ | |||
import java.awt.*; | import java.awt.*; | ||
import java.awt.event.*; | import java.awt.event.*; | ||
Zeile 70: | Zeile 71: | ||
public class TableLayout extends JTable { | public class TableLayout extends JTable { | ||
+ | /** | ||
+ | * Constructs a TableLayout with 0 rows and 0 columns | ||
+ | */ | ||
public TableLayout() { | public TableLayout() { | ||
super(0, 0); | super(0, 0); | ||
Zeile 75: | Zeile 79: | ||
setDefaultEditor(JComponent.class, new TableLayoutEditor()); | setDefaultEditor(JComponent.class, new TableLayoutEditor()); | ||
setSurrendersFocusOnKeystroke(true); | setSurrendersFocusOnKeystroke(true); | ||
− | |||
} | } | ||
Zeile 112: | Zeile 115: | ||
} | } | ||
+ | /** | ||
+ | * | ||
+ | * @param columnNames one or more names to be added as columns | ||
+ | */ | ||
+ | public void addColumns(final String... columnNames) { | ||
+ | DefaultTableModel model = (DefaultTableModel) getModel(); | ||
+ | for (String name : columnNames) { | ||
+ | model.addColumn(name); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * | ||
+ | * @param components one or more components to be added as a row | ||
+ | */ | ||
public void addRow(final JComponent... components) { | public void addRow(final JComponent... components) { | ||
DefaultTableModel model = (DefaultTableModel) getModel(); | DefaultTableModel model = (DefaultTableModel) getModel(); | ||
Zeile 159: | Zeile 177: | ||
@Override | @Override | ||
public boolean shouldSelectCell(final EventObject anEvent) { | public boolean shouldSelectCell(final EventObject anEvent) { | ||
− | if (editorComponent != null && anEvent instanceof MouseEvent && | + | if (editorComponent != null && anEvent instanceof MouseEvent |
− | + | && ((MouseEvent) anEvent).getID() == MouseEvent.MOUSE_PRESSED) { | |
Component dispatchComponent = | Component dispatchComponent = | ||
SwingUtilities.getDeepestComponentAt(editorComponent, 3, 3); | SwingUtilities.getDeepestComponentAt(editorComponent, 3, 3); |
Version vom 30. Dezember 2009, 18:41 Uhr
Die unten gezeigte Klasse "TableLayout" ermöglicht das Anordnen von Swing Komponenten in Form einer Tabelle. "TableLayoutDemo" zeigt ein Beispiel, wie sie verwendet werden kann: <code=java> /*
* TableLayoutDemo.java */
import java.awt.*; import java.util.*; import javax.swing.*;
public class TableLayoutDemo extends JFrame {
private TableLayout tableLayout; private final Random r;
public TableLayoutDemo() { super("TableLayout Demo"); r = new Random(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(600, 600); setLocationRelativeTo(null); //create empty TableLayout: tableLayout = new TableLayout(); //fill the TableLayout: for (int row = 0; row < 5; row++) { //create a test panel: JPanel panel = createTestPanel(row); //add to TableLayout: tableLayout.addRow(panel); } getContentPane().add(new JScrollPane(tableLayout), BorderLayout.CENTER); }
private JPanel createTestPanel(int row) { JPanel panel = new JPanel(); panel.add(new JLabel("Panel " + row)); //generate random height for testing only: int height = r.nextInt(200); if (height < 34) { height = 34; } panel.setPreferredSize(new Dimension(0, height)); return panel; }
public static void main(final String[] args) { EventQueue.invokeLater(new Runnable() {
public void run() { new TableLayoutDemo().setVisible(true); } });
}
} </code=java> <code=java> /*
* TableLayout.java */
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*;
public class TableLayout extends JTable {
/** * Constructs a TableLayout with 0 rows and 0 columns */ public TableLayout() { super(0, 0); setDefaultRenderer(JComponent.class, new TableLayoutRenderer()); setDefaultEditor(JComponent.class, new TableLayoutEditor()); setSurrendersFocusOnKeystroke(true); }
@Override public TableCellRenderer getCellRenderer(final int row, final int column) { TableColumn tableColumn = getColumnModel().getColumn(column); TableCellRenderer renderer = tableColumn.getCellRenderer(); if (renderer == null) { Class c = getColumnClass(column); if (c.equals(Object.class)) { Object o = getValueAt(row, column); if (o != null) { c = getValueAt(row, column).getClass(); } } renderer = getDefaultRenderer(c); } return renderer; }
@Override public TableCellEditor getCellEditor(final int row, final int column) { TableColumn tableColumn = getColumnModel().getColumn(column); TableCellEditor editor = tableColumn.getCellEditor(); if (editor == null) { Class c = getColumnClass(column); if (c.equals(Object.class)) { Object o = getValueAt(row, column); if (o != null) { c = getValueAt(row, column).getClass(); } } editor = getDefaultEditor(c); } return editor; }
/** * * @param columnNames one or more names to be added as columns */ public void addColumns(final String... columnNames) { DefaultTableModel model = (DefaultTableModel) getModel(); for (String name : columnNames) { model.addColumn(name); } }
/** * * @param components one or more components to be added as a row */ public void addRow(final JComponent... components) { DefaultTableModel model = (DefaultTableModel) getModel(); while (model.getColumnCount() < components.length) { model.addColumn(""); } model.addRow(components); int h = 0; for (JComponent comp : components) { int h2 = comp.getPreferredSize().height; if (h < h2) { h = h2; } } setRowHeight(getRowCount() - 1, h); }
class TableLayoutEditor implements TableCellEditor, Serializable {
protected EventListenerList listenerList = new EventListenerList(); protected transient ChangeEvent changeEvent = null; protected JComponent editorComponent = null;
public Component getComponent() { return editorComponent; } /* implement CellEditor: getCellEditorValue isCellEditable shouldSelectCell stopCellEditing cancelCellEditing addCellEditorListener removeCellEditorListener */
@Override public Object getCellEditorValue() { return editorComponent; }
@Override public boolean isCellEditable(final EventObject anEvent) { return true; }
@Override public boolean shouldSelectCell(final EventObject anEvent) { if (editorComponent != null && anEvent instanceof MouseEvent && ((MouseEvent) anEvent).getID() == MouseEvent.MOUSE_PRESSED) { Component dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent, 3, 3); MouseEvent e = (MouseEvent) anEvent; MouseEvent e2 = new MouseEvent(dispatchComponent, MouseEvent.MOUSE_RELEASED, e.getWhen() + 100000, e.getModifiers(), 3, 3, e.getClickCount(), e.isPopupTrigger()); dispatchComponent.dispatchEvent(e2); e2 = new MouseEvent(dispatchComponent, MouseEvent.MOUSE_CLICKED, e.getWhen() + 100001, e.getModifiers(), 3, 3, 1, e.isPopupTrigger()); dispatchComponent.dispatchEvent(e2); } return true; }
@Override public boolean stopCellEditing() { fireEditingStopped(); return true; }
@Override public void cancelCellEditing() { fireEditingCanceled(); }
@Override public void addCellEditorListener(final CellEditorListener l) { listenerList.add(CellEditorListener.class, l); }
@Override public void removeCellEditorListener(final CellEditorListener l) { listenerList.remove(CellEditorListener.class, l); }
protected void fireEditingStopped() {//used in stopCellEditing Object[] listeners = listenerList.getListenerList(); // Process the listeners last to first, notifying // those that are interested in this event for (int i = listeners.length - 2; i >= 0; i -= 2) { if (listeners[i] == CellEditorListener.class) { // Lazily create the event: if (changeEvent == null) { changeEvent = new ChangeEvent(this); } ((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent); } } }
protected void fireEditingCanceled() {//used in cancelCellEditing // Guaranteed to return a non-null array Object[] listeners = listenerList.getListenerList(); // Process the listeners last to first, notifying // those that are interested in this event for (int i = listeners.length - 2; i >= 0; i -= 2) { if (listeners[i] == CellEditorListener.class) { // Lazily create the event: if (changeEvent == null) { changeEvent = new ChangeEvent(this); } ((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent); } } }
// implement TableCellEditor:
@Override public Component getTableCellEditorComponent(final JTable table, final Object value, final boolean isSelected, final int row, final int column) { editorComponent = (JComponent) value; return editorComponent; } }
class TableLayoutRenderer implements TableCellRenderer {
@Override public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) { return (JComponent) value; } }
} </code=java>