Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

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.

  • 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?

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.