0

First time working with Java swing and I'm not understanding where I'm going wrong. I'm creating a JFrame, and then creating two JPanels to add to it. Each JPanel has a few JLabels added to them. The JFrame and JPanels are specified as FlowLayouts, which (as I understand it) means that things are just displayed in the order they are added and added to the next line if there isn't room for them on the current line.

Here's my problem: If I run my current code and only add my first panel (infoPanel), I get a correct result that looks like this: this.

But, if I try to add both panels, the first one gets overridden and the JLabels of the first are not fully displayed (There should be much more then "Na..." on each, this is only the first part of a multi-line string starting with "Name". I specify the minimum size as 100x100, and yet they are still only a few characters. I've tried making them 400x400 and other sizes, but nothing changes. It looks like this:

My questions are: (1) Am I doing something with my panels incorrectly? Should the JFrame with a FlowLayout not be able to add both panels without overwriting the first one that was added? (2) Why aren't my full JLabels displaying? I don't understand where I'm going wrong, as they're specified the exact same as the JLabels in my infoPanel JPanel and not appearing the same...

Any help is appreciated, my code is below:

private void createView()
            {
                    // Create the frame 
                    frame = new JFrame("Shared Resources ");
                    frame.setSize(850, 750);
                    frame.setResizable(true);
                    frame.setLocation(0,0);;
                    frame.setVisible(true);

                    // Set the content
                    Container content = frame.getContentPane();
                    content.setBackground(Color.WHITE);

                    // Create our panels
                    JPanel infoPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
                    JPanel playerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 

                    // Empty border template
                    Border emptyBorder = BorderFactory.createEmptyBorder();



                    // Shared resources information
                    // Palace Cards
                    palaceCards = new JLabel("Player Palace Cards");
                    /* TODO */
                    palaceCards.setHorizontalTextPosition(SwingConstants.CENTER);
                    palaceCards.setVerticalTextPosition(SwingConstants.BOTTOM);
                    palaceCards.setVerticalAlignment(SwingConstants.BOTTOM);
                    palaceCards.setBorder(emptyBorder);
                    palaceCards.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    palaceCards.setPreferredSize(new Dimension(350,25));
                    palaceCards.setMinimumSize(palaceCards.getPreferredSize());
                    infoPanel.add(palaceCards);

                    festivalCards = new JLabel("Festival Cards");

                    festivalCards.setBorder(emptyBorder);

                    festivalCards.setPreferredSize(new Dimension(350,25));
                    festivalCards.setMinimumSize(festivalCards.getPreferredSize());
                    festivalCards.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    infoPanel.add(festivalCards);

                    numThreeBlocks = newJLabel("Three Blocks:    ");
                    numThreeBlocks.setText("");
                    numThreeBlocks.setPreferredSize(new Dimension(350,25));
                    numThreeBlocks.setHorizontalTextPosition(SwingConstants.CENTER);
                    numThreeBlocks.setVerticalTextPosition(SwingConstants.BOTTOM);
                    numThreeBlocks.setVerticalAlignment(SwingConstants.BOTTOM);
                    numThreeBlocks.setMinimumSize(numThreeBlocks.getPreferredSize());
                    numThreeBlocks.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    infoPanel.add(numThreeBlocks);

                    numIrrigationTiles = newJLabel("Irrigation Tiles");
                    numIrrigationTiles.setPreferredSize(new Dimension(350,25));
                    numIrrigationTiles.setMinimumSize(numIrrigationTiles.getPreferredSize());
                    numIrrigationTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    infoPanel.add(numIrrigationTiles);

                    numTwoPalaceTiles = newJLabel("Two Palace Tiles: ");
                    numTwoPalaceTiles.setPreferredSize(new Dimension(350,25));
                    numTwoPalaceTiles.setMinimumSize(numTwoPalaceTiles.getPreferredSize());
                    numTwoPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    infoPanel.add(numTwoPalaceTiles);

                    numFourPalaceTiles = newJLabel("Four Palace Tiles: ");
                    numFourPalaceTiles.setPreferredSize(new Dimension(350,25));
                    numFourPalaceTiles.setMinimumSize(numFourPalaceTiles.getPreferredSize());
                    numFourPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    infoPanel.add(numFourPalaceTiles);

                    numSixPalaceTiles = newJLabel("Six Palace Tiles: ");
                    numSixPalaceTiles.setPreferredSize(new Dimension(350,25));
                    numSixPalaceTiles.setMinimumSize(numSixPalaceTiles.getPreferredSize());
                    numSixPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    infoPanel.add(numSixPalaceTiles);

                    numEightPalaceTiles = newJLabel("Eight Palace Tiles: ");
                    numEightPalaceTiles.setPreferredSize(new Dimension(350,25));
                    numEightPalaceTiles.setMinimumSize(numEightPalaceTiles.getPreferredSize());
                    numEightPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    infoPanel.add(numEightPalaceTiles);

                    numTenPalaceTiles = newJLabel("Ten Palace Tiles: ");
                    numTenPalaceTiles.setPreferredSize(new Dimension(350,25));
                    numTenPalaceTiles.setMinimumSize(numTenPalaceTiles.getPreferredSize());
                    numTenPalaceTiles.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    infoPanel.add(numTenPalaceTiles);

                    player1 = newJLabel("Player 1");
                    player1.setMinimumSize(new Dimension(400,400));
                    player1.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    playerPanel.add(player1);

                    player2 = newJLabel("PLayer 2");
                    player2.setMinimumSize(new Dimension(400,400));
                    player2.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    playerPanel.add(player2);

                    player3 = newJLabel("Player 3");
                    player3.setMinimumSize(new Dimension(400,400));
                    player3.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    playerPanel.add(player3);

                    player4 = newJLabel("Player 4");
                    player4.setMinimumSize(new Dimension(100,100));
                    player4.setFont(new Font("Charlemagne Std", Font.BOLD, 14));
                    playerPanel.add(player4);

                    // Add panels           
                    content.add(infoPanel);
                    content.add(playerPanel);
            }
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Ladybro
  • 756
  • 2
  • 9
  • 30
  • 2
    1) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 2) Call `pack()` rather than `setSize(..)` for frames. 3) Call `frame.setVisible(true)` last. 4) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Apr 21 '14 at 01:38
  • which editor u are using?? – Kishan Bheemajiyani Apr 21 '14 at 05:03

3 Answers3

3

Should the JFrame with a FlowLayout not be able to add both panels without overwriting the first one that was added?

Yes, but the problem is that a JFrame uses a BorderLayout (not a FlowLayout) as the default layout manager. If you don't specify a constraint the component gets added to the CENTER but you can only display one component in the CENTER so only the last component is displayed. So if you want to add the two panels to the frame then you should use something like:

content.add(infoPanel, BorderLayout.CENTER);
content.add(playerPanel, BorderLayout.SOUTH);

FlowLayouts, which (as I understand it) means that things are just displayed in the order they are added and added to the next line if there isn't room for them on the current line.

Basically correct, but for a FlowLayout the preferred size is calculated as a single row of components. The "wrapped" components will only be visible if there is extra space available to the panel. To demonstrate this change the above code to use:

//content.add(infoPanel, BorderLayout.CENTER);
content.add(infoPanel, BorderLayout.NORTH);

and you will not see the wrapped components. See Wrap Layout for more information and a solution to this problem.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

It seems like you're trying to add 2 JPanels with FlowLayout.CENTER so they WILL override each other.

If I understand correctly, You should add another panel like:

JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
mainPanel.add(infoPanel);
mainPanel.add(playerPanel);

content.add(mainPanel);

Hope I understood your issue correctly.

Rock_Artist
  • 555
  • 8
  • 14
0

The problem is that you add the two panels to the same place: the default JFrame's layout is BorderLayout, and BorderLayout's add(panel) defaults to add(panel, BorderLayout.CENTER).

So either specify the position explicitly, say:

content.add(infoPanel, BorderLayout.CENTER);
content.add(playerPanel, BorderLayout.SOUTH);

Or use a different layout with the frame:

frame.setLayout(new FlowLayout());

Also you

  • should make the frame visible after you add all components.
  • You might want to add frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

However, I would highly recommend using a visual gui designer such as the one in netbeans. (Unless you need to build the gui dynamically)

Jirka
  • 4,184
  • 30
  • 40
  • I would recommend you don't use a visual designer. You spend time learning an IDE instead of learning Swing. – camickr Apr 22 '14 at 16:10