TableLayout: Unterschied zwischen den Versionen
Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen (Die Seite wurde neu angelegt: „Die unten gezeigte Klasse "TableLayout" ermöglicht das Anordnen von Swing Komponenten in Form einer Tabelle. "TableLayoutDemo" zeigt ein Beispiel, wie sie verwe…“) |
|||
Zeile 112: | Zeile 112: | ||
} | } | ||
− | public void addRow(final | + | public void addRow(final JComponent... components) { |
DefaultTableModel model = (DefaultTableModel) getModel(); | DefaultTableModel model = (DefaultTableModel) getModel(); | ||
− | while (model.getColumnCount() < | + | while (model.getColumnCount() < components.length) { |
model.addColumn(""); | model.addColumn(""); | ||
} | } | ||
− | model.addRow( | + | model.addRow(components); |
int h = 0; | int h = 0; | ||
− | for ( | + | for (JComponent comp : components) { |
− | int h2 = | + | int h2 = comp.getPreferredSize().height; |
if (h < h2) { | if (h < h2) { | ||
h = h2; | h = h2; |
Version vom 4. November 2009, 21:25 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 {
public TableLayout() { super(0, 0); setDefaultRenderer(JComponent.class, new TableLayoutRenderer()); setDefaultEditor(JComponent.class, new TableLayoutEditor()); setSurrendersFocusOnKeystroke(true); setTableHeader(null); }
@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; }
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>