Jump to content

[1.15.2] GUI TextFieldWidget not letting me backspace


TurtlesAreHot

Recommended Posts

After fixing my last issue, I now have another issue I cannot solve. The TextFieldWidget lets me type in characters into the textbox, but it won't let me delete characters. How am I able to do this? Here is my code:

 

package mypackage;
import com.mojang.blaze3d.systems.RenderSystem;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TranslationTextComponent;

public class GUIPunishKick extends Screen {
	private final int WIDTH = this.width;
	private final int HEIGHT = this.height;
	
	private TextFieldWidget user;
	
	public GUIPunishKick() {
		super(new TranslationTextComponent("screen.guipunishkick.spawn"));
	}
	
	@Override
	protected void init() {
		int relX = WIDTH /2;
		int relY = HEIGHT /2;
		this.user = new TextFieldWidget(font, relX + 10, relY + 40, 160, 20, "User");
		user.setText("");
		user.setFocused2(true);
		user.setVisible(true);
		user.setMaxStringLength(17);
		//buttons
	}
	
	public void changeFocus() {
		if(user.isFocused()) user.setFocused2(false);
		else user.setFocused2(true);
	}
	public boolean charTyped(char par1, int par2) {
		super.charTyped(par1, par2);
		if(user.isFocused()) {
			this.user.charTyped(par1, par2);
		}
		
	
		return true;
	}
	
	
	public void updateScreen() {
		this.updateScreen();
		this.user.moveCursorBy(this.user.getText().length());
	}
	
	protected void mouseClicked(int x, int y, int btn) {
		super.mouseClicked(x,  y, btn);
		this.user.mouseClicked(x, y, btn);
	}
	
	
	
	@Override
	public boolean isPauseScreen() {
		return false;
	}
	
	
	
	@Override
	public void render(int mouseX, int mouseY, float partialTicks) {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
		int relX = (this.width - WIDTH) /2;
		int relY = (this.height - HEIGHT) /2;
		this.blit(relX, relY, 0, 0, WIDTH, HEIGHT);
		this.user.render(mouseX, mouseY, partialTicks);
		super.render(mouseX, mouseY, partialTicks);
	}
	
	public static void open() {
		Minecraft.getInstance().displayGuiScreen(new GUIPunishKick());
	}
}

I would really like to know how I can enable a way to delete characters from my TextFieldWidget. Thanks :)

Link to comment
Share on other sites

Hi,

 

You need to override keyPressed too and put logic inside to pass the key information to textwidget if it has focus

Something like this:

    @Override
    public boolean keyPressed(int p_keyPressed_1_, int p_keyPressed_2_, int p_keyPressed_3_) {
        if (txtXPToTransfer.isFocused()) {
            txtXPToTransfer.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_);
        }
        return super.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_);
    }

 

†GnR† Slash

can one man truly make a difference?

Link to comment
Share on other sites

7 hours ago, GnRSlashSP said:

Hi,

 

You need to override keyPressed too and put logic inside to pass the key information to textwidget if it has focus

Something like this:


    @Override
    public boolean keyPressed(int p_keyPressed_1_, int p_keyPressed_2_, int p_keyPressed_3_) {
        if (txtXPToTransfer.isFocused()) {
            txtXPToTransfer.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_);
        }
        return super.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_);
    }

 

Thank you this is exactly what I needed :)

Link to comment
Share on other sites

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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