java - Swing: How do i create an east hugging component that will not move over western components -


i've tried several ways render correctly. want logo area hug east window resized, not overlap western components. following code not hug east, think makes sense because the logoarea box not know should take rest of horizontal space.

if add logoarea directly 'area' hugs east, overlap western components if window shrinks much. ideas?

 box box = box.createhorizontalbox();  box.add( main );  for( jcomponent item : items ) //western stuff  {     box.add( box.createhorizontalstrut( 8 ) );     box.add( item );  }   //eastern stuff  box logoarea= box.createhorizontalbox();  logoarea.add( new jlabel( laf.icon.png( "companylogo" ) ), borderlayout.east );   box.add( box.createhorizontalstrut( 8 ) );  box.add( logoarea, borderlayout.east );   jpanel area = new jpanel( new borderlayout() );  area.setborder( borderfactory.createemptyborder( 2, 2, 2, 2 ) );  area.add( box, borderlayout.west );  return area;  //dashboard built 

edit

to answer @nitin i'd move left until reaches western components, , stop moving , disapear right side.

i don't know standart layout act want, not hard create one. check small example (cross-shaped paintings displays content , logo bounds):

public static void main ( string[] args ) {     jframe frame = new jframe ();      logolayout layout = new logolayout ();     frame.setlayout ( layout );      frame.add ( new jlabel ( "label same preferred size text length" )     {         protected void paintcomponent ( graphics g )         {             super.paintcomponent ( g );              g.setcolor ( color.black );             g.drawline ( 0, 0, getwidth (), getheight () );             g.drawline ( getwidth (), 0, 0, getheight () );         }     }, layout.content );      frame.add ( new jcomponent ()     {         protected void paintcomponent ( graphics g )         {             g.setcolor ( color.black );             g.drawline ( 0, 0, getwidth (), getheight () );             g.drawline ( getwidth (), 0, 0, getheight () );         }          public dimension getpreferredsize ()         {             return new dimension ( 100, 100 );         }     }, layout.logo );      frame.setsize ( 700, 500 );     frame.setdefaultcloseoperation ( jframe.exit_on_close );     frame.pack ();     frame.setlocationrelativeto ( null );     frame.setvisible ( true ); }  public static class logolayout implements layoutmanager {     public string content = "content";     public string logo = "logo";      private map<component, string> constraints = new hashmap<component, string> ();      public void addlayoutcomponent ( string name, component comp )     {         constraints.put ( comp, name );     }      public void removelayoutcomponent ( component comp )     {         constraints.remove ( comp );     }      public void layoutcontainer ( container parent )     {         insets bi = parent.getinsets ();         int contentsize = 0;         int logosize = 0;         int maxheight = 0;         ( component component : parent.getcomponents () )         {             dimension ps = component.getpreferredsize ();             if ( constraints.get ( component ).equals ( content ) )             {                 contentsize = math.max ( ps.width, contentsize );             }             else if ( constraints.get ( component ).equals ( logo ) )             {                 logosize = math.max ( ps.width, logosize );             }             maxheight = math.max ( ps.height, maxheight );         }          int width = parent.getwidth () - bi.left - bi.right;         int height = parent.getheight () - bi.top - bi.bottom;         ( component component : parent.getcomponents () )         {             if ( constraints.get ( component ).equals ( content ) )             {                 if ( contentsize + logosize < width )                 {                     component.setbounds ( bi.left, bi.top, width - logosize, height );                 }                 else                 {                     component.setbounds ( bi.left, bi.top, contentsize, height );                 }             }             else if ( constraints.get ( component ).equals ( logo ) )             {                 if ( contentsize + logosize < width )                 {                     component                             .setbounds ( bi.left + width - logosize, bi.top, logosize, height );                 }                 else                 {                     int scaledlogosize = width - contentsize;                     if ( scaledlogosize > 0 )                     {                         component.setbounds ( bi.left + width - scaledlogosize, bi.top,                                 scaledlogosize, height );                     }                 }             }         }     }      public dimension preferredlayoutsize ( container parent )     {         insets bi = parent.getinsets ();         int contentsize = 0;         int logosize = 0;         int maxheight = 0;         ( component component : parent.getcomponents () )         {             dimension ps = component.getpreferredsize ();             if ( constraints.get ( component ).equals ( content ) )             {                 contentsize = math.max ( ps.width, contentsize );             }             else if ( constraints.get ( component ).equals ( logo ) )             {                 logosize = math.max ( ps.width, logosize );             }             maxheight = math.max ( ps.height, maxheight );         }         return new dimension ( bi.left + contentsize + logosize + bi.right,                 bi.top + maxheight + bi.bottom );     }      public dimension minimumlayoutsize ( container parent )     {         return preferredlayoutsize ( parent );     } } 

is how want logo act when window getting sized?

p.s. way can modify layout anytime need add (for example) specific component location, gap between logo , content or else...


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -