开发者

Java: GridLayout calls layoutContainer before resize finished

开发者 https://www.devze.com 2023-02-09 14:18 出处:网络
I have a simple gui with container using the GridLayout Manager. I added two components which inherit from JComponent and paint some stuff on screen using paintComponent.

I have a simple gui with container using the GridLayout Manager. I added two components which inherit from JComponent and paint some stuff on screen using paintComponent.

Now i added the componentListener to resize the gui using the GridLayout Manager. After resize the two components are still small, so not resized.

I checked this by creating a simple copy of GridLayout printing the size the methode layoutContainer is getting from the parent container to see if the parent has the new size (after resize event). It prints still a small size with a small change, but not the correct one.

I print the size in layoutContainer and i print the size of my parent gui every second using a simple Timer.

I realized that before the Timer prints the correct size (1600x1099), the layoutContainer in my GridLayout Manager gets called with the old size.

I thought the GridLayout is always auto resizing all of its components according to the rows and cols configuration. But it seems it doesnt, the layoutContainer method just gets called too early.

Is there any way to get this right using the GridLayout Manager or do i have to resize my components on my own?

How can i check when and what is calling the layoutContainer method in the LayoutManager?

Heres the code of one module which gets resized correctly, but its subcomponent which are subclasses are not resized correctly (hope its not too much code for you):

Info: I wrote all needed classes for this module in one java file, so i have everything i need in one java file.

public class Main extends Module {

  // ############### MAIN PAGE COMPONENT ##############
  public class MainPage extends ModulePageContainer {
    // ############### CLOCK COMPONENT ##############
    public class ClockComponent extends JComponent {
      private Date              currentDateTime;
      private SimpleDateFormat  dateFormat;

      public ClockComponent() {
        this.dateFormat = new SimpleDateFormat( "HH:mm");
      }

      @Override
      protected void paintComponent( Graphics g) {
        super.paintComponent( g);
        //...doing some paint stuff here...
      }

      public void setDateTime( Date dateTime) {
        this.currentDateTime = dateTime;
      }
    }
    // ############### CLOCK COMPONENT END ##############

    // ############### MAIN INFO COMPONENT ##############
    public class MainInfoComponent extends JComponent {   
      public MainInfoComponent() {
        this.setLayout( null);
      }

      @Override
      protected void paintComponent( Graphics g) {
        super.paintComponent( g);
        // ... just empty subcomponent ...
      }
    }

    // ############### MAIN INFO COMPONENT END ##############

    开发者_如何学Goprivate ClockComponent    clock;
    private MainInfoComponent mainInfo;

    public MainPage( Module parent) {
      super( parent);
      this.clock = new ClockComponent();
      this.mainInfo = new MainInfoComponent();
      this.setLayout( new MyGridLayout( 2, 1));
      this.add( this.clock);
      this.add( this.mainInfo);
    }

    public void clockTick( Date date) {
      this.clock.setDateTime( date);
      this.repaint();
    }
  }

  // ############### MAIN PAGE COMPONENT END ##############    
  public Main( String name) {
    super( name);
  }

  @Override
  public void init() {
    // every module has pages, this module has only one page called 'main'
    MainPage mainPage = new MainPage( this);
    mainPage.setName( "main");
    this.addPage( mainPage);
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消