Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3599962
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:26:03+00:00 2026-05-18T20:26:03+00:00

Hello I’m an amateur trying to learn/improve my understanding of Java by writing a

  • 0

Hello
I’m an amateur trying to learn/improve my understanding of Java by writing a score card for archery. I ‘m trying to produce a GUI and so far have successfully produced on a JPanel a row of 18 labels of differing sizes and colours suitable for scoring a dozen.
I then tried to add five of these ‘labels panels’ to another panel to build up a grid and save having to create and add as many as 150 labels in some cases . No success so far as the original labels panels will not show up. All the panels are displayed on a JFrame

I’ve tried a number of different ways of getting the code to work, using the Java tutorial and googling the internet and studying similar problems on this site but I’m going round in circles. I must have missed something somewhere and hope that you may be able to help.

I’m using Java 6 and JGrasp v1.8.8_01 as an IDE

The following code for the labels panel has been cut down as much of it is repetitive.

import javax.swing.*;  
import java.awt.*;  
public class ArrowScoreLabels extends JPanel{
  public  JPanel createContentPane(){
    JPanel panelForLabels = new JPanel();
    panelForLabels.setLayout(null);
//Code creates 18 labels, sets the size, position, background colours, border and    
//font and adds the labels to the’panelForLabels
    JLabel scorelabel1;
     scorelabel1 = new JLabel("",JLabel.CENTER);
     scorelabel1.setBorder(BorderFactory.createLineBorder(Color.black)); 
     scorelabel1.setFont(new Font("Arial", Font.ITALIC, 26));  
     scorelabel1.setLocation(0, 0);//first value differs for each label
     scorelabel1.setSize(35, 35);
     scorelabel1.setOpaque(true);

        panelForLabels.add(scorelabel1);
        panelForLabels.setOpaque(true);
    return panelForLabels;
  }
}  

Running the following class shows the 18 labels on panel

import javax.swing.*;
import java.awt.*; 
public class TestArrowScoreLabels {

  private static void createAndShowArrowLabels() {
 //Create and set up the window.
  JFrame frame = new JFrame("To score one dozen");
 //Create and set up the content pane.
  ArrowScoreLabels asl = new ArrowScoreLabels();
  frame.setContentPane(asl.createContentPane()); 
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  frame.setSize(676, 73);
  frame.setVisible(true);
  }
//Main method to show the GUI/
  public static void main(String[] args) {
    SwingUtilities.invokeLater(
     new Runnable() {
      public void run() {
      createAndShowArrowLabels();
      }
    });
  }
}

The following code for the second panel is similar, compiles but only shows the second green JPanel and not the panel with the labels.

import javax.swing.*;
import java.awt.*;


public class FiveDozenScorePanel{
 public JPanel createContentPane(){
  //A bottom JPanel on which to place five dozenpanels.
  JPanel fivedozenpanel = new JPanel();
  fivedozenpanel.setLayout(null); //requires absolute spacing
  fivedozenpanel.setSize(676,185);  
  fivedozenpanel.setBackground(Color.green);
  //Label panels for five dozen
  ArrowScoreLabels dozenscorepanel1, dozenscorepanel2,
         dozenscorepanel3,dozenscorepanel4,dozenscorepanel5;   
  //Create the 5 dozenscorelabels.
  dozenscorepanel1 = new ArrowScoreLabels();         
  dozenscorepanel1.setLocation(5,5);//y value changes for each panel

  fivedozenpanel.add(dozenscorepanel1);//plus the other 4
  fivedozenpanel.setOpaque(true);
  return fivedozenpanel;
}

private static void createAndShowDozenPanels() {
 JFrame frame = new JFrame("To score five dozen");
  FiveDozenScorePanel fdsp = new FiveDozenScorePanel();
  frame.setContentPane(fdsp.createContentPane());   
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  //Display the window  
  frame.setSize(700, 233);
  frame.setVisible(true);
 }   

public static void main(String[] args) {
 SwingUtilities.invokeLater(
  new Runnable() {
   public void run() {                     
     createAndShowDozenPanels();
   }
  });               
 }
}

I’ve also tried frame.getContentPane().add(fdsp); – frame.pack(); and read so much about paint methods that I’m totally confused.
I can get the ArrowScoreLabels image to appear directly onto a JFrame rather than a JPanel but only one of them and not five.

I would appreciate being pointed in the right direction. Thankyou for your time.

Update – 14th Dec 2010
I have managed to display the panelForLabels Jpanel on another Jpanel on a JFrame. This was done by adding the following code to the class ArrowScoreLabels. The original createContentPane() method was renamed createRowOne(). The panelForLabels was coloured red and the fivedozen panel yellow to ascertain which was showing. However I was only able to persuade the programme to display one row of labels despite a great deal of experimentation and research.

public  static JPanel createContentPane(){
   //Bottom panelto hold rows of labels
     JPanel fivedozenscorepanel = new JPanel();      
     fivedozenscorepanel.setLayout(null);//requires absolute spacing
     fivedozenscorepanel.setSize(660,180);
     fivedozenscorepanel.setBackground(Color.yellow);
     fivedozenscorepanel.add(createRowOne());

     fivedozenscorepanel.setOpaque(true);
     return fivedozenscorepanel;
  }

The only way I displayed the 5 rows of 18 labels was to create all 90 in the ArrowScoreLabels class and then add them to one JPanel using absolute spacing and then to a JFrame.
I’ve taken note of pstantons advice – thankyou for that – and I’m looking into using the MigLayout Manager.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:26:04+00:00Added an answer on May 18, 2026 at 8:26 pm

    simple answer: use a layout manager. don’t use absolute positioning.

    just comment out all of your calls to setLocation and setLayout and swing will use the default FlowLayout.

    for more control over the display, use a different layout manager.

    also, if you use multiple panels you will have trouble aligning things in different panels unless they contain the same number of components which are exactly the same size so consider using one panel for all of the labels.

    you can achieve just about any layout you need using MigLayout.

    EDIT: in your example, there’s no need for ArrowScoreLabels need to extend JPanel since you are doing the work in createContentPane to construct a separate JPanel. later in your code you call new ArrowScoreLabels() wich will just return a blank JPanel, instead you need to call new ArrowScoreLabels().createContentPane()

    if you want ArrowScoreLabels to extend JPanel, implement public ArrowScoreLabels() ie the constructor instead of createContentPane.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone i am trying to format the input number range with php number_format
hello friends i am trying to get a list of user id and want
Hello Guys I am trying to figure out why i am gettings this error
Hello All I am trying to flatten a list in Ocaml. I am a
Hello There, I am new to phonegap.I am trying to record a audio clip
. Hello, I am trying to add a UIButton and other items to my
. Hello, I am trying to make a Custom Segue for my storyboard. Now
Hello there I am new to php and want to learn to write reusable
Hello i am trying to check to see if there is a row in
Hello i'm having some trouble in displaying multiple pushpins on maps. So far i've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.