How to set JTextField as a selected component - java

I am developing an application which have a JTextField, JButton and JTextArea in same JFrame. How do I set JTextField selected when JFrame starts.

How do I set JTextField selected when JFrame starts.
I assume you mean you want the text field to have focus so you can start typing into it. ("Selected", in Swing terminology, means the text contained in the text field will be highlighted.)
The first component on the frame (top/left) will gain focus automatically.
If your text field isn't the first component then you can request focus with code like:
frame.setVisible(true);
textField.requestFocusInWindow();
The key is that you need to make the focus request AFTER the frame is visible.

Related

Java jFrame toBack() and focusable

I got a Jframe which has a JTextField and a JButton which opens a popup. The popup has to be always in front of the JFrame.
Is it possible to write on the JTextField while the popup is still in front of the JFrame?
Yes you can still modify the contents inside an object.

How to write something in JTextArea and show it without pressing any button

I have taken a text area on a jframe but I dont know how to write text for text area on the source and I want to show message on the text area when I press a button from another jframe.
Suppose I clicked "Location" button on a jframe and the location will show in a text area on another jframe.
Use a Observer Pattern to allow you class with the JTextArea to "obsever" changes or events from your other class. This way you decouple your code and make it easier to manage and update.
You could make your own or re-use one of the listeners from Swing, such as ActionListener or ChangeListener

Remove Focus from JTextfields

I have a JDialog with some JTextfields, and I want to remove the focus from the first textfield when I open the Dialog.
I tried .removeFocus(), but then I can't use focus anymore. I just want to remove it, so when I open the dialog no textfield is selected.
From How to use the focus subsystem we get the following:
If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed. The following sample code shows how this operation can be done:
//...Where initialization occurs...
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(new BorderLayout());
//...Create a variety of components here...
//Create the component that will have the initial focus.
JButton button = new JButton("I am first");
panel.add(button);
frame.getContentPane().add(panel); //Add it to the panel
frame.pack(); //Realize the components.
//This button will have the initial focus.
button.requestFocusInWindow();
frame.setVisible(true); //Display the window.
Call requestFocus() on the control you want the focus. You could for example focus the default button of the dialog or something similar. From the How to Use the Focus Subsystem:
If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed. The following sample code shows how this operation can be done:

How to add JTextField in time running in Java?

In order to develop a edit text in java for study. I'm with a problem: My program once opened by the user, if the user to click in button "Search", then the ActionListener will add a field in Jpanel.
For example: I have a class JToolBar that sets up jtoolbar menu that extends JPanel. Then, I add it in JFrame. Within that JToolbar there's a button "Search", if the user to click at this button, a JTextField will appears side this menu instantly.
I try to create a class private within that JToolBar class. So, I just add the JTextField to JPanel contains the JToolbar. However, is not working. There's no error. Simply not appears the JTextField. What I do to solve that problem ?
When you add a component to a visible GUI the general code is:
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
You need the revalidate() to tell the layout manager that a component has been added.

add panel to combobox popup menu

i have jFrame = frame
it have jcombobox = combo
then i have jpanel = panel
i have many component inside this panel
i try to add this panel into combobox popupmenu
so if combobox clicked,
panel that have many components will show up
it is possible to add panel into combobox popup menu?!?!
how to do it???
i already read
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
and
http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ComboBoxDemoProject/src/components/ComboBoxDemo.java
but still not have any clue
how to do it?
thankz a lot for any help...
So from your description, you have a panel that is not visible, that you would like to appear if the combobox is clicked? So it will appear for any option in the combobox?
That should be simple enough. Lets modify the JLabel in this ComboBoxDemo from the Java Tutorials. Since they both inherit from JComponent, we will be able to make the JLabel and the JPanel visible in the same way.
First, make sure you understand what the demo is doing. The Combobox options are changing the format of the date's text in the JLabel. We want to edit the demo in such a way that this JLabel is not visible until after we select any option in our JComboBox.
First, we will want to include a boolean as a class variable, so that we can access it in any of our methods.
boolean visibleComp;
Next, in the constructor, you will want to change the JLabel "result" to be invisible by default. We can do this by using the setVisible method of the JComponent.
result.setVisible(false);
Now we need to control when and how result becomes visible -- as we continue through the code, we see that the actionPerformed method handles our events, and passes the formatting details off to another method called reformat.
Since reformat is also called in our constructor, we will want to set our boolean value in the actionPerformed method.
visibleComp = true;
We will then want to add a conditional statement to the try block in reformat -- this will check to see if our boolean is true, which would only occur if an action had been performed by the user. We can use this to set our component's visibility.
if(visibleComp){
result.setVisible(true);
}
You can easily interchange a JPanel with this example. Hope that helps.

Categories

Resources