java - Cannot add to layout constraints must be a gridbagconstraint -
i need use gridbaglayout below error. can please help?
java.lang.illegalargumentexception: cannot add layout: constraints must gridbagconstraint @ java.awt.gridbaglayout.addlayoutcomponent(unknown source) @ java.awt.container.addimpl(unknown source) @ java.awt.container.add(unknown source) @ havuzaracıformu.(havuzaracıformu.java:221) @ havuzaracıformu$1.run(havuzaracıformu.java:47) @ java.awt.event.invocationevent.dispatch(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$500(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
import java.awt.borderlayout; import java.awt.eventqueue; import java.awt.font; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.gridlayout; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.border.emptyborder; import org.jdatepicker.impl.jdatepanelimpl; import org.jdatepicker.impl.jdatepickerimpl; import org.jdatepicker.impl.utildatemodel; import javax.swing.jlabel; import javax.swing.jtextfield; import javax.swing.jbutton; import javax.swing.jformattedtextfield.abstractformatter; import java.awt.event.actionlistener; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import java.text.parseexception; import java.text.simpledateformat; import java.util.calendar; import java.util.properties; import java.awt.event.actionevent; public class havuzaracıformu extends jframe { private jpanel contentpane; private jtextfield textfieldadsoyad; private jtextfield textfieldgorev; private jtextfield textfielddepartman; /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { havuzaracıformu frame = new havuzaracıformu(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create frame. * @throws classnotfoundexception * @throws sqlexception */ public havuzaracıformu() throws classnotfoundexception, sqlexception { setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 728, 992); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); contentpane.setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.fill = gridbagconstraints.horizontal; gbc.anchor = gridbagconstraints.center; setcontentpane(contentpane); jlabel lblkullancbilgileri = new jlabel("kullan\u0131c\u0131 bilgileri"); font font = lblkullancbilgileri.getfont(); lblkullancbilgileri.setfont(new font(font.getfontname(), font.bold, 14)); contentpane.add(lblkullancbilgileri); jlabel lbladsoyad = new jlabel("ad\u0131 soyad\u0131"); jlabel lblgorevi = new jlabel("görevi"); jlabel lbldepartmani = new jlabel("departman\u0131"); contentpane.add(lbladsoyad,gbc); textfieldadsoyad = new jtextfield(); contentpane.add(textfieldadsoyad, gbc); textfieldadsoyad.setcolumns(10); contentpane.add(lblgorevi); textfieldgorev = new jtextfield(); contentpane.add(textfieldgorev, gbc); textfieldgorev.setcolumns(5); contentpane.add(lbldepartmani, gbc); textfielddepartman = new jtextfield(); contentpane.add(textfielddepartman); textfielddepartman.setcolumns(10); jlabel lblaracbilgileri = new jlabel("araç bilgileri"); font font2 = lblaracbilgileri.getfont(); lblaracbilgileri.setfont(new font(font2.getfontname(), font.bold, 14)); contentpane.add(lblaracbilgileri, gbc); jlabel lblmarka = new jlabel("marka"); contentpane.add(lblmarka, gbc); jtextfield textfieldmarka = new jtextfield(5); contentpane.add(textfieldmarka,gbc); textfieldmarka.setcolumns(5); jlabel lblmodel = new jlabel("model"); contentpane.add(lblmodel,gbc); jtextfield textfieldmodel = new jtextfield(); contentpane.add(textfieldmodel,gbc); textfieldmodel.setcolumns(5); jlabel lblplakano = new jlabel("plakano"); contentpane.add(lblplakano,gbc); jtextfield textfieldplakano = new jtextfield(); contentpane.add(textfieldplakano,gbc); textfieldplakano.setcolumns(5); jlabel lblcikistarihivesaati = new jlabel("Çıkış tarihi ve saati"); contentpane.add(lblcikistarihivesaati,gbc); jtextfield textfieldcikis = new jtextfield(); // contentpane.add(textfieldcikis); utildatemodel model = new utildatemodel(); properties p = new properties(); p.put("text.today", "today"); p.put("text.month", "month"); p.put("text.year", "year"); jdatepanelimpl datepanel = new jdatepanelimpl(model, p); jdatepickerimpl datepicker = new jdatepickerimpl(datepanel, new datelabelformatter()); contentpane.add(datepicker,gbc); textfieldcikis.setcolumns(5); jlabel lblcikiskm = new jlabel("Çıkış km"); contentpane.add(lblcikiskm,gbc); jtextfield textfieldcikiskm = new jtextfield(); contentpane.add(textfieldcikiskm,gbc); textfieldcikiskm.setcolumns(5); jlabel lbldonustarihivesaati = new jlabel("dönüş tarihi ve saati"); contentpane.add(lbldonustarihivesaati,gbc); jtextfield textfielddonus = new jtextfield(); utildatemodel model2 = new utildatemodel(); properties p2 = new properties(); p2.put("text.today", "today"); p2.put("text.month", "month"); p2.put("text.year", "year"); jdatepanelimpl datepanel2 = new jdatepanelimpl(model2, p); jdatepickerimpl datepicker2 = new jdatepickerimpl(datepanel2, new datelabelformatter()); contentpane.add(datepicker2,gbc); textfielddonus.setcolumns(5); jlabel lbldonuskm = new jlabel("dönüş km"); contentpane.add(lbldonuskm,gbc); jtextfield textfielddonuskm = new jtextfield(); contentpane.add(textfielddonuskm,gbc); textfielddonuskm.setcolumns(5); jlabel lblneden = new jlabel("aracın kullanılacağı yer-kullanım neden geçici araç"); contentpane.add(lblneden,gbc); jtextfield textfieldneden = new jtextfield(); contentpane.add(textfieldneden,gbc); textfieldneden.setcolumns(5); jlabel lblkullanimsuresi = new jlabel("kullanım süresi"); contentpane.add(lblkullanimsuresi,gbc); jtextfield textfieldsure = new jtextfield(); contentpane.add(textfieldsure,gbc); textfieldsure.setcolumns(5); jlabel lblhasarveeksiklikler = new jlabel("hasar ve eksiklikler"); contentpane.add(lblhasarveeksiklikler,gbc); jlabel lblaractagorulenhasarlar = new jlabel("araçta görülen hasarlar veya eksiklikler (zincir, İstetme, İlk yardım Çantası vs."); contentpane.add(lblaractagorulenhasarlar,gbc); jtextfield textfieldhasar = new jtextfield(); contentpane.add(textfieldhasar,gbc); textfieldhasar.setcolumns(5); jbutton btnnewbutton = new jbutton("gönder"); btnnewbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { string url = "jdbc:sqlserver://win-j53h5bk2anc;databasename=test;integratedsecurity=true;"; try { class.forname("com.microsoft.sqlserver.jdbc.sqlserverdriver"); connection conn = drivermanager.getconnection(url); statement st = conn.createstatement(); string querystring = "insert havuzaracıformu values ('" + textfieldadsoyad.gettext() + "'," + "'" + textfieldgorev.gettext() + "'," + "'" + textfielddepartman.gettext() + "'," + "'" + textfieldmarka.gettext() + "'," + "'" + textfieldmodel.gettext() + "'," + "'" + textfieldplakano.gettext() + "'," + "'" + datepicker.getjformattedtextfield().gettext() + "'," + "'" + textfieldcikiskm.gettext() + "'," + "'" + datepicker2.getjformattedtextfield().gettext()+ "'," + "'" + textfielddonuskm.gettext() + "'," + "'" + textfieldneden.gettext() + "'," + "'" + textfieldsure.gettext() + "'," + "'" + textfieldhasar.gettext() + "')"; st.executeupdate(querystring); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); contentpane.add(btnnewbutton, borderlayout.south); } } class datelabelformatter extends abstractformatter{ private string datepattern = "yyyy-mm-dd"; private simpledateformat dateformatter = new simpledateformat(datepattern); @override public object stringtovalue(string text) throws parseexception { return dateformatter.parseobject(text); } @override public string valuetostring(object value) throws parseexception { if(value != null){ calendar cal = (calendar) value; return dateformatter.format(cal.gettime()); } return ""; } }
once code formed simpler form compile without cruft, first line of stack trace relates custom (i.e. your) code is:
contentpane.add(btnnewbutton, borderlayout.south);
borderlayout.south
constraint meant borderlayout
, earlier in code see..
contentpane.setlayout(new gridbaglayout()); // i.e. **not** borderlayout
general advice
- see what stack trace, , how can use debug application errors? programmers see lot of exceptions throughout our careers, need learn how solve them, or @ least majority of them. when know how solve exceptions not sure why particular exception happening in particular line of code, should ask on so.
- as mentioned several people in comments, when in doubt, simplify code point error shows irrelevant code removed.
- the reason looked because bored , not sleep. i'd vote close , move on next problem person manage create mcve. ..don't rely on taking pity on in future.
Comments
Post a Comment