JButton in Swing

Posted on 2007-10-19
Last Modified: 2013-11-23
I am using GridBagLayout for rendering the 6 Buttons in
B1 B2
B3 B4
B5 B6
But it screwed out .I can't even see some buttons.
What should be the numbering for it.

private final JComponent getNewMonitorButtonsPane() {
    BasicCompositePane wAnswer = new BasicCompositePane(ChannelMode.WRITE);
    wAnswer.setLayout(new GridBagLayout());
    for (int i=0; i<getMonitorButtons().length; i++) {
      wAnswer.add(getMonitorButtons()[i], ViewUtilities.getGridBagConstraints(
        i, i, 5+i, 5, .50, 0.5, GridBagConstraints.NORTHWEST,
        GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
return wAnswer;
Question by:Anj78
    1 Comment
    LVL 13

    Accepted Solution

    Just guessing here what BasicCompositePane and ViewUtilities.getGridBagConstraints actually do, but try this:
          wAnswer.add(getMonitorButtons()[i], ViewUtilities.getGridBagConstraints(
                i % 2, i / 2, 1, 1, .50, 0.5, GridBagConstraints.NORTHWEST,
                GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

    The first to parameters could be the column and row for the component. So, using the given math it will add the buttons alternating in column 0 and 1 and place each next 2 buttons on a new row.

    The 3rd and 4th parameter tell us about the gridwidth and height. In your case they should simply be 1 as every button will ge exact the same amount of space. I left all other parameters as you provided them, because they wont do much in the given example.

    Check out this for more info on how to work with GridBagLayout

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now