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 497515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:47:20+00:00 2026-05-13T05:47:20+00:00

I have a problem. I used Gridbaglayout in my JFrame. One of the component

  • 0

I have a problem. I used Gridbaglayout in my JFrame. One of the component is JPanel.

I wanted to draw a gridline as a background for my JPanel. e.g in the program below, it supposed to produce 3 vertical and 3 horizontal lines, however it only shows 2 vertical and 2 horizontal line. the last line was not shown.

Another problem was that, it seems that the size of the JPanel was bigger that what I have set. I noticed this by the length of the line which is shorter that the JPanel white background.

  public class drawLayout extends JComponent 
    {

 public Dimension getPreferredSize() { 
  return new Dimension(600, 600); 
 }

 public int getY() { 
  return 0; 
 } 

 public int getX() { 
   return 0; 
    }

    @Override public void paintComponent(Graphics g)
    {
     g.setPaint(Color.GRAY);

            for (int i = 0; i <= getSize().width; i += 300) 
            {
               g2.drawLine(i, 0, i, getSize().height);
            }

            for (int i = 0; i <= getSize().height; i += 300) 
            {
               g2.drawLine(0,i, getSize().width, i);
            }
    } 
}

EDIT:

http://www.freeimagehosting.net/image.php?1af16edc28.jpg

The first problem solved (the gridlines were shown on JPanel).
The other problem puzzled me. As you can see on the image attached, the size of the JPanel seems to be more than 600 when look at the length of the grid (marked as red box). How can I solve this so the gridline background look nice without any extra white space outside the gridline?

  • 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-13T05:47:20+00:00Added an answer on May 13, 2026 at 5:47 am

    If your JPanel’s size is 600, then its available coordinates only go from 0 to 599. The line at 600 isn’t going to be drawn.

    Also, any borders and/or insets would further reduce the available space.

    Update: I had some time, so I wrote your application. Hopefully you will find some useful hints.

    public class Jessy extends JFrame {
    
       private static final int DRAWING_SIZE = 600;
       private static final int SUBDIVISIONS = 2;
       private static final int SUBDIVISION_SIZE = DRAWING_SIZE / SUBDIVISIONS;
    
       public Jessy() {
          setSize(800, 800);
          setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.weightx = 1.0;
          gbc.weighty = 1.0;
          gbc.gridx = 0;
          gbc.gridy = 0;
          JLabel drawingBoard = new JLabel("Drawing Board");
          gbc.anchor = GridBagConstraints.SOUTH;
          drawingBoard.setFont(new Font("Serif", Font.BOLD, 28));
          add(drawingBoard, gbc);
          JPanel panel = new JPanel() {
             @Override public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.setPaint(Color.GRAY);
                for (int i = 1; i < SUBDIVISIONS; i++) {
                   int x = i * SUBDIVISION_SIZE;
                   g2.drawLine(x, 0, x, getSize().height);
                }
                for (int i = 1; i < SUBDIVISIONS; i++) {
                   int y = i * SUBDIVISION_SIZE;
                   g2.drawLine(0, y, getSize().width, y);
                }
             }          
          };
          panel.setPreferredSize(new Dimension(DRAWING_SIZE, DRAWING_SIZE));
          panel.setBackground(Color.WHITE);
          panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
          gbc.gridy++;
          gbc.anchor = GridBagConstraints.CENTER;
          add(panel, gbc);
          JButton saveDrawing = new JButton("SAVE DRAWING");
          gbc.gridy++;
          gbc.anchor = GridBagConstraints.NORTH;
          add(saveDrawing, gbc);
       }
       public static void main(String[] args) {
          (new Jessy()).setVisible(true);
       }
    }
    

    Some details:

    • I used a Border for the outside lines, this saves us a bit of trouble with “599”.
    • I assumed you wanted a subdivided grid on the inside so I added some plumbing for making that flexible and configurable.
    • I noticed your paintComponents() doesn’t call super.paintComponents(). It should!
    • I used what I think to be the minimum required coding to specify the gridbag constraints. Less coding = less bugs 🙂
    • I subclassed a JPanel (in accordance with what you wrote) rather than a JComponent (in accordance with what’s in your code). The difference seems to be that JComponent isn’t able to draw its background, so that ended up gray.
    • Perhaps most importantly, I measured my GUI with kruler: The drawing component is exactly 600 in size both ways! 🙂
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problem with my cfml website. I have used the below code
The problem: we have one application that has a portion which is used by
Greeting, I have problem stopping FLVPlaypack component when I navigate from one frame to
i used v s 2010 i have one problem..when i delete more then one
So here is my problem: I have used a number of interfaces in my
I have a problem with ROW_NUMBER() , if i used it with DISTINCT in
I have a problem when call applet method from javascript.. I used this function
I have a problem regarding the ksoap2. My problem is that the project used
I tweaked this script that I used from JqueryUi and I have a problem.
I have three tables being used for this problem: songs, blacklist, and whitelist. The

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.