1. Java Swing Table API
package com.home.swing; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.util.Map; import java.util.TreeMap; public class JSTable { public JTable ctrl; Runnable selectedHandler; int selectedRow = -1; Map<Integer,Color> rowFgColor = new TreeMap<>(); Map<Integer,Color> rowBgColor = new TreeMap<>(); static class StripedTableCellRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column) { setEnabled(table == null || table.isEnabled()); if ((row % 2) == 0) setBackground(Color.green); else setBackground(Color.lightGray); super.getTableCellRendererComponent(table, value, selected, focused, row, column); return this; } } class ColoredTableCellRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column) { setEnabled(table == null || table.isEnabled()); Color fg = rowFgColor.get(row); Color bg = rowBgColor.get(row); if ( fg != null ) { setForeground(fg); } else setForeground(Color.black); if ( bg != null ) { setBackground(bg); } else setBackground(Color.white); super.getTableCellRendererComponent(table, value, selected, focused, row, column); return this; } } public JSTable(String[] colNames, int[] widths, int[] aligns, Runnable handler) { DefaultTableModel tableModel = new DefaultTableModel(colNames,0); ctrl = new JTable(tableModel) { private static final long serialVersionUID = 1L; public boolean isCellEditable(int row, int column) { return false; }; }; int colCount = ctrl.getModel().getColumnCount(); if( widths != null ) { for (int col = 0; col < colCount && col < widths.length; col++) { ctrl.getColumnModel().getColumn(col).setPreferredWidth(widths[col]); } } if( aligns != null ) { for( int col = 0; col < colCount && col < aligns.length; col++ ) { //DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(); ColoredTableCellRenderer centerRenderer = new ColoredTableCellRenderer(); if( aligns[col] < 0 ) centerRenderer.setHorizontalAlignment( SwingConstants.LEFT ); if( aligns[col] == 0 ) centerRenderer.setHorizontalAlignment( SwingConstants.CENTER ); if( aligns[col] > 0 ) centerRenderer.setHorizontalAlignment( SwingConstants.RIGHT ); ctrl.getColumnModel().getColumn(col).setCellRenderer(centerRenderer); } } selectedHandler = handler; ctrl.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent event) { if( selectedRow != ctrl.getSelectedRow() ) { selectedRow = ctrl.getSelectedRow(); selectedHandler.run(); } } }); } public void addRow( String[] row) { DefaultTableModel model = (DefaultTableModel) ctrl.getModel(); model.addRow(row); } public String[] getSelectedRow() { int count = ctrl.getModel().getColumnCount(); String[] row = new String[count]; for( int r = 0; r < count; r++ ) { row[r] = ctrl.getValueAt( ctrl.getSelectedRow(), r).toString(); } return row; } public String getValue(int row, int col) { return ctrl.getValueAt( row, col).toString(); } public void setValue(int row, int col, String value) { ctrl.setValueAt( value, row, col); } public void setRowColor(int row, Color fg, Color bg) { rowFgColor.put(row,fg); rowBgColor.put(row,bg); } }
댓글 없음:
댓글 쓰기