Remove Focus from JTextfields - java

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:

Related

How to set JTextField as a selected component

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.

Removing a button during actionperformed of that button

I was trying to figure out why my program freezes and I was able to replicate it with a small script so I can put it here. Basically, in this script When you clicked on button Test1, it is supposed to remove it and added new button Test2. The program freezes. Why? How can I over come this?
final JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 600));
final JPanel panel = new JPanel();
final JButton bTest1 = new JButton("test1");
bTest1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
panel.remove(bTest1);
panel.add(new JButton("test2"));
}
});
panel.add(bTest1);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
Offcourse in the real program, on the button click, remove all the contents of the panel and re add a new set of components.
Looking for your help!
Offcourse in the real program, on the button click, remove all the contents of the panel and re add a new set of components.
Then you should probably be using a CardLayout. A CardLayout is designed to allow you to swap panels.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
The program freezes. Why? How can I over come this?
It doesn't freeze, its just that the panel isn't smart enough to repaint itself. It you resize the frame you will see the new button.
The problem is you remove the button and add a new button but the panel never repaints itself because the panel is not aware of the changes. You need to invoke the layout manager so the new button can be given a proper size.
The basic code for add/removing components on a visible GUI is:
panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
Action performed method of the JButton will be executed in AWT thread. When you remove the button from the container, that will launch events that should be executed as well in the same thread. So one is waiting for the other , and so the program freezes. For solving that situation use
SwingUtilities.invokeLater
method to execute the removing action of your button

Change JFrame Window buttons

I've created a normal window with 3 buttons (minimize,maximize,close)
But i want to change the Window bar to containe the title and the close button only
I did it before with one line but i forget how to make it
This is my code :
frame.setSize(60, 500);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setLocation(10,10);
frame.setResizable(false);
frame.setVisible(true);
I did it before with one line but i forget how to make it
I don't think you did it before because Swing doesn't support this on a JFrame.
Maybe you used a JDialog before? This is the behaviour of a dialog.
However a JDialog should generally only be used as a popup window of a main JFrame. A JDialog does not appear in the task bar and if the dialog loses focus it is not easy to get back unless you tab through all the open windows.

How to do a custom JoptionPane

I need to do a JOptionPane (or something similar) with a JTextField and two RadioButtons but I don't know if that's possible. I have a main frame with differents options and when I click on "Operacion" I should call the dialog. How can I make that dialog?
A textfield and two radio buttons should be small enough to fit into a JOptionPane, so perhaps it's best to keep using that.
Add the JTextField with the two radio buttons to a JPanel, and add that JPanel as the component that's displayed in a JOptionPane. Probably want to use the option pane that displays just an "Ok", with one of the radio buttons already selected.
To be safe, you may want to wrap that JPanel in a JScrollPane because I don't think JOptionPanes are re-sizable and depending on if a user changes your look and feel through command line options or perhaps accessibility settings then you might cut off some GUI components from them.
Personally, I'm not a fan of using the JOptionPane for this. Go down the path of using JDialog.
It can be as simple as:
JPanel innerPanel = new JPanel(new FlowLayout());
// Add components and listeners here
JDialog dialog = new JDialog();
dialog.add(innerPanel);
dialog.setModal(true);
dialog.pack();
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
dialog.addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing (WindowEvent e)
{
super.windowClosing(e);
}
});

Open a window-popup from JMenuItem java

I have a JMenu with a JMenuItem, and when I click on this, I need to open a JFrame or window, in other words a component with inside JButton, JTextField,...
How can i do this ?
You can create the JFrame in the same way you created your initial JFrame, and call
setVisible(true);
in the ActionListener of your JMenuItem to make it visible when the menu is clicked.
If you want it to be modal (original frame cannot be accessed unless the new window is closed), you can use a JDialog instead, setting modal to true in the constructor, or calling setModal(true).
I write in answer for good code view:
If i use a JFrame i got this error:
"java.lang.IllegalArgumentException: adding a window to a container".
That's my code in actionPerformed method:
PopupFactory factory = PopupFactory.getSharedInstance();
JFrame frame = new JFrame();
frame.setLayout(null);
frame.setBounds(428, 99, 185, 155);
final JButton button = new JButton();
button.setText("Button");
button.setBounds(10, 93, 111, 25);
frame.getContentPane().add(button);
final Popup popup = factory.getPopup(null, frame, 200, 200);
popup.show();
You are confusing "popups" and "windows".
A popup is generally shown when you right click on some object. The popup will display a list of actions that can be performed on that object. For example a text field might have "cut", "copy" and "paste". Read the section from the Swing tutorial on "Bringing Up a Popup Menu" for more information.
A window is used to display other Swing components in a JFrame or JDialog.
Given that you are invoking this Action from a menu item I think you probably want to create and display a modal JDialog, not a JFrame or popup.
Also, while reading the tutorial, read the section on "Using Layout Managers". Using null layouts is not the best way to create a dialog.

Categories

Resources