下面列出了javax.swing.JTable#getRowSorter ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static TableState createMetaState(final JTable table, final boolean selectedOnly) {
final TableState state = new TableState(table, selectedOnly);
final GraphTableModel tm = (GraphTableModel) table.getModel();
for (int i = 0; i < table.getColumnCount(); i++) {
final TableColumn tc = table.getColumnModel().getColumn(i);
final int modelIndex = tc.getModelIndex();
final Attribute attr = tm.getAttribute(modelIndex);
final String label = attr.getName();
final ColumnState cs = new ColumnState(label, tm.getSegment(modelIndex), tc.getWidth());
state.columns.add(cs);
}
final RowSorter<? extends TableModel> sorter = table.getRowSorter();
for (final RowSorter.SortKey sk : sorter.getSortKeys()) {
// TODO: should really store the column label + segment here.
state.sortOrder.add(String.format("%d,%s", sk.getColumn(), sk.getSortOrder()));
}
return state;
}
public static SortOrder getColumnSortOrder(JTable table, int column)
{
SortOrder rv = null;
if (table == null || table.getRowSorter() == null)
{
return rv;
}
java.util.List<? extends RowSorter.SortKey> sortKeys = table
.getRowSorter().getSortKeys();
if (sortKeys.size() > 0 && sortKeys.get(0).getColumn() == table
.convertColumnIndexToModel(column))
{
rv = sortKeys.get(0).getSortOrder();
}
return rv;
}
/**
* DOCUMENT ME!
*
* @param table DOCUMENT ME!
* @param column DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public static SortOrder getColumnSortOrder(JTable table, int column) {
SortOrder rv = null;
if (table == null || table.getRowSorter() == null) {
return rv;
}
java.util.List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();
if (sortKeys.size() > 0 && sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
rv = sortKeys.get(0).getSortOrder();
}
return rv;
}
private void tabPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabPaneStateChanged
JScrollPane sPane = (JScrollPane) tabPane.getSelectedComponent();
if (sPane != null) {
JViewport view = (JViewport) sPane.getComponent(0);
selectedTable = (JTable) view.getView();
// re-apply the filter to the current tab
if (selectedTable.getRowSorter() != null) {
((TableRowSorter) selectedTable.getRowSorter()).sort();
}
if (tabManagers != null) {
// Remove all toggle button listeners
for (ChangeListener l : autoScrollToggleButton.getChangeListeners()) {
autoScrollToggleButton.removeChangeListener(l);
}
LogTableManager manager = tabManagers.get(tabPane.getSelectedIndex());
// Add current listener
autoScrollToggleButton.addChangeListener(manager);
// Refresh buttons
autoScrollToggleButton.setSelected(manager.isAutoFollowScroll());
}
}
}
/**
* get all selected rows and DELETE one by one from the last.
*
* @param table the table to DELETE rows
*/
public static void deleterow(JTable table) {
int[] selectedrows = table.getSelectedRows();
for (int row : selectedrows) {
row = table.getSelectedRow();
if (table.getRowSorter() != null) {
row = table.getRowSorter().convertRowIndexToModel(row);
}
((DefaultTableModel) table.getModel()).removeRow(row);
}
}
/**
* Returns the current sort key, or null if the column is unsorted.
*
* @param table the table
* @param column the column index
* @return the SortKey, or null if the column is unsorted
*/
protected SortKey getSortKey(JTable table, int column) {
RowSorter<?> rowSorter = table.getRowSorter();
if (rowSorter == null) {
return null;
}
List<?> sortedColumns = rowSorter.getSortKeys();
if (sortedColumns.size() > 0) {
return (SortKey) sortedColumns.get(0);
}
return null;
}
@SuppressWarnings("unchecked")
public PollTabPane(Poll poll)
{
super();
//POLL DETAILS
this.pollDetailsPanel = new PollDetailsPanel(poll);
this.addTab("Poll Details", this.pollDetailsPanel);
//ALL VOTES
VotesTableModel allVotesTableModel = new VotesTableModel(poll.getVotes());
final JTable allVotesTable = Gui.createSortableTable(allVotesTableModel, 0);
TableRowSorter<VotesTableModel> sorter = (TableRowSorter<VotesTableModel>) allVotesTable.getRowSorter();
sorter.setComparator(VotesTableModel.COLUMN_VOTES, new BigDecimalStringComparator());
this.addTab("All Votes", new JScrollPane(allVotesTable));
//MY VOTES
VotesTableModel myVotesTableModel = new VotesTableModel(poll.getVotes(Controller.getInstance().getAccounts()));
final JTable myVotesTable = Gui.createSortableTable(myVotesTableModel, 0);
sorter = (TableRowSorter<VotesTableModel>) myVotesTable.getRowSorter();
sorter.setComparator(VotesTableModel.COLUMN_VOTES, new BigDecimalStringComparator());
this.addTab("My Votes", new JScrollPane(myVotesTable));
}
public Component getTableCellRendererComponent( final JTable table,
final Object value,
final boolean isSelected,
final boolean hasFocus,
final int row,
final int column ) {
final JLabel component =
(JLabel) super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
try {
if ( column == 0 ) {
final RepositoryTableModel tableModel = (RepositoryTableModel) table.getModel();
final RowSorter rowSorter = table.getRowSorter();
final FileObject e;
if ( rowSorter != null ) {
e = tableModel.getElementForRow( rowSorter.convertRowIndexToModel( row ) );
} else {
e = tableModel.getElementForRow( row );
}
if ( e.getType() == FileType.FOLDER ) {
component.setIcon( closedIcon );
} else {
component.setIcon( leafIcon );
}
} else {
component.setIcon( null );
}
} catch ( FileSystemException fse ) {
// ok, ugly, but not fatal.
}
return component;
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
TableRowSorter rowsorter = (TableRowSorter) table.getRowSorter();
LogTableModel model = (LogTableModel) table.getModel();
LogLineRowFilter rowfilter = null;
String line = htmlEscape(String.valueOf(value));
// if possible, try to translate the visual row number to the model's row number
if (rowsorter != null) {
row = rowsorter.convertRowIndexToModel(row);
if (rowsorter.getRowFilter() instanceof LogLineRowFilter) {
rowfilter = (LogLineRowFilter) rowsorter.getRowFilter();
}
}
// get the according event object
LogEvent event = model.getValueAt(row);
LogLevel level = event.getLevel();
final String htmlLeft = "<html><nobr>";
final String htmlRight = "</nobr></html>";
switch (column) {
case LogTableModel.COL_TIME: {
return super.getTableCellRendererComponent(table, htmlLeft + line + htmlRight, isSelected, hasFocus, row, column);
}
case LogTableModel.COL_TAG:
case LogTableModel.COL_PROCESS: {
line = rowfilter.highlight(line);
break;
}
case LogTableModel.COL_MESSAGE: {
line = rowfilter.highlight(line);
int indentSize = 0;
while (indentSize < line.length() && line.charAt(indentSize) == ' ') {
++indentSize;
}
String indent = indentSize == 0 ? "" : line.substring(0, indentSize - 1);
line = indentSize == 0 ? line : line.substring(indentSize);
if (event.getStackTraceElement() != null) {
line = "<pre>" + indent + "<u>" + line + "</u></pre>";
}
break;
}
}
Component c = super.getTableCellRendererComponent(table, htmlLeft + line + htmlRight, isSelected, hasFocus, row, column);
Color color = Color.BLACK;
switch (level) {
case ASSERT:
color = COLOR_ASSERT;
break;
case DEBUG:
color = COLOR_DEBUG;
break;
case ERROR:
color = COLOR_ERROR;
break;
case INFO:
color = COLOR_INFO;
break;
case WARN:
color = COLOR_WARN;
break;
default:
color = COLOR_VERBOSE;
break;
}
c.setForeground(color);
if (column == LogTableModel.COL_MESSAGE && c instanceof JLabel) {
((JLabel) c).setToolTipText(value != null ? value.toString() : "");
}
return c;
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
if (table == null)
{
return this;
}
if(column == table.getColumnCount() - 1)
{
setBorder(endColumnBorder);
}
else
{
setBorder(noramlBorder);
}
boolean isPaintingForPrint = false;
if(table.getTableHeader() != null)
{
isPaintingForPrint = table.getTableHeader().isPaintingForPrint();
}
Icon sortIcon = null;
if (!isPaintingForPrint && table.getRowSorter() != null)
{
SortOrder sortOrder = getColumnSortOrder(table, column);
if (sortOrder != null)
{
switch (sortOrder)
{
case ASCENDING:
sortIcon = UIManager.getIcon("Table.ascendingSortIcon");
break;
case DESCENDING:
sortIcon = UIManager.getIcon("Table.descendingSortIcon");
break;
case UNSORTED:
break;
}
}
}
setIcon(sortIcon);
setFont(table.getFont());
setValue(value);
return this;
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Icon sortIcon = null;
// setBackground(Constants.DS_BACK);
boolean isPaintingForPrint = false;
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
Color fgColor = null;
Color bgColor = null;
if (hasFocus) {
fgColor = UIManager.getColor("TableHeader.focusCellForeground");
bgColor = UIManager.getColor("TableHeader.focusCellBackground");
}
if (fgColor == null) {
fgColor = header.getForeground();
}
if (bgColor == null) {
bgColor = header.getBackground();
}
setFont(header.getFont());
isPaintingForPrint = header.isPaintingForPrint();
}
if (!isPaintingForPrint && table.getRowSorter() != null) {
if (!horizontalTextPositionSet) {
// There is a row sorter, and the developer hasn't
// set a text position, change to leading.
setHorizontalTextPosition(JLabel.LEADING);
}
java.util.List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();
if (sortKeys.size() > 0
&& sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
switch (sortKeys.get(0).getSortOrder()) {
case ASCENDING:
sortIcon = UIManager.getIcon("Table.ascendingSortIcon");
break;
case DESCENDING:
sortIcon = UIManager.getIcon("Table.descendingSortIcon");
break;
case UNSORTED:
sortIcon = UIManager.getIcon("Table.naturalSortIcon");
break;
}
}
}
}
setText(value == null ? "" : value.toString());
setIcon(sortIcon);
Border border = null;
if (hasFocus) {
border = UIManager.getBorder("TableHeader.focusCellBorder");
}
if (border == null) {
border = UIManager.getBorder("TableHeader.cellBorder");
}
setBorder(border);
return this;
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Icon sortIcon = null;
boolean isPaintingForPrint = false;
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
Color fgColor = null;
Color bgColor = null;
if (hasFocus) {
fgColor = UIManager.getColor("TableHeader.focusCellForeground");
bgColor = UIManager.getColor("TableHeader.focusCellBackground");
}
if (fgColor == null) {
fgColor = header.getForeground();
}
if (bgColor == null) {
bgColor = header.getBackground();
}
setForeground(fgColor);
setFont(header.getFont());
isPaintingForPrint = header.isPaintingForPrint();
}
if (!isPaintingForPrint && table.getRowSorter() != null) {
if (!horizontalTextPositionSet) {
// There is a row sorter, and the developer hasn't
// set a text position, change to leading.
setHorizontalTextPosition(JLabel.LEADING);
}
java.util.List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();
if (sortKeys.size() > 0
&& sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
switch (sortKeys.get(0).getSortOrder()) {
case ASCENDING:
sortIcon = UIManager.getIcon("Table.ascendingSortIcon");
break;
case DESCENDING:
sortIcon = UIManager.getIcon("Table.descendingSortIcon");
break;
case UNSORTED:
sortIcon = UIManager.getIcon("Table.naturalSortIcon");
break;
}
}
}
}
SupportTableModel model = (SupportTableModel) table.getModel();
ImageIcon icon = model.getColumnIcon((String) value);
BufferedImage i = ImageUtils.createCompatibleBufferedImage(18, 18, BufferedImage.BITMASK);
Graphics2D g2d = i.createGraphics();
// setIcon(sortIcon);
if (icon != null) {
icon.paintIcon(this, g2d, 0, 0);
setText("");
if (sortIcon != null) {
g2d.setColor(getBackground());
g2d.fillRect(18 - sortIcon.getIconWidth() - 2, 18 - sortIcon.getIconHeight() - 2, sortIcon.getIconWidth() + 2, sortIcon.getIconHeight() + 2);
sortIcon.paintIcon(this, g2d, 18 - sortIcon.getIconWidth() - 1, 18 - sortIcon.getIconHeight() - 1);
}
setIcon(new ImageIcon(i));
} else {
setIcon(sortIcon);
setText(value == null ? "" : value.toString());
}
Border border = null;
if (hasFocus) {
border = UIManager.getBorder("TableHeader.focusCellBorder");
}
if (border == null) {
border = UIManager.getBorder("TableHeader.cellBorder");
}
setBorder(border);
return this;
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Icon sortIcon = null;
boolean isPaintingForPrint = false;
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
Color fgColor = null;
Color bgColor = null;
if (hasFocus) {
fgColor = UIManager.getColor("TableHeader.focusCellForeground");
bgColor = UIManager.getColor("TableHeader.focusCellBackground");
}
if (fgColor == null) {
fgColor = header.getForeground();
}
if (bgColor == null) {
bgColor = header.getBackground();
}
setForeground(fgColor);
setFont(header.getFont());
isPaintingForPrint = header.isPaintingForPrint();
}
if (!isPaintingForPrint && table.getRowSorter() != null) {
if (!horizontalTextPositionSet) {
// There is a row sorter, and the developer hasn't
// set a text position, change to leading.
setHorizontalTextPosition(JLabel.LEADING);
}
java.util.List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();
if (sortKeys.size() > 0
&& sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
switch (sortKeys.get(0).getSortOrder()) {
case ASCENDING:
sortIcon = UIManager.getIcon("Table.ascendingSortIcon");
break;
case DESCENDING:
sortIcon = UIManager.getIcon("Table.descendingSortIcon");
break;
case UNSORTED:
sortIcon = UIManager.getIcon("Table.naturalSortIcon");
break;
}
}
}
}
TroopsTableModel model = (TroopsTableModel) table.getModel();
ImageIcon icon = model.getColumnIcon((String) value);
BufferedImage i = ImageUtils.createCompatibleBufferedImage(18, 18, BufferedImage.BITMASK);
Graphics2D g2d = i.createGraphics();
// setIcon(sortIcon);
if (icon != null) {
icon.paintIcon(this, g2d, 0, 0);
setText("");
if (sortIcon != null) {
g2d.setColor(getBackground());
g2d.fillRect(18 - sortIcon.getIconWidth() - 2, 18 - sortIcon.getIconHeight() - 2, sortIcon.getIconWidth() + 2, sortIcon.getIconHeight() + 2);
sortIcon.paintIcon(this, g2d, 18 - sortIcon.getIconWidth() - 1, 18 - sortIcon.getIconHeight() - 1);
}
setIcon(new ImageIcon(i));
} else {
setIcon(sortIcon);
setText(value == null ? "" : value.toString());
}
Border border = null;
if (hasFocus) {
border = UIManager.getBorder("TableHeader.focusCellBorder");
}
if (border == null) {
border = UIManager.getBorder("TableHeader.cellBorder");
}
setBorder(border);
return this;
}
/**
* @see com.seaglasslookandfeel.ui.SeaGlassTableHeaderUI$DefaultTableCellHeaderRenderer#getTableCellRendererComponent(javax.swing.JTable,
* java.lang.Object, boolean, boolean, int, int)
*/
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
boolean hasRollover = false; // (column == getRolloverColumn());
if (isSelected || hasRollover || hasFocus) {
SeaGlassLookAndFeel.setSelectedUI((SeaGlassLabelUI) SeaGlassLookAndFeel.getUIOfType(getUI(), SeaGlassLabelUI.class),
isSelected, hasFocus, table.isEnabled(), hasRollover);
} else {
SeaGlassLookAndFeel.resetSelectedUI();
}
// Stuff a variable into the client property of this renderer
// indicating the sort order, so that different rendering can be
// done for the header based on sorted state.
RowSorter rs = table == null ? null : table.getRowSorter();
java.util.List<? extends RowSorter.SortKey> sortKeys = rs == null ? null : rs.getSortKeys();
if (sortKeys != null && sortKeys.size() > 0 && sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
switch (sortKeys.get(0).getSortOrder()) {
case ASCENDING:
putClientProperty("Table.sortOrder", "ASCENDING");
break;
case DESCENDING:
putClientProperty("Table.sortOrder", "DESCENDING");
break;
case UNSORTED:
putClientProperty("Table.sortOrder", "UNSORTED");
break;
default:
throw new AssertionError("Cannot happen");
}
} else {
putClientProperty("Table.sortOrder", "UNSORTED");
}
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
return this;
}
/**
* @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
* java.lang.Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Icon sortIcon = null;
boolean isPaintingForPrint = false;
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
Color fgColor = null;
Color bgColor = null;
if (hasFocus) {
fgColor = DefaultLookup.getColor(this, ui, "TableHeader.focusCellForeground");
bgColor = DefaultLookup.getColor(this, ui, "TableHeader.focusCellBackground");
}
if (fgColor == null) {
fgColor = header.getForeground();
}
if (bgColor == null) {
bgColor = header.getBackground();
}
setForeground(fgColor);
setBackground(bgColor);
setFont(header.getFont());
isPaintingForPrint = header.isPaintingForPrint();
}
if (!isPaintingForPrint && table.getRowSorter() != null) {
SortOrder sortOrder = getColumnSortOrder(table, column);
if (sortOrder != null) {
switch (sortOrder) {
case ASCENDING:
sortIcon = DefaultLookup.getIcon(this, ui, "Table.ascendingSortIcon");
break;
case DESCENDING:
sortIcon = DefaultLookup.getIcon(this, ui, "Table.descendingSortIcon");
break;
case UNSORTED:
sortIcon = DefaultLookup.getIcon(this, ui, "Table.naturalSortIcon");
break;
}
}
}
}
if (value instanceof Icon) {
setText("");
setIcon((Icon) value);
} else if (value instanceof JLabel) {
JLabel label = (JLabel) value;
setText(label.getText());
setIcon(label.getIcon());
setHorizontalAlignment(label.getHorizontalAlignment());
setHorizontalTextPosition(label.getHorizontalTextPosition());
setVerticalAlignment(label.getVerticalAlignment());
setVerticalTextPosition(label.getVerticalTextPosition());
setToolTipText(label.getToolTipText());
} else {
setText(value == null ? "" : value.toString());
setIcon(null);
}
sortArrow = sortIcon;
Border border = null;
if (hasFocus) {
border = DefaultLookup.getBorder(this, ui, "TableHeader.focusCellBorder");
}
if (border == null) {
border = DefaultLookup.getBorder(this, ui, "TableHeader.cellBorder");
}
setBorder(border);
return this;
}