Tabnine Logo
GridLayout
Code IndexAdd Tabnine to your IDE (free)

How to use
GridLayout
in
java.awt

Best Java code snippets using java.awt.GridLayout (Showing top 20 results out of 7,425)

Refine searchRefine arrow

  • Container
  • JFrame
  • Window
  • JPanel
  • JButton
  • JComponent
origin: opentripplanner/OpenTripPlanner

protected JComponent makeTextPanel(String text) {
  JPanel panel = new JPanel(false);
  JLabel filler = new JLabel(text);
  filler.setHorizontalAlignment(JLabel.CENTER);
  panel.setLayout(new GridLayout(1, 1));
  panel.add(filler);
  return panel;
}
origin: 4thline/cling

  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();
  }
}
origin: 4thline/cling

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();
}
origin: stackoverflow.com

final JButton b = new JButton("r" + row + ",c" + col);
b.addActionListener(new ActionListener() {
JPanel p = new JPanel(new GridLayout(N, N));
for (int i = 0; i < N * N; i++) {
  int row = i / N;
  JButton gb = createGridButton(row, col);
  list.add(gb);
  p.add(gb);
JFrame f = new JFrame("GridButton");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(createGridPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
origin: igniterealtime/Smack

private void createDebug() {
  frame = new JFrame("Smack Debug Window");
    frame.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent evt) {
  JPanel informationPanel = new JPanel();
  informationPanel.setLayout(new BoxLayout(informationPanel, BoxLayout.Y_AXIS));
  JPanel versionPanel = new JPanel();
  versionPanel.setLayout(new BoxLayout(versionPanel, BoxLayout.X_AXIS));
  versionPanel.setMaximumSize(new Dimension(2000, 31));
  JPanel iqProvidersPanel = new JPanel();
  iqProvidersPanel.setLayout(new GridLayout(1, 1));
  iqProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed IQ Providers"));
  Vector<String> providers = new Vector<>();
  JPanel extensionProvidersPanel = new JPanel();
  extensionProvidersPanel.setLayout(new GridLayout(1, 1));
  extensionProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed Extension Providers"));
  providers = new Vector<>();
  frame.getContentPane().add(tabbedPane);
  frame.setSize(650, 400);
origin: stackoverflow.com

 JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new GridLayout(4, 4));
firstPanel.setMaximumSize(new Dimension(400, 400));
JButton btn;
for (int i=1; i<=4; i++) {
  for (int j=1; j<=4; j++) {
    btn = new JButton();
    btn.setPreferredSize(new Dimension(100, 100));
    firstPanel.add(btn);
JPanel secondPanel = new JPanel();
secondPanel.setLayout(new GridLayout(5, 13));
secondPanel.setMaximumSize(new Dimension(520, 200));
for (int i=1; i<=5; i++) {
  for (int j=1; j<=13; j++) {
    btn = new JButton();
    btn.setPreferredSize(new Dimension(40, 40));
    secondPanel.add(btn);
frame.setSize(520,600);
origin: kiegroup/optaplanner

  return;
final JFrame exceptionFrame = new JFrame("Uncaught exception: " + e.getMessage());
Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
BufferedImage errorImage = new BufferedImage(
    errorIcon.getIconWidth(), errorIcon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
errorIcon.paintIcon(null, errorImage.getGraphics(), 0, 0);
exceptionFrame.setIconImage(errorImage);
exceptionFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel contentPanel = new JPanel(new BorderLayout(5, 5));
contentPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPanel.add(new JLabel("An uncaught exception has occurred: "), BorderLayout.NORTH);
JTextArea stackTraceTextArea = new JTextArea(30, 80);
stackTraceTextArea.setEditable(false);
contentPanel.add(stackTraceScrollPane, BorderLayout.CENTER);
stackTraceTextArea.setCaretPosition(0); // Scroll to top
JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
JButton closeButton = new JButton(new AbstractAction("Close") {
  @Override
  public void actionPerformed(ActionEvent e) {
buttonPanel.add(closeButton);
JButton exitApplicationButton = new JButton(new AbstractAction("Exit application") {
  @Override
  public void actionPerformed(ActionEvent e) {
origin: wiztools/rest-client

jb_add.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
jb_addAndClose.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
jb_cancel.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
c.setLayout(new BorderLayout());
  JPanel jp = new JPanel(new BorderLayout());
    JPanel jp_west = new JPanel(new GridLayout(2, 1));
    jp_west.add(new JLabel(" Content type: "));
    jp_west.add(new JLabel(" Name: "));
    jp.add(jp_west, BorderLayout.WEST);
    JPanel jp_center = new JPanel(new GridLayout(2, 1));
    jp_center.add(jp_contentType.getComponent());
    jp_center.add(UIUtil.getFlowLayoutPanelLeftAligned(jtf_name));
  c.add(jp, BorderLayout.NORTH);
  c.add(jsp, BorderLayout.CENTER);
origin: wiztools/rest-client

c.setLayout(new BorderLayout());
JPanel jp_center = new JPanel();
jp_center.setBorder(BorderFactory.createTitledBorder("Run Test"));
jp_center.setLayout(new GridLayout(3, 1));
jp_center.add(jrb_archive);
JPanel jp_center_file = new JPanel();
jtf_archive.setEditable(false);
jp_center_file.add(jtf_archive);
jb_archive_browse.setMnemonic('b');
jb_archive_browse.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
jp_center.add(jp_center_file);
jp_center.add(jrb_last);
c.add(jp_center, BorderLayout.CENTER);
JPanel jp_south = new JPanel();
jp_south.setLayout(new FlowLayout(FlowLayout.RIGHT));
jb_next.setMnemonic('n');
getRootPane().setDefaultButton(jb_next);
jb_next.addActionListener(new ActionListener() {
jp_south.add(jb_cancel);
c.add(jp_south, BorderLayout.SOUTH);
origin: deathmarine/Luyten

protected JDialog createDialog(Component parent) {
  Frame frame = parent instanceof Frame ? (Frame) parent
      : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
  JDialog dialog = new JDialog(frame, ("Select Font"), true);
  Action okAction = new DialogOKAction(dialog);
  Action cancelAction = new DialogCancelAction(dialog);
  JButton okButton = new JButton(okAction);
  okButton.setFont(DEFAULT_FONT);
  JButton cancelButton = new JButton(cancelAction);
  cancelButton.setFont(DEFAULT_FONT);
  JPanel buttonsPanel = new JPanel();
  buttonsPanel.setLayout(new GridLayout(2, 1));
  buttonsPanel.add(okButton);
  buttonsPanel.add(cancelButton);
  buttonsPanel.setBorder(BorderFactory.createEmptyBorder(25, 0, 10, 10));
  ActionMap actionMap = buttonsPanel.getActionMap();
  actionMap.put(cancelAction.getValue(Action.DEFAULT), cancelAction);
  actionMap.put(okAction.getValue(Action.DEFAULT), okAction);
  InputMap inputMap = buttonsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), cancelAction.getValue(Action.DEFAULT));
  inputMap.put(KeyStroke.getKeyStroke("ENTER"), okAction.getValue(Action.DEFAULT));
  JPanel dialogEastPanel = new JPanel();
  dialogEastPanel.setLayout(new BorderLayout());
  dialogEastPanel.add(buttonsPanel, BorderLayout.NORTH);
  dialog.getContentPane().add(this, BorderLayout.CENTER);
  dialog.getContentPane().add(dialogEastPanel, BorderLayout.EAST);
  dialog.pack();
  dialog.setLocationRelativeTo(frame);
  return dialog;
}
origin: stackoverflow.com

 JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4,4,4,4));

for(int i=0 ; i<16 ; i++){
  JButton btn = new JButton(String.valueOf(i));
  btn.setPreferredSize(new Dimension(40, 40));
  panel.add(btn);
}
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
origin: stackoverflow.com

topPanel = new JPanel();         // our top component
bottomPanel = new JPanel();      // our bottom component
inputPanel = new JPanel();
textField = new JTextField();    // first the input field where the user can type his text
button = new JButton("send");    // and a button at the right, to send the text
getContentPane().setLayout(new GridLayout());  // the default GridLayout is like a grid with 1 column and 1 row,
getContentPane().add(splitPane);               // due to the GridLayout, our splitPane will now fill the whole window
inputPanel.add(button);           // and right the "send" button
pack();   // calling pack() at the end, will ensure that every layout and size we just defined gets applied before the stuff becomes visible
  @Override
  public void run(){
    new MyFrame().setVisible(true);
origin: stackoverflow.com

final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
plafComponents.add(plafChooser);
plafComponents.add(pack);
gui.add(plafComponents, BorderLayout.NORTH);
final JPanel labels = new JPanel(new GridLayout(0,2,3,3));
labels.setBorder(
  new TitledBorder("GridLayout(0,2,3,3)") );
JButton addNew = new JButton("Add Another Label");
dynamicLabels.add( addNew, BorderLayout.NORTH );
addNew.addActionListener( new ActionListener(){
gui.add( splitPane, BorderLayout.CENTER );
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
origin: stackoverflow.com

public Calculator() {
   window = new JFrame("Calculator");
   window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JPanel digitsPanel = new JPanel();
   digitsPanel.setLayout(new GridLayout(4, 3));
   for (int digit = 0; digit <=9; digit++) {
     JButton button = new JButton(Integer.toString(digit));
     button.addActionListener(this);
     digitsPanel.add(button);
   }
   window.add(digitsPanel);
   window.pack();
   window.setVisible(true);
 }
origin: checkstyle/checkstyle

final JButton openFileButton = new JButton(new FileSelectionAction());
openFileButton.setMnemonic(KeyEvent.VK_S);
openFileButton.setText("Open File");
modesLabel.setBorder(BorderFactory.createEmptyBorder(0, leftIndentation, 0, 0));
final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
buttonPanel.add(openFileButton);
buttonPanel.add(reloadFileButton);
origin: stackoverflow.com

JPanel p = new JPanel(new GridLayout(2,1));
header.setPreferredSize(new Dimension(100, HEADER_HEIGHT));
p.add(makeTitledPanel("Bad: JTableHeader#setPreferredSize(...)", new JScrollPane(table1)));
p.add(makeTitledPanel("Override getPreferredSize()", scroll));
panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
return panel;
JPanel p = new JPanel(new BorderLayout());
p.add(c);
p.setBorder(BorderFactory.createTitledBorder(title));
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TableHeaderHeightTest().makeUI());
f.setSize(320, 320);
f.setLocationRelativeTo(null);
f.setVisible(true);
origin: stackoverflow.com

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.add(f);
frame.add(p);
frame.pack();
frame.setVisible(true);
autoSuggestionPopUpWindow.setOpacity(opacity);
suggestionsPanel = new JPanel();
suggestionsPanel.setLayout(new GridLayout(0, 1));
suggestionsPanel.setBackground(popUpBackground);
textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released");
textField.getActionMap().put("Down released", new AbstractAction() {
  @Override
  public void actionPerformed(ActionEvent ae) {//focuses the first label on popwindow
origin: stackoverflow.com

JFrame frame = new JFrame("Layout Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(
        new GridLayout(0, 1, hGap, vGap));
contentPane.setBorder(
  BorderFactory.createEmptyBorder(hGap, vGap, hGap, vGap));
borderPanel = new JPanel(new BorderLayout(hGap, vGap));
borderPanel.setBorder(
  BorderFactory.createTitledBorder("BorderLayout"));
borderPanel.setOpaque(true);
  buttons[i] = new JButton(borderConstraints[i]);
  borderPanel.add(buttons[i], borderConstraints[i]);
contentPane.add(borderPanel);
flowPanel = new JPanel(new FlowLayout(
  buttons[i] = new JButton(Integer.toString(i));
  flowPanel.add(buttons[i]);
gridPanel = new JPanel(new GridLayout(2, 2, hGap, vGap));
  buttons[i] = new JButton(Integer.toString(i));
origin: stackoverflow.com

final JFrame frame = new JFrame("Text HIGHLIGHT");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createTitledBorder(
    BorderFactory.createEmptyBorder(5, 5, 5, 5), "Highlighter JTextArea"));
contentPane.add(scrollPane);
JButton remHighButton = new JButton("REMOVE HIGHLIGHT");
remHighButton.addActionListener(new ActionListener()
JButton button = new JButton("HIGHLIGHT TEXT");
button.addActionListener(new ActionListener()
frame.add(remHighButton, BorderLayout.PAGE_START);
frame.add(contentPane, BorderLayout.CENTER);
frame.add(button, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(
        BorderFactory.createLineBorder(Color.DARK_GRAY, 2), "COLOUR SELECTION"));
panel.setLayout(new GridLayout(0, 2, 5, 5));
origin: stackoverflow.com

private static final Dimension LAYERED_PANE_SIZE = new Dimension(WIDTH, HEIGHT);
private static final Dimension LABEL_SIZE = new Dimension(60, 40);
private GridLayout gridlayout = new GridLayout(GRID_ROWS, GRID_COLS, GAP, GAP);
private JPanel backingPanel = new JPanel(gridlayout);
private JPanel[][] panelGrid = new JPanel[GRID_ROWS][GRID_COLS];
private JLabel redLabel = new JLabel("Red", SwingConstants.CENTER);
  redLabel.setOpaque(true);
  redLabel.setBackground(Color.red.brighter().brighter());
  redLabel.setPreferredSize(LABEL_SIZE);
  panelGrid[4][3].add(redLabel);
  blueLabel.setOpaque(true);
  blueLabel.setBackground(Color.blue.brighter().brighter());
  blueLabel.setPreferredSize(LABEL_SIZE);
  panelGrid[1][1].add(blueLabel);
  backingPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
  JFrame frame = new JFrame("DragLabelOnLayeredPane");
  frame.getContentPane().add(new DragLabelOnLayeredPane());
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
java.awtGridLayout

Javadoc

The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle. For example, the following is an applet that lays out six buttons into three rows and two columns:

Note: The following code example includes classes that do not appear in this specification. Their inclusion is purely to serve as a demonstration.


 
import java.awt.*; 
import java.applet.Applet; 
public class ButtonGrid extends Applet { 
public void init() { 
setLayout(new GridLayout(3,2)); 
add(new Button("1")); 
add(new Button("2")); 
add(new Button("3")); 
add(new Button("4")); 
add(new Button("5")); 
add(new Button("6")); 
} 
} 

It produces the following output:

Most used methods

  • <init>
    Creates a grid layout with the specified number of rows and columns. All components in the layout ar
  • setHgap
    Sets the horizontal gap between components to the specified value.
  • setVgap
    Sets the vertical gap between components to the specified value.
  • setColumns
    Sets the number of columns in this layout to the specified value. Setting the number of columns has
  • setRows
    Sets the number of rows in this layout to the specified value.
  • getColumns
    Gets the number of columns in this layout.
  • getRows
    Gets the number of rows in this layout.
  • getHgap
    Gets the horizontal gap between components.
  • getVgap
    Gets the vertical gap between components.
  • layoutContainer
    Lays out the specified container using this layout. This method reshapes the components in the speci
  • add
  • addComponent
  • add,
  • addComponent,
  • addStyleName,
  • addView,
  • getComponent,
  • removeAllViews,
  • setBackgroundColor,
  • setColumnCount,
  • setColumnExpandRatio,
  • setComponentAlignment

Popular in Java

  • Creating JSON documents from java classes using gson
  • getSupportFragmentManager (FragmentActivity)
  • startActivity (Activity)
  • requestLocationUpdates (LocationManager)
  • Graphics2D (java.awt)
    This Graphics2D class extends the Graphics class to provide more sophisticated control overgraphics
  • DecimalFormat (java.text)
    A concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features desig
  • SimpleDateFormat (java.text)
    Formats and parses dates in a locale-sensitive manner. Formatting turns a Date into a String, and pa
  • Dictionary (java.util)
    Note: Do not use this class since it is obsolete. Please use the Map interface for new implementatio
  • Executor (java.util.concurrent)
    An object that executes submitted Runnable tasks. This interface provides a way of decoupling task s
  • LogFactory (org.apache.commons.logging)
    Factory for creating Log instances, with discovery and configuration features similar to that employ
  • Top 25 Plugins for Webstorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now