public void addGridComponent(Component component) { GridLayout l = (GridLayout) getContentPane().getLayout(); // Make more sections if necessary int availableSections = l.getColumns() * l.getRows(); if (availableSections == getContentPane().getComponentCount()) { getContentPane().setLayout(new GridLayout(l.getColumns() + 1, l.getRows() + 1)); } getContentPane().add(component); validate(); }
public void removeGridComponent(Component component) { GridLayout l = (GridLayout) getContentPane().getLayout(); // Remove sections if necessary int threshold = (l.getColumns() - 1) * (l.getRows() - 1); if (threshold > 0 && threshold == getContentPane().getComponentCount() - 1) { getContentPane().setLayout(new GridLayout(l.getColumns() - 1, l.getRows() - 1)); } getContentPane().remove(component); validate(); } }
GridLayout layout = (GridLayout) jPanel1.getLayout(); int numCol = layout.getColumns(); int index = (row-1)*numCol + col-1; if(index < jPanel1.getComponentCount()) jPanel1.getComponent(index).setEnabled(false);
@ScriptFunction public int getColumns() { return layout.getColumns(); } protected JSObject published;
JPanel m = new JPanel(); GridLayout gridLayout = new GridLayout(3, 3,0,0); m.setLayout( gridLayout); for (int i = 0; i < gridLayout.getRows(); i++) { for (int ii = 0; ii < gridLayout.getColumns(); ii++) { JPanel p = new JPanel(); p.add(new JButton("X")); m.add(p, i, ii); } } frame.add(m);
@ScriptFunction(jsDoc = ADD_JSDOC, params = {"component", "row", "column"}) public void add(JComponent aComp, int aRow, int aCol) { int index = aRow * layout.getColumns() + aCol; super.remove(index); if (aComp != null) { super.add(aComp, index); } else { super.add(new PlaceHolder(), index); } super.revalidate(); super.repaint(); }
@ScriptFunction(jsDoc = REMOVE_JSDOC, params = {"component"}) @Override public void remove(JComponent aComp) { for (int i = 0; i < layout.getRows(); i++) { for (int j = 0; j < layout.getColumns(); j++) { int index = i * layout.getColumns() + j; if (super.getComponent(index) == aComp) { super.remove(index); super.add(new PlaceHolder(), index); super.revalidate(); super.repaint(); return; } } } }
@ScriptFunction @Undesignable @Override public JComponent[] children() { List<JComponent> ch = new ArrayList<>(); for (int i = 0; i < layout.getRows(); i++) { for (int j = 0; j < layout.getColumns(); j++) { int index = i * layout.getColumns() + j; JComponent comp = (JComponent) super.getComponent(index); if (comp instanceof PlaceHolder) { ch.add(null); } else { ch.add(comp); } } } return ch.toArray(new JComponent[]{}); }
@Override public void componentResized(ComponentEvent ce) { GridLayout expansionLayout = (GridLayout)expansion.getLayout(); int cols = Math.max(1, expansion.getWidth() / widestCheck); if (cols != expansionLayout.getColumns()) { expansionLayout.setColumns(cols); expansion.validate(); } } };
@ScriptFunction(jsDoc = GRID_CHILD_JSDOC, params = {"row", "column"}) public JComponent child(int aRow, int aCol) { int index = aRow * layout.getColumns() + aCol; if (index >= 0 && index < getCount()) { JComponent res = (JComponent) super.getComponent(index); return res instanceof PlaceHolder ? null : res; } else { return null; } }
public void renderMeasurements (final @Nonnull String[][] measurements) { assert EventQueue.isDispatchThread(); innerPanel.removeAll(); gridLayout.setColumns(measurements[0].length); gridLayout.setRows(measurements.length); gridLayout.setHgap(1); gridLayout.setVgap(1); for (int row = 0; row < gridLayout.getRows(); row++) { for (int column = 0; column < gridLayout.getColumns(); column++) { innerPanel.add(new MeasurementPanel(measurements[row][column])); } } innerPanel.validate(); } }
public void renderMeasurements (final @Nonnull String[][] measurements) { assert EventQueue.isDispatchThread(); innerPanel.removeAll(); gridLayout.setColumns(measurements[0].length); gridLayout.setRows(measurements.length); gridLayout.setHgap(1); gridLayout.setVgap(1); for (int row = 0; row < gridLayout.getRows(); row++) { for (int column = 0; column < gridLayout.getColumns(); column++) { innerPanel.add(new MeasurementPanel(measurements[row][column])); } } innerPanel.validate(); } }
@Override public void componentResized(ComponentEvent e) { int oldCols = ((GridLayout)getLayout()).getColumns(); int newCols = Math.min(getComponentCount(), getWidth() / widestCheck); if (newCols != oldCols) { Tool.LOG.info("New width for " + argument.name + " was " + getWidth() + ", setting columns to " + newCols); ((GridLayout)getLayout()).setColumns(newCols); revalidate(); } } });
int ncomponents = parent.getComponentCount(); int nrows = getRows(); int ncols = getColumns(); boolean ltr = parent.getComponentOrientation().isLeftToRight();
for (int col=0; col<grid.getColumns(); col++) { for (int row=0; row<grid.getRows(); row++) { Component c = grid.getComponent(col, row);
@Test public void testLayouts() { final GridLayout result = (GridLayout) JPanelBuilder.builder() .gridLayout(1, 2) .build() .getLayout(); assertThat(result.getRows(), is(1)); assertThat(result.getColumns(), is(2)); assertThat(JPanelBuilder.builder() .gridBagLayout(2) .build() .getLayout(), instanceOf(GridBagLayout.class)); assertThat(JPanelBuilder.builder() .flowLayout() .build() .getLayout(), instanceOf(FlowLayout.class)); assertThat(JPanelBuilder.builder() .borderLayout() .build() .getLayout(), instanceOf(BorderLayout.class)); }