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
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 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);
}
});
In Swing for Java 1.5 I want to create a dialog, which allows the user to do other things in background. I want this dialog to have one button, with which you can close the dialog. If the user doesn't close the dialog within X seconds, it should close itself. In both cases a routine has to be done after the dialog has been closed.
I tried to use Swing Timer with a modal dialog and it works. But, as I noticed above, I need a non-modal dialog. When I set the modal-Property to false, the dialog disappears immediately.
Does someone know, why this happens?
JOptionPane pane = new JOptionPane (text, JOptionPane.WARNING_MESSAGE);
pane.setOptions(new String[]{"Close"});
final JDialog dialog = pane.createDialog(frame, title);
//dialog.setModal(false);
Timer timer = new Timer(time, new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
timer.setRepeats(false);
timer.start();
dialog.setVisible(true);
//routine to do after the dialog disappears
for better help sooner post an SSCCE, there no code or descriptions about MultiThreading
don't create final JDialog dialog = pane.createDialog(frame, title); on runtime, create this JDialog one time and re_use that for another action, and / or by removing all childs from ContentPane
override proper event from WindowListener, there you can invoke your custom code before dipose(), setVisible() e.i.
(to point 2nd.) simply to override JDialog#setDefaultCloseOperation to HIDE_ON_CLOSE
all code that invoking a new Top-Level Container on runtime must be wrapped into invokeLater(), especially setVisible(true)
use Application Inactivity by #camickr
In this example, a modeless JDialog containing a direct JOptionPane counts down to zero before closing. A nearby JFrame containing a label remains responsive.
Addendum: As #mKorbel helpfully comments, your class can use a PropertyChangeListener to learn when the dialog's option pane closes. This is a convenient feature of JOptionPane, but you can add your own support, as shown here.
I have a simple problem with opening a new JFrame called focalSumFrame while clicking on JButton called focalSum. I am working in netBeans IDE. My code looks like this:
private void focalSumActionPerformed(java.awt.event.ActionEvent evt) {
focalSumFrame.pack();
focalSumFrame.setVisible(true);
}
It's more efficent and easier to use JDialogs and/or CardLayout for alternative pages of your program. Creating other JFrames to use as your pop-ups is a very ineffective way to go about what you're trying to do.
JDialog Information
Card Layout Information
I wrote a class that extends JWindow that serves as a kind of customizable dialog box in my application. When I need to invoke one of these windows, I create a new instance of the class; to remove the window, I call the method dispose().
The problem I am having is that the user cannot edit components that have a text box, such as JTextField and JSpinner. The user can click on components such as drop-down boxes and buttons, and this works fine, but when it comes to entering text in a text box, this does not work.
Has anyone else experienced this problem?
Thanks!
There are a bunch of conditions to meet until a window child can receive focus, see the api doc for window.isFocusableWindow().
for most contexts it's enough to set its focusableWindowState property to true, like
JFrame owner = new JFrame();
owner.setVisible(true);
JWindow window = new JWindow(owner);
window.setFocusableWindowState(true);
window.add(new JTextField("edit me"));
window.setSize(200, 200);
window.setVisible(true);
1) maybe you are mixing AWT with Swing drop-down box and button, are you sure that all Components definitions starts with J, drop-down boxes == JComboBox, Button == JButton etc
2) don't create lots of Top-Level Containers on the Fly/Runtinme
3) Has anyone else experienced this problem? no, never
4) for real and better help sooner, please edit you post and sent here code that demonstrate your problem here are simples rulles sscce