Why is my JTextField in JFrame not working? - java

Simple question. I've been trying to block/disable my textfield in my JFrame, but unfortunately it just doesn't work!
Suppose the name of my disable textfield is "fnTxt", so in the code I wrote fnTxt.setEnabled(false);. And when I run it, nothing happen! The textfield is still able to be click/fill.
Am I doing it wrong or it's something else?

setEnabled works fine for what it is meant for.
But I assume you want to make it invisible, for that you simple have to use fnTxt.setVisible(false);

Related

Letting user set default save directory via textfield

Im aware that there are better ways, but I need to implement it like this for this application. Its supposed to be a practice for properties too.
I bound a textProperty defaultDir from textfield inside a setDefaultDir Dialog, with the method FileChooser.setInitialDirectory(new File(defaultDir.get())); inside of my Save Dialog.
The binding itself works fine, I tested it by simply calling a printline of defaultDir right before calling setInitialDirectory(). So the problem must lie with how I input the text in my textfield.
I could not find anything on this online, usually I dont have problems googling stuff but I didnt find anything that would help me out. Since the binding seems to be working just fine, how does the user have to format the Directory in the textfield for this to work? My "C:/User/Jonas/Documents" didnt work
Thanks in advance

Can't change LaF JMenu "buttons"

As I'm modifying an existing Look and Feel, I also want to change how the "buttons" of a PopUpMenu behave. Right now it behaves like this, when I hover my mouse over it. As you can see it behaves very "3D":
And I want to let it behave like the buttons I made below them:
I've looked trough alot of documentation of Java Swing but I can't seem to find it. So if someone knows, please help me out. I have tried to change every property I could find.
The correct answer here is, as I found out, not everything can be managed by the LaF. Therefore, sometimes you have to get your hands dirty.
In this case I created my own CSTMButton, because in Swing one is also able to add buttons to a menubar. Now I can create it's own listener to generate the behaviour I want.

Switching the text on a JButton

This one may be easy for you. But I'm stuck and can't figure out an algorithm for doing that. I want to show a JTextField and change the text on the JButton to "Hide" if it's "Search". If
the text on the JButton is "Search" a JTextBox should appear and vice versa, if the text is "Hide" make the JTextField invisivle and change the text on JButton to "Search"
This is how I have done it:
private void switchBtnText(){
searchTxtField.setVisible(true);
btnSearch.setText("Hide");
if(btnSearch.getText().equals("Hide")){
btnSearch.setText("Search");
searchTxtField.setVisible(false);
}
}
If I comment the if section it works to show the JTextField. My problem is to go back to the default settings which is a JButton with "Search" as a text and with an invisible JTextField.
The method is then called in an ActionEvent. I've done this before, in C#, so I know I'm close.
Thank you in advance. The fastest and best answer will get upvoted and accepted.
This should work although I've not tested it.
//btn action
private void toggleVisible(){
String btnVal = btnSearch.getText();
if(btnVal.equals("Search")){
searchTxtField.setVisible(true); // or however you are showing search field
btnSearch.setText("Hide");
}else{
searchTxtField.setVisible(false);
btnSearch.setText("Search");
}
}
Take a look at your execution sequence....
setText to "Hide"
if text equals "Hide", change text to "Show"
Try changing the logic so you check the text first, then make decisions about what should be done...
If text equals "Hide", change text to "Show"
Else, change text to "Hide"

JLabel should not be able to receive focus.. but it does?

I'm working on a Swing application that uses the default Swing methods for handling focus. Focus isn't working as I'd expect.
In one case, I have a JTextField that I call .requestFocusInWindow() When the window is displayed a JLabel has focus instead
The Java 6 docs for JLabel say "As a result, it cannot get the keyboard focus." http://docs.oracle.com/javase/6/docs/api/javax/swing/JLabel.html
However, I have a sample application that shows a JLabel receiving focus and KeyboardFocusManager.getFocusOwner() returns that component. (http://github.com/akinsgre/swingStarter)
The code the the class is https://raw.github.com/akinsgre/swingStarter/master/src/main/java/test/HelloWorldSwing.java
Can anyone help me understand or explain what I'm missing?
I think you need to associate the label with the text field. So try using the setLabelFor method and see if that helps.

Make a text field highlighted when tabbed to in NetBeans (Java)

I'm trying to make it so when I tab to some text fields on in my JFrame the data in the text field will be highlighted. I thought I had done this in the properties before but I am not seeing the option now. Does anyone know how to do this?
You could use a FocusListener to work out when your field has focus (tutorial here).
Decide whether you want to select the text in the field, or whether you just want to change the background color. It's not quite clear what you mean by highlight.

Categories

Resources