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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:11:32+00:00 2026-05-26T15:11:32+00:00

I’m having trouble with a contentPane. Here’s the code in question: public void graph()

  • 0

I’m having trouble with a contentPane. Here’s the code in question:

public void graph() {
    JFrame frame = new JFrame("Graph");
    Graph[] graphs = new Graph[timeSlices];
    int k = 0;

    for (TreeMap<MyPoint, BigDecimal> prevU : prevUs) {
        graphs[k] = new Graph(prevU);
        k++;
    }

    // The KeyList handles switching between graphs.
    frame.addKeyListener(new KeyList(frame.getContentPane(), graphs));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(810, 500);
    frame.setVisible(true);
}

private class KeyList extends KeyAdapter {
    private Container contentPane;
    private Graph[] graphs;
    private int index;

    public KeyList(Container contentPane, Graph[] graphs) {
        this.contentPane = contentPane;
        this.graphs = graphs;
        this.index = 0;

        this.contentPane.add(this.graphs[0]);
    }

    public void keyPressed(KeyEvent e) {
        // Go back a time step
        if (e.getKeyCode() == KeyEvent.VK_LEFT && index > 0) {
            contentPane.remove(graphs[index]);
            contentPane.add(graphs[--index]);
            contentPane.validate();
            System.out.println(index);
        }
        // Go forward a time step
        else if (e.getKeyCode() == KeyEvent.VK_RIGHT && index < timeSlices - 1) {
            contentPane.remove(graphs[index]);
            contentPane.add(graphs[++index]);
            contentPane.validate();
            System.out.println(index);
        }
        // Exit if Esc is hit
        else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
            System.exit(0);
    }
}

Graph is just a Component, easy peasy. When I hit the right arrow, I want to replace the currently displayed Graph with the next one in the array, and when I hit the left arrow, I want to replace the Graph with the previous one in the array.

The weird thing is that when I hit right, it works just fine. However, when I hit left, the Graph doesn’t change. The index changes, thus I know the code is being reached, but the GUI doesn’t change.

Now get ready for this. When I comment out the right key’s validate line, the left one will work about half the time. What is going on there? Here’s the rest of the code if you want to run and see for your self (just one file) : http://pastebin.com/qWxWrypK. The starting paramemters I’m currently using are T=1, dt=.01, L=1, h=.05.

I was looking into it, I thought it might be because the contentPane of a JFrame is really a JPanel, but that line of thinking didn’t get anywhere…

Thanks for any help

Edit:

So I’m still working with it. Here’s another weird thing. If I set the index in the KeyList class to timeSlices-1 (basically getting the last Graph in graphs array), and I hit left, it works! But, now the right doesn’t! Something weird has to be going on with the array or something because the index changes just fine. Hmm.

Edit:

Something’s going on with the array. For some reason, the Graph can only be used once. Perhaps it’s being destroyed on removal? Or something like that…

  • 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-26T15:11:32+00:00Added an answer on May 26, 2026 at 3:11 pm

    Instead of trying to remove/add panels to a container use a CardLayout which was designed for this purpose.

    Also, don’t use KeyListeners. Instead you should be using Key Bindings. Then you simply bind the next/previous keys to the next/previous methods of the card layout.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I

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.