Implementing absolute positioning for JPanels in java/netbeans JPanels applied through CardLayout!

Posted on 2011-10-13
Medium Priority
Last Modified: 2012-08-14
I have a few JPanels stacked under a mainPanel via CardLayout under Netbeans 7.  Now for some weird reason I can't get any of these JPanels to maintain a setPreferredSize of [600,400] and most annoyingly I can't get the associated components to be placed right on where they're supposed to be positioned on their associated JPanel.  Please note that I'm using Netbeans 7 properties menu to set these parameters. I even have set up the minimum and maximum size parameters.
Question by:snajalm
  • 4
  • 3
LVL 86

Expert Comment

ID: 36963226
You need to set a layout on the main panel that allows absolute positioning. As you probably know, the default is GroupLayout with the gui designer. It sounds like you attempted to do that, but perhaps have not done ...

Author Comment

ID: 36963304
Done what??!

I already have the CardLayout in place with allows me to browse through my panels via "next" and "previous" buttons.  How can I have both these layouts be implemented so that I can get my "CardLayout" to impose absolute positioning on all it's underlying JPanels?!  
LVL 86

Expert Comment

ID: 36964110
CardLayout is a special thing and doesn't affect in any way the layout of the components on any one 'card' you happen to be looking at. You need to set the correct layout on the main Container on the card
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Accepted Solution

CEHJ earned 1500 total points
ID: 36964546
Here's an example of a CardLayout with absolute positioning
import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

public class FB extends JFrame implements ActionListener {
    private JButton b;
    private JPanel cards;
    private String[] CARD_NAMES = { "One", "Two", "Three" };
    private int currentCardIndex = 0;

    public void actionPerformed(java.awt.event.ActionEvent e) {
	CardLayout cLayout = (CardLayout)cards.getLayout();
	System.out.printf("Will now show card %d (%s)\n", currentCardIndex, CARD_NAMES[currentCardIndex]);
        cLayout.show(cards, CARD_NAMES[currentCardIndex]);
	currentCardIndex %= CARD_NAMES.length;

    private void setGui() {
        try {
            setLocation(0, 100);

            Container cp = getContentPane();
	    cards = new JPanel();
	    cards.setLayout(new CardLayout());
	    cp.add(cards, BorderLayout.CENTER);
            JPanel bPanel = new JPanel();
            b = new JButton("Next");
            cp.add(bPanel, BorderLayout.SOUTH);
        } catch (Exception e) {

    private void addCards() {
        int x;
        int y;

        for (int i = 0; i < CARD_NAMES.length; i++) {
            x = y = (i * 30);

            String name = CARD_NAMES[i];
            JPanel p = new JPanel();

            JLabel lab = new JLabel(String.format("Card %s", name));
            lab.setBounds(x, y, 100, 25);
	    cards.add(p, name);

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        FB f = new FB();
                        f.setSize(200, 200);
        } catch (Exception e) {

Open in new window


Author Comment

ID: 36966422
Thanks CEHJ!  I have implemented the CardLayout using Netbeans 7 which works fine, however, the mainPanel with the CardLayout layout does not maintain the preferredSize parametes.  I just don't understand why a JPanel's size can't be set to an unResizable state.  If there is a parameter that can set this up, please let me know since this thing is driving me crazy!  

Author Closing Comment

ID: 37691397
LVL 86

Expert Comment

ID: 37691414

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 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