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);
}
});
Related
I have my main JFrame and one more JDialog.
If user click on the button, i want JDialog to call method from this JFrame(which contains some operations on ComboBox in this JFrame).
How can I do that?
I don't want to use MyJFrame form = new MyJFrame(); because it will make a new JFrame which i don't want to do, i want to call method from JFrame which is running currently on my computer.
Thanks.
Assuming the JButton is on the JDialog.
If both are in the same class, why not just do this?
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//I just got clicked
form.doSomething();
}
});
which can be shortened to
button.addActionListener(e -> form.doSomething());
If they aren't, and you're extending JDialog (which I wouldn't recommend) just pass the JFrame in its constructor, then your dialog will have access to it.
It's not possible to add much more without seeing more of your code.
Maybe JOptionPane.showInputDialog() , show a JDialog to take input from User.
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
I am a new Java Programmer and I am tying to link two JFrames Windows together nut I don't know how , can I get some help please.
What I mean is that I made a button and I need the button to go to the next window,
but I don't know how to do so...
If you are trying to do a wizard then you may want to take a look to Creating Wizard Dialogs with Java Swing document. You would need only one JFrame (or JDialog) and several JPanel which will pass as you press "Next" button.
If you want open a new window then you can create and show a new JDialog within button action listener implementation. Something like this:
JButton nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setTitle("Title");
dialog.setModal(true);
dialog.getContentPane().add(...); // add components here
dialog.pack();
dialog.setVisible(true);
}
Suggested readings:
The Use of Multiple JFrames, Good/Bad Practice? (it's a bad practice)
How to Use CardLayout
How to Use Buttons, Check Boxes, and Radio Buttons
How to Use Modality in Dialogs
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:
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.