Posted March 1, 20169 yr Hi! I have a problem with the GuiTextField. I'm trying to show a GUI to enter a String. But my GuiTextField does not show in the GUI when I open it in the game. Here is my code: [embed=425,349]public class CustomGUI extends GuiContainer { public CustomGUI(Container inventorySlotsIn) { super(inventorySlotsIn); } private int distanceFromLeftSide; private GuiTextField idTextField; @Override public void initGui() { super.initGui(); this.distanceFromLeftSide = (this.width - 256) / 2; //net.minecraft.client.gui.GuiTextField.GuiTextField(int componentId, FontRenderer fontrendererObj, int x, int y, int par5Width, int par6Height) this.idTextField = new GuiTextField(1, this.fontRendererObj, this.distanceFromLeftSide + 10, 30, 236, 15); this.idTextField.setEnableBackgroundDrawing(false); this.idTextField.setMaxStringLength(40); this.idTextField.setVisible(true); this.idTextField.setEnabled(true); } @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { super.drawScreen(mouseX, mouseY, partialTicks); // this.drawString(this.fontRendererObj, "lalala", // this.distanceFromLeftSide + 10, 30, 0x000000); } @Override protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { this.mc.getTextureManager().bindTexture(new ResourceLocation("textures/gui/demo_background.png")); this.drawTexturedModalRect(this.distanceFromLeftSide, 20, 0, 0, 256, 256); } @Override protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { super.drawGuiContainerForegroundLayer(mouseX, mouseY); this.idTextField.drawTextBox(); } @Override public boolean doesGuiPauseGame() { return true; } } [/embed] Does anybody have any idea why this only shows the GUI background and not the GuiTextField? Any help would be very appreciated. Thanks Mike
March 1, 20169 yr People should really start reading vanilla code. For future: when extending something, actually read its code. http://www.minecraftforge.net/forum/index.php/topic,31297.0.html Should help. EDIT - HOLD ON! Why are you using GuiContainer? My answer above is about placement of BG and slots in GuiContainer that actually corresponds with Container class. If you don't have Container for your Gui, you most likely don't need it - use (extend) GuiScreen! 1.7.10 is no longer supported by forge, you are on your own.
March 1, 20169 yr Author I tried GuiScreen first. Using GuiContainer was a try to fix the problem since all the examples I found on the internet used GuiContainer. My Problem is not about positioning, it's about the GuiTextField not being displayed at all. I recognized that in all the examples I found on the internet the GuiTextField constructor didn't have the componentId parameter. Is it possible that there was a recent update that causes my problem?
March 1, 20169 yr public class GuiTest extends GuiScreen { private GuiTextField idTextField; @Override public void initGui() { super.initGui(); this.idTextField = new GuiTextField(1, this.fontRendererObj, 40, 40, this.width - 80, 20); this.idTextField.setEnableBackgroundDrawing(true); this.idTextField.setMaxStringLength(400); this.idTextField.setVisible(true); this.idTextField.setEnabled(true); } @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { super.drawScreen(mouseX, mouseY, partialTicks); this.idTextField.drawTextBox(); } @Override protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { super.mouseClicked(mouseX, mouseY, mouseButton); this.idTextField.mouseClicked(mouseX, mouseX, mouseButton); } @Override protected void keyTyped(char typedChar, int keyCode) throws IOException { super.keyTyped(typedChar, keyCode); this.idTextField.textboxKeyTyped(typedChar, keyCode); } } This should do it. If you want more use-cases look at textBox's code (e.g: setCanLoseFocus() or setFocused()). 1.7.10 is no longer supported by forge, you are on your own.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.