JPanel doesn't update no matter what I try


Drew Wood

I'm making a small program that shows teams/players/then their twitter feeds, tweets, etc. My problem doesn't seem to be related to this, it's just that my jPanel is not updating. I'm so far in my project that I keep throwing random code snippets in methods to get it done, so once I click on something, the "left" jpanel is either replaced with jbuttons or jbuttons are put in it, I tried before. Here's the code snippet I've been trying to get to work, just not doing what I expected.

public void mousePressed(MouseEvent e) {        
    System.out.println("You clicked on " + ap.getTeams()  [addPlayers.OPTIC].getTeamName());
        for(int i = 0; i<4; i++){
            //JPanel temp = ap.makePanel(ap.getTeams()[ap.OPTIC].getPlayers().get(i).getTwitterScreenName());
            //temp.setBounds(0,(i*125),   450,125);
            //left.add(temp);

            JButton b = new JButton("Test");
            b.setBounds(30,30,30,30);
            left.add(b);
            left.revalidate();
            add(left);
            add(b);
            right.add(b);
            left.setVisible(false);
            left.setVisible(true);

            System.out.println(i);
        }
    }   
}

The commented out code is my main code, but I'm just using jbuttons for testing. As you can see, I just keep adding random bits, hopefully of some use. I've also tried calling it later, but sadly, it didn't work either. Not sure why jpanels aren't updating, but any feedback would be great, thanks.

Hovercraft full of eels
  1. Avoid null layouts and setBounds, as this results in a very inflexible GUI, and while they may look good on one platform, they will look bad on most other platforms or screen resolutions, and are hard to update and maintain. Instead, you will need to learn and learn layout managers, then nest JPanels, each using its own layout manager to create pleasing and complex GUIs that look good on all OSes.
  2. The best way to swap JPanels or any components is to use CardLayout .
  3. If you are still stuck, please post a minimal example program . This is the best way for us to actually see and experience your problem.
  4. Don't add components to multiple containers like using bJButton variables . You're actually adding it to three containers.
  5. You seem to be trying to add four JButtons to the container so that they have the same bounds, one just above the other? Sorry, but the more I look at your code, the trickier it gets. Again, stop this nonsense, learn about layout managers and use them in the Swing tutorial.
  6. "and i keep throwing random code segments in my method to make it so ..."- Throwing stuff on the wall to see what isn't a great inspiration for creating programs. Start with the knowledge base - here is the Swing tutorial and the Java API, then plan the program structure, then create the code.

Edit
you asked:

.....I'm asking what is the problem with the whole question not being updated?

If you don't use CardLayout and change the components manually, you need to call revalidate()sum on the container after the change repaint(), the container should be updated with its new components. If this doesn't solve the problem, there may be a problem in your code that isn't shown to us, and you'll create and post a minimal example program again . Again, this is the best way for us to really understand and experience your problem.

Related


JPanel doesn't update no matter what I try

Drew Wood I'm making an applet that shows teams/players/then their twitter feeds, tweets, etc. My problem doesn't seem to be related to this, it's just that my jPanel is not updating. I'm so far in my project that I keep throwing random snippets of code in met

JPanel doesn't update no matter what I try

Drew Wood I'm making a small program that shows teams/players/then their twitter feeds, tweets, etc. My problem doesn't seem to be related to this, it's just that my jPanel is not updating. I'm so far in my project that I keep throwing random code snippets in

UpButton doesn't work no matter what I try

Alex Coster In my app ( minSdkVersion15) I have a Toolbarinstead of ActionBarand NavigationDrawerto switch between fragments. Some TabBarfragments have subfragments inside. These sub-fragments are ListViews and its onItemClickListenertriggers DetailFragment. I

UIScrollView doesn't scroll no matter what I try

mosusi I have a screen where the main view has a vertical placement UIScrollViewwhich contains one UIViewin turn containing UIView3 s, I have set the constraints accordingly, tried to set contentSizeboth viewDidLoadand viewDidLayoutSubviews, to no avail. I cre

@Font-face doesn't work no matter what I try

Jack Wright Yep...this has been asked a lot here, I'm a rookie web developer doing some small experiments on my own time. I'm trying to use the CSS @font-face property but don't know why it doesn't work. I simply can't do anything to get it to work. And I've b

UpButton doesn't work no matter what I try

Alex Coster In my app ( minSdkVersion15) I have a Toolbarinstead of ActionBarand NavigationDrawerto switch between fragments. Some TabBarfragments have subfragments inside. These sub-fragments are ListViews and its onItemClickListenertriggers DetailFragment. I

Ng-options doesn't work, no matter what I try

Sebas this is my choice <select ng-options="country.country for country in countries" formControlName="country"></select></label> Here is the component typescript: import { Component } from '@angular/core'; import { AuthService } from '../../services/auth

No matter what I try, the require code doesn't work

User 13880771 I'm trying to break up my Discord bot into different files to keep everything neat. I created a test file and tested it and it either fails to load or it doesn't at all. Please let me know if there is anything I should add/fix. Required code: mod

Cypress, no matter what I try, the if statement doesn't work

Sam 2142 I'm trying to do a simple test in Cypress to see if a field is read-only, but no matter what I try I can't get the if statement to work, I checked the cypress documentation and went through tons of different types of examples, I don't usually have pro

UpButton doesn't work no matter what I try

Alex Coster In my app ( minSdkVersion15) I have a Toolbarinstead of ActionBarand NavigationDrawerto switch between fragments. Some TabBarfragments have subfragments inside. These sub-fragments are ListViews and its onItemClickListenertriggers DetailFragment. I

Why doesn't my button move up no matter what I try?

sp92 I tried to move my back button up but no matter what I do it doesn't work. E.g If I do margin-bottom: 29%;then Button Oneand Button Twomove down, the backbutton stays put and doesn't move. If I do that margin-top: 29%;, then move down Button Onewith the b

No matter what I try I can't find the CsvReader

username So I'm trying to get the following code " https://github.com/jintrone/TEVA " to work , the only problem is that I'm new to Java. Initially, I got a compile error "it.uniroma1.dis.wsngroup.gexf4j.core does not exist", which I resolved by downloading th