Changing where an object is on a GridBagLayout in Java -


i working on project have requirement when button clicked, object in gridbaglayer moves different spot. here code far:

    import java.awt.gridbagconstraints;     import java.awt.gridbaglayout;     import java.awt.event.actionevent;     import java.awt.event.actionlistener;      import javax.swing.imageicon;     import javax.swing.jbutton;     import javax.swing.jframe;     import javax.swing.jlabel;      public class main extends jframe{     private jbutton button,littlebutton;     private jlabel holdred1,holdred2;     private imageicon red1,red2;  public main(){ //sets layout setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); //sets button button = new jbutton("button"); c.gridx = 0; c.gridy = 0; add(button,c); //sets image red1 = new imageicon(getclass().getresource("/images/redtile.png")); red2 = new imageicon(getclass().getresource("/images/redtile.png")); //puts in jlabel holdred1 = new jlabel(red1); c.gridx = 0; c.gridy = 0; add(holdred1, c); holdred2 = new jlabel(red2); c.gridx = 1; c.gridy = 0; add(holdred2, c); littlebutton = new jbutton("click me"); c.gridx = 0; c.gridy = 0; add(littlebutton,c);  event e = new event(); littlebutton.addactionlistener(e); }  public class event implements actionlistener {     public void actionperformed(actionevent e){         //code goes here       } } public static void main(string args[]){     //displaying window     main gui = new main();     gui.setdefaultcloseoperation(jframe.exit_on_close);     gui.setsize(575,575);     gui.setvisible(true);     gui.settitle("change spot"); } } 

i don't have experience grid bag layouts have no clue how this, in advance!

the question how can make button change it's gridx 1

in gridbagconstraints specify position want element take in grid. have done that, in portion of code

c.gridx = 0; c.gridy = 0; add(holdred1, c); holdred2 = new jlabel(red2); c.gridx = 1; c.gridy = 0; add(holdred2, c); littlebutton = new jbutton("click me"); c.gridx = 0; c.gridy = 0; 

but you're telling layout place multiple components in same grid using c.gridx = 0, c.gridy = 0. every component should have own cell space don't overlap.

i edited code:

 setlayout(new gridbaglayout());             gridbagconstraints c = new gridbagconstraints(); //sets button             button = new jbutton("button");             c.gridx = 2;             c.gridy = 2;             add(button, c); //sets image //            red1 = new imageicon(getclass().getresource("/images/redtile.png")); //            red2 = new imageicon(getclass().getresource("/images/redtile.png")); //puts in jlabel             holdred1 = new jlabel("red1");             c.gridx = 1;             c.gridy = 1;             add(holdred1, c);             holdred2 = new jlabel("red2");             c.gridx = 1;             c.gridy = 2;             add(holdred2, c);             littlebutton = new jbutton("click me");             c.gridx = 2;             c.gridy = 1;             add(littlebutton, c);              event e = new event();             littlebutton.addactionlistener(e); 

i assume wanted labels on top of buttons, place on side practice it.

another easy tips, if want component take more cell in table have use c.weightx = somevalue, c.weighty = somevalue.

take look: how use gridbaglayout.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -