swing - Issue with JTextField and JComboBox in same GridBagLayout in Java -


i trying these 2 elements onto frame using gridbaglayout, add jcombobox gridbaglayout , add frame, jtextfields become short in width , have no idea why or how fix it. when take jcombobox out, works fine, need have jcombobox there, otherwise can't complete project. on how resolve issue appreciated. here code:

package userinterface; import java.awt.dimension; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.calendar; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jtextfield;  public class addspend extends jframe {      public addspend(){         settitle("add new spend");         setsize(300,200);         setvisible(true);         setresizable(false);         setdefaultcloseoperation(dispose_on_close);         setlayout(new gridbaglayout());         create();     }      private void create(){         gridbagconstraints c = new gridbagconstraints();         c.gridx = 0;         c.gridy = 0;         c.anchor = gridbagconstraints.line_end;         add(new jlabel("category: "), c);         c.gridy++;         add(new jlabel("amount: "), c);         c.gridy++;         add(new jlabel("date: "), c);          c.gridx = 1;         c.gridy = 0;         c.anchor = gridbagconstraints.line_start;         jcombobox<string> category = new jcombobox<string>();         category.additem("test 1");         category.additem("test 2");         category.additem("test 3");         add(category, c);         c.gridy++;         jtextfield amount = new jtextfield();         add(amount, c);         c.gridy++;         jtextfield date = new jtextfield();         add(date, c);         c.gridy++;         jbutton today = new jbutton("today");         today.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e){                 calendar = calendar.getinstance();                 date.settext((now.get(calendar.month) + 1) + "/" + now.get(calendar.year));             }         });         today.setfont(today.getfont().derivefont(7.0f));         today.setpreferredsize(new dimension(30,15));         add(today, c);         c.gridy++;         jbutton add = new jbutton("add spend");         add(add, c);     } } 

suggestions:

  • get rid of setsize(300,200); , replace pack(); setting size artificially constrain gui size not best natural size of it. calling pack() after adding components gui, allow each component size own calculated best size.
  • also, change new jtextfield(); new jtextfield(col_width); col_width int number of text columns wish jtextfield show. suggest jtextfield increase preferred size accommodate col_width number of characters.
  • also add insets gridbagconstraints
  • call pack() , setvisible(true) after adding components gui , not before.

e.g.,

import java.awt.dimension; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.calendar;  import javax.swing.borderfactory; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.swingutilities;  public class addspend extends jpanel {      public addspend() {         create();     }      private void create() {         setlayout(new gridbaglayout());         gridbagconstraints c = new gridbagconstraints();          int ins = 5;         c.insets = new insets(ins, ins, ins, ins);         setborder(borderfactory.createemptyborder(ins, ins, ins, ins));          c.gridx = 0;         c.gridy = 0;         c.anchor = gridbagconstraints.line_end;         add(new jlabel("category: "), c);         c.gridy++;         add(new jlabel("amount: "), c);         c.gridy++;         add(new jlabel("date: "), c);          c.gridx = 1;         c.gridy = 0;         c.anchor = gridbagconstraints.line_start;         jcombobox<string> category = new jcombobox<string>();         category.additem("test 1");         category.additem("test 2");         category.additem("test 3");         add(category, c);         c.gridy++;         jtextfield amount = new jtextfield(10);         add(amount, c);         c.gridy++;         jtextfield date = new jtextfield(10);         add(date, c);         c.gridy++;         jbutton today = new jbutton("today");         today.addactionlistener(new actionlistener() {             public void actionperformed(actionevent e) {                 calendar = calendar.getinstance();                 date.settext((now.get(calendar.month) + 1) + "/" + now.get(calendar.year));             }         });         today.setfont(today.getfont().derivefont(7.0f));         today.setpreferredsize(new dimension(30, 15));         add(today, c);         c.gridy++;         jbutton add = new jbutton("add spend");         add(add, c);     }      public static void main(string[] args) {         swingutilities.invokelater(() -> {             jframe frame = new jframe("add new spend");             frame.setdefaultcloseoperation(jframe.exit_on_close);             frame.add(new addspend());             frame.pack();             frame.setresizable(false);             frame.setlocationrelativeto(null);             frame.setvisible(true);         });     } } 

note prefer extending jpanel on jframe. may painting in corner having class extend jframe, forcing create , display jframes, when more flexibility called for. in fact, venture of swing gui code i've created , i've seen not extend jframe, , in fact rare you'll ever want this. more commonly gui classes geared towards creating jpanels, can placed jframes or jdialogs, or jtabbedpanes, or swapped via cardlayouts, wherever needed. increase flexibility of gui coding.


Comments