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

  • Home
  • SEARCH
  • 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 1072285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:50:07+00:00 2026-05-16T20:50:07+00:00

There is a panel in a JFrame called a WatchPanel extends JPanel which uses

  • 0

There is a panel in a JFrame called a WatchPanel extends JPanel which uses a GroupLayout and contains a WatchListPanel extends JPanel. The WatchListPanel uses a null LayoutManager and contains several instances of WatchItemPanel extends JPanel. The WatchItemPanel lays out its contents with a GridBagLayout.

When I load up the frame and instantiate the WatchPanel, I pre-load the WatchListPanel with three WatchItemPanel instances with data. These work fine. My implementation allows these to be reordered by dragging and dropping (hence the null layout), and that works as well. On the WatchPanel is a button to add a new watch, which pops up a new panel in its own JFrame where a watch can be added. When the “Save” button is pressed on this new panel, it adds the new watch to the WatchPanel, which trickles down into the WatchListPanel and closes the JFrame for adding a new watch.

The problem is that when this new WatchItemPanel is added, it doesn’t get painted unless I resize the JFrame containing the WatchPanel. I can interact with it through drag-n-drop. I can even drag that specific panel – but it’s blank. The moment I resize the WatchPanel, it appears and is painted correctly.

Now, I have overridden the setBounds method on WatchListPanel to call setSize and repaint on each of the WatchItemPanel instances in the list, but I am calling setBounds and repaint on the new WatchItemPanel already as I add it. I have tried adding repaint and invalidate calls in every conceivable place thinking maybe I missed something, but no luck. I absolutely have added the WatchItemPanel to the WatchListPanel and called setBounds, setVisible(true), and repaint on the new panel.

Here are the addWatch and updatePositions methods on the WatchListPanel:

public void addWatch(Watch watch) {
    WatchItemPanel watchPanel = new WatchItemPanel(watch);
    synchronized (this.watches) {
        this.watches.add(watch);
    }
    this.watchPanels.put(watch, watchPanel);
    this.add(watchPanel);
    watchPanel.setOpaque(false);
    watchPanel.setForeground(this.getForeground());
    watchPanel.setBackground(this.getBackground());
    watchPanel.setVisible(true);
    watchPanel.setSize(this.getWidth(), WatchItemPanel.PANEL_HEIGHT);
    for (MouseListener l: this.mouseListeners)
        watchPanel.addMouseListener(l);
    for (MouseMotionListener l: this.mouseMotionListeners)
        watchPanel.addMouseMotionListener(l);
    this.updatePositions();
}

private void updatePositions() {
    int count = 0;
    synchronized (this.watches) {
        for (Watch watch: this.watches) {
            if ((this.selectedWatchPanel != null) && (count == this.selectedWatchPanelPosition))
                count++;
            WatchItemPanel panel = this.watchPanels.get(watch);
            if (panel == this.selectedWatchPanel)
                continue;
            panel.setBounds(0, count * WatchItemPanel.PANEL_HEIGHT, this.getWidth(), WatchItemPanel.PANEL_HEIGHT);
            count++;
        }
    }
    if (this.selectedWatchPanel != null)
        this.selectedWatchPanel.setBounds(0, (this.selectedWatchPanelPosition * WatchItemPanel.PANEL_HEIGHT) + this.selectedWatchPanelDragOffset, this.getWidth(), WatchItemPanel.PANEL_HEIGHT);
    this.repaint();
}

And here are the setBounds methods on WatchListPanel:

@Override
public void setBounds(Rectangle r) {
    super.setBounds(r);
    for (WatchItemPanel panel: this.watchPanels.values()) {
        panel.setSize(r.width, WatchItemPanel.PANEL_HEIGHT);
        panel.repaint();
    }
}

@Override
public void setBounds(int x, int y, int width, int height) {
    super.setBounds(x, y, width, height);
    for (WatchItemPanel panel: this.watchPanels.values()) {
        panel.setSize(width, WatchItemPanel.PANEL_HEIGHT);
        panel.repaint();
    }
}

Another piece of information here – all four panels are being painted. I overrode paintComponent in the WatchItemPanel and added a System.out.println and got this result:

WatchItemPanel[,0,66,366x22,invalid,layout=java.awt.GridBagLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777219,maximumSize=,minimumSize=,preferredSize=]
WatchItemPanel[,0,44,366x22,layout=java.awt.GridBagLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777219,maximumSize=,minimumSize=,preferredSize=]
WatchItemPanel[,0,22,366x22,layout=java.awt.GridBagLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777219,maximumSize=,minimumSize=,preferredSize=]
WatchItemPanel[,0,0,366x22,layout=java.awt.GridBagLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777219,maximumSize=,minimumSize=,preferredSize=]

What’s that extra “invalid” column there in the panel? When I resize the WatchPanel the “invalid” goes away. It looks like an extra column, though. There’s no corresponding non-invalid value on the other lines. (this is the default JPanel.toString() output with the package name removed.

  • 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-16T20:50:08+00:00Added an answer on May 16, 2026 at 8:50 pm

    After adding the component, call this.revalidate(). This causes the container to be marked to re-layout its components. Once this line of code is added, it works perfectly.

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

Sidebar

Related Questions

I have a JFrame which contains a JApplet. There are shortcut keys that I
I have made a JFrame and inside that frame, there is panel on which
I have a JFrame which contains a JPanel . The frame can be resized
Is there any library which provide a text panel with basic formatting features. Such
I have a JFrame with an associated JPanel which fill the screen, both having
so I have this jFrame with a Panel. Inside that panel there are two
I have a JPanel with a GridLayout(1,0) set to a JFrame Borderlayout.SOUTH , there
How can I get the JFrame in which a JPanel is living? My current
I Have two files. One extends JFrame, and another Extends JPanel. Whenever I change
I have a JFrame panel. It opens several times. However, I want the previous

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.