/* * * Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** * main method allows us to run as a standalone demo.
*/ publicstaticvoid main(String[] args) {
TableDemo demo = new TableDemo(null);
demo.mainImpl();
}
// Show that showHorizontal/Vertical controls are related
relatedComponents.removeAllElements();
relatedComponents.add(showHorizontalLinesCheckBox);
relatedComponents.add(showVerticalLinesCheckBox);
buildAccessibleGroup(relatedComponents);
// Show that row/column selections are related
relatedComponents.removeAllElements();
relatedComponents.add(isColumnSelectionAllowedCheckBox);
relatedComponents.add(isRowSelectionAllowedCheckBox);
buildAccessibleGroup(relatedComponents);
// Show that spacing controls are related
relatedComponents.removeAllElements();
relatedComponents.add(interCellSpacingSlider);
relatedComponents.add(rowHeightSlider);
buildAccessibleGroup(relatedComponents);
// Create the table.
tableAggregate = createTable();
getDemoPanel().add(tableAggregate, BorderLayout.CENTER);
// ComboBox for selection modes.
JPanel selectMode = new JPanel();
selectMode.setLayout(new BoxLayout(selectMode, BoxLayout.X_AXIS));
selectMode.setBorder(new TitledBorder(getString("TableDemo.selection_mode")));
JPanel buttons = new JPanel();
buttons.add(fitWidth);
buttons.add(printButton);
printPanel.add(buttons);
// Show that printing controls are related
relatedComponents.removeAllElements();
relatedComponents.add(headerTextField);
relatedComponents.add(footerTextField);
relatedComponents.add(printButton);
buildAccessibleGroup(relatedComponents);
// wrap up the panels and add them
JPanel sliderWrapper = new JPanel();
sliderWrapper.setLayout(new BoxLayout(sliderWrapper, BoxLayout.X_AXIS));
sliderWrapper.add(labelPanel);
sliderWrapper.add(sliderPanel);
sliderWrapper.add(Box.createHorizontalGlue());
sliderWrapper.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 0));
JPanel leftWrapper = new JPanel();
leftWrapper.setLayout(new BoxLayout(leftWrapper, BoxLayout.Y_AXIS));
leftWrapper.add(cbPanel);
leftWrapper.add(sliderWrapper);
/** * Sets the Accessibility MEMBER_OF property to denote that * these components work together as a group. Each object * is set to be a MEMBER_OF an array that contains all of * the objects in the group, including itself. * * @param components The list of objects that are related
*/ void buildAccessibleGroup(Vector<JComponent> components) {
AccessibleContext context = null; int numComponents = components.size();
Object[] group = components.toArray();
Object object = null; for (int i = 0; i < numComponents; ++i) {
object = components.elementAt(i); if (object instanceof Accessible) {
context = ((Accessible)components.elementAt(i)).
getAccessibleContext();
context.getAccessibleRelationSet().add( new AccessibleRelation(
AccessibleRelation.MEMBER_OF, group));
}
}
} // buildAccessibleGroup()
/** * This sets CONTROLLER_FOR on the controls that manipulate the * table and CONTROLLED_BY relationships on the table to point * back to the controllers.
*/ privatevoid setTableControllers() {
// Set up the relationships to show what controls the table
setAccessibleController(isColumnReorderingAllowedCheckBox,
tableAggregate);
setAccessibleController(showHorizontalLinesCheckBox,
tableAggregate);
setAccessibleController(showVerticalLinesCheckBox,
tableAggregate);
setAccessibleController(isColumnSelectionAllowedCheckBox,
tableAggregate);
setAccessibleController(isRowSelectionAllowedCheckBox,
tableAggregate);
setAccessibleController(interCellSpacingSlider,
tableAggregate);
setAccessibleController(rowHeightSlider,
tableAggregate);
setAccessibleController(selectionModeComboBox,
tableAggregate);
setAccessibleController(resizeModeComboBox,
tableAggregate);
} // setTableControllers()
/** * Sets up accessibility relationships to denote that one * object controls another. The CONTROLLER_FOR property is * set on the controller object, and the CONTROLLED_BY * property is set on the target object.
*/ privatevoid setAccessibleController(JComponent controller,
JComponent target) {
AccessibleRelationSet controllerRelations =
controller.getAccessibleContext().getAccessibleRelationSet();
AccessibleRelationSet targetRelations =
target.getAccessibleContext().getAccessibleRelationSet();
controllerRelations.add( new AccessibleRelation(
AccessibleRelation.CONTROLLER_FOR, target));
targetRelations.add( new AccessibleRelation(
AccessibleRelation.CONTROLLED_BY, controller));
} // setAccessibleController()
public JScrollPane createTable() {
// final final String[] names = {
getString("TableDemo.first_name"),
getString("TableDemo.last_name"),
getString("TableDemo.favorite_color"),
getString("TableDemo.favorite_movie"),
getString("TableDemo.favorite_number"),
getString("TableDemo.favorite_food")
};
// Create a model of the data.
TableModel dataModel = new AbstractTableModel() { publicint getColumnCount() { return names.length; } publicint getRowCount() { return data.length;} public Object getValueAt(int row, int col) {return data[row][col];} public String getColumnName(int column) {return names[column];} publicClass<?> getColumnClass(int c) {
Object obj = getValueAt(0, c); return obj != null ? obj.getClass() : Object.class;
} publicboolean isCellEditable(int row, int col) {return col != 5;} publicvoid setValueAt(Object aValue, int row, int column) { data[row][column] = aValue; }
};
// Create the table
tableView = new JTable(dataModel);
TableRowSorter<TableModel> sorter = new TableRowSorter<>(dataModel);
tableView.setRowSorter(sorter);
// Show colors by rendering them in their own color.
DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() { publicvoid setValue(Object value) { if (value instanceof NamedColor) {
NamedColor c = (NamedColor) value;
setBackground(c);
setForeground(c.getTextColor());
setText(c.toString());
} else { super.setValue(value);
}
}
};
// Create a combo box to show that you can use one in a table.
JComboBox<NamedColor> comboBox = new JComboBox<>();
comboBox.addItem(aqua);
comboBox.addItem(beige);
comboBox.addItem(black);
comboBox.addItem(blue);
comboBox.addItem(eblue);
comboBox.addItem(jfcblue);
comboBox.addItem(jfcblue2);
comboBox.addItem(cybergreen);
comboBox.addItem(darkgreen);
comboBox.addItem(forestgreen);
comboBox.addItem(gray);
comboBox.addItem(green);
comboBox.addItem(orange);
comboBox.addItem(purple);
comboBox.addItem(red);
comboBox.addItem(rustred);
comboBox.addItem(sunpurple);
comboBox.addItem(suspectpink);
comboBox.addItem(turquoise);
comboBox.addItem(violet);
comboBox.addItem(yellow);
TableColumn colorColumn = tableView.getColumn(getString("TableDemo.favorite_color")); // Use the combo box as the editor in the "Favorite Color" column.
colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
class NamedColor extends Color {
String name; public NamedColor(Color color, String name) { super(color.getRGB()); this.name = name;
}
public Color getTextColor() { int r = getRed(); int g = getGreen(); int b = getBlue(); if(r > 240 || g > 240) { return Color.black;
} else { return Color.white;
}
}
public String toString() { return name;
}
}
class ColumnLayout implements LayoutManager { int xInset = 5; int yInset = 5; int yGap = 2;
publicvoid addLayoutComponent(String s, Component c) {}
publicvoid layoutContainer(Container c) {
Insets insets = c.getInsets(); int height = yInset + insets.top;
Component[] children = c.getComponents();
Dimension compSize = null; for (int i = 0; i < children.length; i++) {
compSize = children[i].getPreferredSize();
children[i].setSize(compSize.width, compSize.height);
children[i].setLocation( xInset + insets.left, height);
height += compSize.height + yGap;
}
}
public Dimension minimumLayoutSize(Container c) {
Insets insets = c.getInsets(); int height = yInset + insets.top; int width = 0 + insets.left + insets.right;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.