#Making text-fields not user-editable

1 messages · Page 1 of 1 (latest)

edgy gulch
#

Have some text-fields, but I can enter text in them. They should only be there to display calculations. How would I make it so I can't edit the text-fields.

proud sundialBOT
#

<@&987246399047479336> please have a look, thanks.

proud sundialBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

edgy gulch
#

the text fields code:

#

double subTotal = (bagelsQuantity * bagelsPrice) + (cookiesQuantity * cookiesPrice)+ (muffinsQuantity * muffinsPrice) + (pieQuantity * piePrice);
textfieldSubtotal.setText(String.format("%.2f", subTotal)); // https://stackoverflow.com/questions/6350797/how-to-set-new-text-in-jtextfield-after-creation

final double taxRate = 0.13;
double taxAmount = subTotal * taxRate;
textfieldTax.setText(String.format("%.2f", taxAmount)); // https://stackoverflow.com/questions/34461275/how-to-set-text-format#:~:text=You%20should%20use%20the%20String.format%20%28%29%20method%20along,results%20that%20you%20want%3A%20high.setText%20%28String.format%20%28%22%2506d%22%2C%20gamescore%29%29%3B

double Gtotal = subTotal + taxAmount;
textfieldTotal.setText(String.format("%.2f", Gtotal));
textfieldTotal.setEditable(false);
#

Not sure how I would make it unedible

#

uneditable*

winter shore
#

Just call setEnabled() on it:

        searchField = new JTextField();
        searchField.setText("whatever");
        searchField.setEnabled(false);
proud sundialBOT
winter shore
#

note that you might need to adjust colors to have black text instead of gray

dapper python
#

Might aswell just use a label 😉

winter shore
dapper python
#

looks don't really matter, stylesheet will override that

winter shore
#

what stylesheet? JTextField is Swing iirc

winter shore
#

You don't. The HTML parser that some Swing components use does not even support most HTML tags; it does not support CSS at all.

If you need advanced HTML support in a Java app, you will have to use one of the third-party components that provide it.

dapper python
#

yeah , scroll down :p

#

looks are personal aswell. If its just for displaying some text that the user has no need to interact with, a label suffices

#

textbox screams, click me

winter shore
#

Welll, I don't know about the use case of OP, but from what I saw you'll need to override UI LaF often enough anyways if you want custom behaviour - using CSS seems counterproductive to me

#

And labels on their own look kinda clunky, compare:

edgy gulch
#

I got it work so that if they click any button, they can no longer click on the textfield

#

But they can click on the text field before clicking any other button so it kinda worked and kinda didnt