java - How to keep JComponents always centered even after being shrunk? -


objective

i making jpanel 3 jcomponents on it: wide custom jcomponent, , 2 custom abstractbuttons in turn extends jcomponent.

i'd organize jpanel 2 rows , 2 columns, wide jcomponent spanning 2 columns. there plenty of space around abstractbuttons. diagram drew of trying achieve:

enter image description here

problem

basically, when user clicks on 1 of abstractbuttons, shrinks. i'd them centered in cells when clicked upon. not case in code when used gridbaglayout however; buttons become off-centered , top-left hand corners stay wherever before.

code

this code below display (the red buttons shrink after being pressed):

enter image description here

import java.awt.*; import java.awt.event.*;  import javax.swing.*; import javax.swing.border.*;  public class example {     public static void main(string[] args)     {         // instantiate components needed.         jframe frame = new jframe();         jpanel panel = new jpanel();         frame.add(panel);         button b1 = new button();         button b2 = new button();         widecomponent w = new widecomponent();          // give margins each component         emptyborder padding = new emptyborder(20, 20, 20, 20);         b1.setborder(padding);         b2.setborder(padding);         w.setborder(padding);          // add stuff panel         panel.add(b1);         panel.add(b2);         panel.add(w);          // add panel frame, show frame         frame.add(panel);         frame.setvisible(true);         frame.pack();     } }  class button extends abstractbutton implements mouselistener {        final int width = 175;     final int height = 75;      boolean mousepressed;      public button()      {         addmouselistener(this);     }      // draw method     public void paintcomponent(graphics g)     {         super.paintcomponent(g);         g.setcolor(color.red);         if (!mousepressed)             g.fillrect(0, 0, width, height);         else         {             g.fillrect(0, 0, width - 30, height - 30);             g.setcolor(color.yellow);         }     }      // mouselistener interface implementation     public void mousepressed(mouseevent me) { mousepressed = true; repaint(); }     public void mousereleased(mouseevent me) { mousepressed = false; repaint(); }     public void mouseclicked(mouseevent e) {}     public void mouseentered(mouseevent e) {}     public void mouseexited(mouseevent e) {}      // jcomponent sizing methods     public dimension getpreferredsize()     {         return new dimension(width, height);     }     public dimension getmaximumsize() { return getpreferredsize(); }     public dimension getminimumsize() { return getpreferredsize(); }        }  class widecomponent extends jcomponent {     final int width = 500;     final int height = 150;       // drawing method     public void paintcomponent(graphics g)     {         super.paintcomponent(g);         g.setcolor(color.blue);         g.fillrect(0, 0, width, height);     }      // jcomponent sizing methods     public dimension getpreferredsize()     {         return new dimension(width, height);     }     public dimension getmaximumsize() { return getpreferredsize(); }     public dimension getminimumsize() { return getpreferredsize(); }     } 

you example code doesn't match picture drew , doesn't use gridbaglayout. sscce should replicate problem code tested.

the problem implementation of custom object. need custom property in class reflects current state of component. like:

public void setlargesize(boolean largesize) {     this.largesize = largesize;     revalidate();     repaint(); } 

then need modify getpreferredsize() method return size of component based on variable:

@override public dimension getpreferredsize() {     if (largesize)         return new dimension(width, height);     else         return new dimension(width - 30, height - 30); } 

now layout manager can job based on current state of component.

you change painting code. painting code doesn't need check pressed state because layout manager has taken consideration. actually, dont' think need override paintcomponent(). set properties of button:

setborderpainted( false ); setcontentareapainted( false ); setfocuspainted( false ); 

you can set background color in setlargesize(...) method.

finally mouselistener invoke setlargesize(...) method appropriate parameter.

so need make sure set constraints component gets centered in cell. may need set alignment x/y of component.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -