java - GridBagLayout not resizing on image load -
i have jframe. uses jpanel content pane, , jpanel uses gridbaglayout layoutmanager. jpanel contains 2 more items: button, , jpanel. on program start, image loaded file lowest-level jpanel bufferedimage using imageio.read(...). here goes pieces.
the image loads correctly, can see small corner of on screen (14px square specified in debugger). there nothing can figure out cause layout grow , fit entire image in lowest level jpanel on screen. image in debuggers shows correct size of 500px. preferred size of cardimagepanel shows correctly same size image. layout not respect preferred size unless manually set cardimagepanel size using setsize(...) i'm pretty sure not supposed necessary gbl.
i have tried putting revalidate() , repaint() calls on every single jframe, jpanel, layout, grid bag, image, etc throughout entire program , can't find correct place or time call them make thing work. i've been trying let image load incorrectly , use button force revalidation , repaint, explicit call not doing anything.
i'm losing mind, i'll thing working.
here code whole stupid thing (minus imports , package specification.
p1s1.java:
public class p1s1 { public static void main(string[] args) { // todo code application logic here build(); } public static void build() { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(new dimension(640, 480)); frame.setcontentpane(new guipanel(frame)); frame.setvisible(true); } }
guipanel.java:
public class guipanel extends jpanel { jframe parentframe; jbutton imageloaderbutton; cardimagepanel cardimagepanel; layoutmanager layout; gridbaglayout gridbaglayout; gridbagconstraints constraints; public guipanel(jframe frame) { parentframe = frame; constraints = new gridbagconstraints(); gridbaglayout = new gridbaglayout(); layout = gridbaglayout; this.setlayout(layout); this.setborder(borderfactory.createlineborder(color.black)); setupimageloaderbutton(imageloaderbutton); cardimagepanel = new cardimagepanel(); this.add(cardimagepanel); } private void setupimageloaderbutton(jbutton button) { button = new jbutton("click load image!"); actionlistener imageloaderlistener; imageloaderlistener = new actionlistener() { @override public void actionperformed(actionevent ae) { system.out.println("button clicked."); cardimagepanel.revalidate(); cardimagepanel.repaint(); guipanel.this.revalidate(); guipanel.this.repaint(); parentframe.revalidate(); parentframe.repaint(); } }; button.addactionlistener(imageloaderlistener); this.add(button); } }
cardimagepanel.java:
public class cardimagepanel extends jpanel { bufferedimage cardimage; public cardimagepanel() { this.setborder(borderfactory.createlineborder(color.black)); try { cardimage = imageio.read(new file("c:\\dev\\cards\\2_of_clubs.png")); this.setpreferredsize(new dimension(cardimage.getwidth(), cardimage.getheight())); } catch (ioexception ex) { system.out.println("exception trying load image file."); } } // getpreferredsize() override suggested madprogrammer. // did not solve issue, see madprogrammer's updated, // accepted answer below correct solution. rest of // code reflects original attempt solve issue. @override public dimension getpreferredsize() { return cardimage != null ? new dimension(cardimage.getwidth(), cardimage.getheight()) : super.getpreferredimage(); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawimage(cardimage, 0, 0, this); } }
gridbaglayout
relies on component telling size (along it's minimum , maximum size when it's relievent). need override getpreferredsize
method of cardimagepanel
, returning size component be
public class cardimagepanel extends jpanel { bufferedimage cardimage; public cardimagepanel() { this.setborder(borderfactory.createlineborder(color.black)); try { cardimage = imageio.read(new file("c:\\dev\\cards\\2_of_clubs.png")); } catch (ioexception ex) { system.out.println("exception trying load image file."); } } @override public dimension getpreferredsize() { return cardimage != null ? new dimension(cardimage.getwidth(), cardimage.getheight()) : super.getpreferredsize(); } @override public dimension getminimumsize() { return getpreferredsize(); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawimage(cardimage, 0, 0, this); } }
have @ how use gridbaglayout more details
Comments
Post a Comment