1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
| public class ArrayPanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final Insets INSETS_VER = new Insets(10, 0, 10, 0);
private static final Insets INSETS_HOR= new Insets(0, 10, 0, 10);
private static final Insets INSETS= new Insets(1, 1, 0, 0);
private int rowCount;
private int columnCount;
private Color lineColor;
private boolean grid;
public ArrayPanel() {
super(new GridBagLayout());
lineColor=Color.WHITE;
}
public void setLineColor(Color color) {
if ( color==null ) {
color=Color.WHITE;
}
this.lineColor=color;
repaint();
}
@Override
public void setForeground(Color fg) {
super.setForeground(fg);
for(int row=0; row<rowCount; row++) {
JLabel jlabel = (JLabel)getComponent(0, row+1);
jlabel.setForeground(fg);
}
for(int column=0; column<columnCount; column++) {
JLabel jlabel = (JLabel)getComponent(column+1,0);
jlabel.setForeground(fg);
}
}
@Override
public void setFont(Font font) {
super.setFont(font);
for(int row=0; row<rowCount; row++) {
JLabel jlabel = (JLabel)getComponent(0, row+1);
jlabel.setFont(font);
}
for(int column=0; column<columnCount; column++) {
JLabel jlabel = (JLabel)getComponent(column+1,0);
jlabel.setFont(font);
}
}
public void setGridVisible(boolean grid) {
this.grid=grid;
repaint();
}
public boolean isGridVisible() {
return grid;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(lineColor);
if ( columnCount>0 ) {
int headerWidth=0;
if (!grid && rowCount>0) {
JLabel jlabel = (JLabel)getComponent(0,1);
headerWidth=jlabel.getWidth();
}
for(int i=0; i<=rowCount; i++) {
Component component = getComponent(1,i);
if( component==null) continue;
if ( grid || i==0 ) {
if ( i==0 ) g.drawLine(0, component.getY(), getWidth(), component.getY());
g.drawLine(0, component.getY()+component.getHeight(), getWidth(), component.getY()+component.getHeight());
}
else {
if ( i==0 ) g.drawLine(0, component.getY(), headerWidth, component.getY());
g.drawLine(0, component.getY()+component.getHeight(), headerWidth, component.getY()+component.getHeight());
}
}
}
if ( rowCount>0 ) {
int headerHeight=0;
if (!grid && columnCount>0) {
JLabel jlabel = (JLabel)getComponent(1,0);
headerHeight=jlabel.getHeight();
}
for(int i=0; i<=columnCount; i++) {
Component component = getComponent(i, 1);
if( component==null) continue;
if ( grid || i==0 ) {
if ( i==0 ) g.drawLine(component.getX(), 0, component.getX(), getHeight());
g.drawLine(component.getX()+component.getWidth(), 0, component.getX()+component.getWidth(), getHeight());
}
else {
if ( i==0 ) g.drawLine(component.getX(), 0, component.getX(), headerHeight);
g.drawLine(component.getX()+component.getWidth(), 0, component.getX()+component.getWidth(), headerHeight);
}
}
}
}
public void addRow(String label) {
if ( label==null ) {
label="";
}
int newRow = ++rowCount;
createRow(label, newRow);
}
protected JLabel createRow(String label, int newRow) {
JLabel jlabel = createJLabel(label);
jlabel.setUI(new VerticalLabelUI(false));
add(jlabel, new GridBagConstraints(0, newRow, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.CENTER, INSETS_VER, 0, 0));
fillRow(newRow);
revalidate();
repaint();
return jlabel;
}
public void addRow(String label, int row) {
if ( row<0 || row>rowCount ) {
throw new IndexOutOfBoundsException();
}
else if ( row==rowCount ) {
addRow(label);
}
else {
if ( label==null ) {
label="";
}
GridBagLayout layout = (GridBagLayout) getLayout();
for(int i=columnCount; i>=0; i--) {
for(int j=rowCount; j>=row; j--) {
Component component = getComponent(i,j);
GridBagConstraints gbc = layout.getConstraints(component);
gbc.gridy++;
layout.setConstraints(component, gbc);
}
}
rowCount++;
createRow(label, row);
}
}
protected JLabel createJLabel(String label) {
JLabel jlabel = new JLabel(label, JLabel.CENTER);
jlabel.setForeground(getForeground());
jlabel.setFont(getFont());
return jlabel;
}
private void fillRow(int newRow) {
for(int i=1; i<=columnCount; i++) {
add( new JPanel(), new GridBagConstraints(i, newRow, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, INSETS, 0, 0));
}
}
public void addColumn(String label) {
if ( label==null ) {
label="";
}
int newColumn = ++columnCount;
createColumn(label, newColumn);
}
protected JLabel createColumn(String label, int newColumn) {
JLabel jlabel = createJLabel(label);
add(jlabel, new GridBagConstraints(newColumn, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.CENTER, INSETS_HOR, 0, 0));
fillColumn(newColumn);
revalidate();
repaint();
return jlabel;
}
private void fillColumn(int newColumn) {
for(int i=1; i<=rowCount; i++) {
add( new JPanel(), new GridBagConstraints(newColumn, i, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, INSETS, 0, 0));
}
}
public void addColumn(String label, int column) {
if ( column<0 || column>columnCount ) {
throw new IndexOutOfBoundsException();
}
else if ( column==columnCount ) {
addColumn(label);
}
else {
if ( label==null ) {
label="";
}
GridBagLayout layout = (GridBagLayout) getLayout();
for(int i=rowCount; i>=0; i--) {
for(int j=columnCount; j>=column; j--) {
Component component = getComponent(j,i);
GridBagConstraints gbc = layout.getConstraints(component);
gbc.gridx++;
layout.setConstraints(component, gbc);
}
}
columnCount++;
createColumn(label, column);
}
}
public void swapRow(int row1, int row2) {
if ( row1<0 || row1>=rowCount || row2<0 || row2>=rowCount ) throw new IndexOutOfBoundsException();
if ( row1==row2 ) return;
if ( row1<row2 ) {
swapRow(row2, row1);
return;
}
GridBagLayout layout = (GridBagLayout) getLayout();
for(int i=columnCount; i>=0; i--) {
Component component1 = getComponent(i,row1+1);
GridBagConstraints gbc1 = layout.getConstraints(component1);
gbc1.gridy=row2+1;
layout.setConstraints(component1, gbc1);
Component component2 = getComponent(i,row2+1);
GridBagConstraints gbc2 = layout.getConstraints(component2);
gbc2.gridy=row1+1;
layout.setConstraints(component2, gbc2);
}
revalidate();
repaint();
}
public void swapColumn(int column1, int column2) {
if ( column1<0 || column1>=columnCount || column2<0 || column2>=columnCount ) throw new IndexOutOfBoundsException();
if ( column1==column2 ) return;
if ( column1<column2) {
swapColumn(column2, column1);
return;
}
GridBagLayout layout = (GridBagLayout) getLayout();
for(int i=rowCount; i>=0; i--) {
Component component1 = getComponent(column1+1,i);
GridBagConstraints gbc1 = layout.getConstraints(component1);
gbc1.gridx=column2+1;
layout.setConstraints(component1, gbc1);
Component component2 = getComponent(column2+1,i);
GridBagConstraints gbc2 = layout.getConstraints(component2);
gbc2.gridx=column1+1;
layout.setConstraints(component2, gbc2);
}
revalidate();
repaint();
}
public void setRowLabel(String label, int row) {
if ( row<0 || row>=rowCount ) throw new IndexOutOfBoundsException();
if ( label==null ) {
label="";
}
JLabel jlabel = (JLabel)getComponent(0, row+1);
jlabel.setText(label);
revalidate();
repaint();
}
public void setColumnLabel(String label, int column) {
if ( column<0 || column>=columnCount ) throw new IndexOutOfBoundsException();
if ( label==null ) {
label="";
}
JLabel jlabel = (JLabel)getComponent(column+1, 0);
jlabel.setText(label);
revalidate();
repaint();
}
public void removeRow(int row) {
if ( row<0 || row>=rowCount ) throw new IndexOutOfBoundsException();
for(int i=columnCount; i>=0; i--) {
Component component = getComponent(i,row+1);
remove(component);
}
GridBagLayout layout = (GridBagLayout) getLayout();
for(int i=columnCount; i>=0; i--) {
for(int j=row+2; j<=rowCount; j++) {
Component component = getComponent(i,j);
GridBagConstraints gbc = layout.getConstraints(component);
gbc.gridy--;
layout.setConstraints(component, gbc);
}
}
rowCount--;
revalidate();
repaint();
}
public void removeColumn(int column) {
if ( column<0 || column>=columnCount ) throw new IndexOutOfBoundsException();
for(int i=rowCount; i>=0; i--) {
Component component = getComponent(column+1, i);
remove(component);
}
GridBagLayout layout = (GridBagLayout) getLayout();
for(int i=rowCount; i>=0; i--) {
for(int j=column+2; j<=columnCount; j++) {
Component component = getComponent(j,i);
GridBagConstraints gbc = layout.getConstraints(component);
gbc.gridx--;
layout.setConstraints(component, gbc);
}
}
columnCount--;
revalidate();
repaint();
}
public int getRowCount() {
return rowCount;
}
public int getColumnCount() {
return columnCount;
}
public void setPanel(JPanel panel, int row, int column) {
if ( row<0 || row>=rowCount || column<0 || column>=columnCount ) throw new IndexOutOfBoundsException();
removeComponentAt(column+1, row+1);
add(panel, new GridBagConstraints(column+1, row+1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, INSETS, 0, 0));
revalidate();
repaint();
}
private void removeComponentAt(int column, int row) {
Component component = getComponent(row, column);
if ( component!=null ) {
remove(component);
}
}
private Component getComponent(int column, int row) {
for(Component component : getComponents()) {
GridBagConstraints gbc = ((GridBagLayout)getLayout()).getConstraints(component);
if ( gbc.gridx==column && gbc.gridy==row ) {
return component;
}
}
return null;
}
} |
Partager