This is an interesting problem that didn't show up when I built the last JDialog, even though that one was way more complex than this. Anyways here is the code that causes the problem.
public class Test extends JDialog{
private final JButton cancel, ok;
private final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 50, 5));
public Test(JFrame parent) {
//Initialize the JDialog
super(parent, "Select Chapters");
setLayout(new BorderLayout());
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(JDialog.ModalityType.APPLICATION_MODAL);
setSize(300, 300);
setLocationRelativeTo(null);
cancel = new JButton("Cancel");
ok = new JButton("OK");
buttonPanel.add(cancel);
buttonPanel.add(ok);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
cancel.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
dispose();
}
});
}}
And the main method
Test test = new Test(new JFrame());
I put the event listener in the constructor for testing but the actual implementation is supposed to happen in another class. Which is why this is a problem. If I put the action listener before setVisible(true), then everything works as it should. But I can't do that since the event handler is to be implemented in another class. What is causing this problem and how do I resolve it?
Event handler in JDialog not working if placed after setVisible(true)
Correct, because the JDialog is modal, so the statement after the setVisible() is not executed until the dialog is closed.
There is no reason you can't add the ActionListener to the button before the dialog is made visible. It doesn't matter that the code is in a separate class. You just create an instance of that class in the constructor of the class where you create the button.
I put setVisible() in the other class after I implemented the listeners
You are still doing something wrong. The setVisible() should be in your main class where you set the dialog properties and create all the components and add the component to the dialog.
I'm not sure why you are doing. Your code can be something like:
cancel = new JButton("Cancel");
cancel.addActionListener( new SomeActionListenerClass() );
...
setVisible( true );
You need to place setVisible(true) at the end of your constructor because the dialog is modal and in that case setVisible(true) will not return until you call setVisible(false).
Quoting Java doc:https://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.html#setVisible(boolean)
Notes for modal dialogs.
setVisible(true): If the dialog is not already visible, this call will
not return until the dialog is hidden by calling setVisible(false) or
dispose.
setVisible(false): Hides the dialog and then returns on
setVisible(true) if it is currently blocked.
It is OK to call this
method from the event dispatching thread because the toolkit ensures
that other events are not blocked while this method is blocked.
Related
I got the problem that my buttons are not working. I have used JButtons before and hadn't had a problem with them before. Visually, the program looks as intended.
Can someone tell me why the button is not working? The class uses JDialog.
JButton cancel;
public CodeExample() {
setLayout(new FlowLayout(FlowLayout.RIGHT));
add(cancel = new JButton ("Cancel"));
setAlwaysOnTop(true);
setModal(true);
setVisible(true);
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("test");
}
});
}
As Roddy of the Frozen Peas already pointed out, the last thing you should do is to make the dialog visible.
The problem here are those two lines:
this.setModal(true);
this.setVisible(true);
If a dialog is modal, then setVisible will block until the dialog is not longer visible or disposed.
This means that everything after setVisible is executed after the user clicks on the red X to close the window. But at this point the dialog is not longer visible and you don't show the dialog again.
public class Create_JFrame extends JFrame{
public Create_JFrame(){
//Create a Frame
JFrame Frame = new JFrame("Bla-Bla");
JPanel Panel_1 = new JPanel();
JPanel Panel_2 = new JPanel();
JButton Option_1 = new JButton("Option-1");
//Layout management for Panels
Frame.getContentPane().add(BorderLayout.WEST, Panel_1);
//Add button to Panel
Panel_1.add(Option_1);
//Registering Listeners for all my buttons
Option_1.addActionListener(new ListenerForRadioButton(Panel_2));
//Make the frame visible
Frame.setSize(500, 300);
Frame.setVisible(true);
}//end of Main
}//end of Class
public class ListenerForRadioButton implements ActionListener{
JPanel Panel_2;
JButton browse = new JButton("Browse");
//Constructor, will be used to get parameters from Parent methods
public ListenerForRadioButton(JPanel Panel){
Panel_2 = Panel;
}
//Overridden function, will be used for calling my 'core code' when user clicks on button.
public void actionPerformed(ActionEvent event){
Panel_2.add(browse);
System.out.println("My listener is called");
}//end of method
}//end of class
Problem Statement:
I have 2 JPanel components in a a given JFrame. Panel_1 is having a Option_1 JButton. When user clicks on that I am expecting my code to add a JButton 'browse' in Panel_2 at runtime.
Runtime Output:
System is not adding any JButton in Panel_2. However, I see my debug message in output, indicating that system was successful in identifying user's click action on 'option-1'.
Question:
Why is JPanel not adding any component at Runtime?
Panel_2.add(browse);
Panel_2.revalidate();
adding a 'revalidate' will solve the problem.
There are some reasons. but:
usually it's because of using unsuitable LayoutManager.
sometimes it's because of adding the JPanel to it's root component in worng way. which any operation (add, remove,...) works but is not visible.
you must refresh the view when you make some changes on it, like adding or removing components to/from it.
try to use Panel_2.revalidate() to refresh.
if it doesn't work properly use it with Panel_2.repaint() method.
see Java Swing revalidate() vs repaint()
see Difference between validate(), revalidate() and invalidate() in Swing GUI
using setSize twice for your jframe is another way.
Frame.setSize(498, 300); then Frame.setSize(500, 300);
Currently I am calling a method (showFrames) which pops up a JFrame which contains many editable text fields. I am storing the value of these text fields in a list (editedFields) which I need to use in the calling method. My issue is that my calling method is not waiting for the user to select ok/cancel before continuing so the list is not populated when I am trying to take action on it. I tried to overcome this by using a modal dialog to no avail. the method is being called here...
...
showFrames(longToShortNameMap);
if (editedFields != null) {
for (JTextField field : editedFields) {
System.out.println(field.getText());
}
}
...
and the showFrames method is implemented as:
private static void showFrames(Map<String, String> longToShortNameMap) {
final ToolDialog frame = new ToolDialog("Data Changed");
frame.setVisible(true);
frame.setModal(true);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400, 500);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel(new GridLayout(0, 2));
JPanel buttonPanel = new JPanel(new GridLayout(2, 0));
List<String> keys = new ArrayList(longToShortNameMap.keySet());
final List<JTextField> textFields = new ArrayList<>();
for (String key : keys) {
JLabel label = new JLabel(key);
JTextField textField = new JTextField(longToShortNameMap.get(key));
panel.add(label);
panel.add(textField);
textFields.add(textField);
}
JButton okButton = new JButton("OK"); //added for ok button
okButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
editedFields = textFields;
frame.setVisible(false);
frame.dispose();
}
});
JButton cancelButton = new JButton("Cancel");//added for cancel button
cancelButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
frame.dispose();
}
});
okButton.setVisible(true);//added for ok button
cancelButton.setVisible(true);//added for cancel button
buttonPanel.add(okButton);//added for ok button
buttonPanel.add(cancelButton);//added for cancel button
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setVisible(true);
scrollPane.setSize(500, 500);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.SOUTH);
}
the current behavior I observe is that when the JFrame pops up, all the fields will immediately print out instead of waiting for the user to click "OK". Effectively this means I am receiving the default values in the text fields instead of the edited values.
Note: ToolDialog extends JDialog
The basic problem that you have is that you are instantiating the Dialog first, making it visible, and then adding fields to it.
That is essentially incorrect. All objects should be added to it while you are instantiating the Frame/Dialog, preferably in the constructor call. Then, you make it visible when everything is ready.
Of course, you can add a new field to the frame after showing it already, but that is typically done based on some event, for example, when user clicks "Add a new number", then you add new text fields, etc to it.
So, the fix for you is simple, move the logic that adds the buttons, the lists, the panels etc, to the constructor, and then make that window visible.
You have 2 different issues here :
Waiting for a dialog.
Displaying the dialog correctly.
1.- Waiting for a dialog.
You should use a JDialog instead of a JFrame to make the window modal.
The window is not modal because you are showing it before setting it to modal. See JDialog.setModal :
Note: changing modality of the visible dialog may have no effect until
it is hidden and then shown again.
You need to switch theese two lines :
frame.setVisible(true);
frame.setModal(true);
An alternate way is to synchronize with a countdown latch:
CountDownLatch latch = new CountDownLatch(1);
.......
showFrames(longToShortNameMap);
latch.await(); // suspends thread util dialog calls latch.countDown
if (editedFields != null) {
.......
/// Dialog code
latch.countDown(); // place it everywhere you are done with the dialog.
dispose();
2.- Displaying the dialog correctly.
Place frame.setVisible(true) as the last line of showFrames.
I am trying to open a GUI from my main GUI by pressing a button. When the button is pressed, this is executed:
WorkloadFactor wf = new WorkloadFactor();
wf.setVisible(true);
This doesn't open the WorkloadFactor GUI. I am confused by this because I have other GUIs that open this way without issue.
WorkloadFactor class works fine when I run it by itself but won't open when it is called by my main GUI. Below is my class without imports and stuff:
public class WorkloadFactor extends JPanel {
public WorkloadFactor() {
setLayout(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
String[] tabnames = { "Zero", "One", "Two", "Three", "Four" };
for (int i = 0; i < tabnames.length; i++) {
tabbedPane.addTab(tabnames[i], createPane(tabnames[i]));
}
tabbedPane.setSelectedIndex(0);
JButton submit = new JButton("Submit All");
submit.setForeground(Color.RED);
add(tabbedPane, BorderLayout.CENTER);
add(submit, BorderLayout.SOUTH);
}
public JPanel createPane(final String t) {
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//setContentPane(contentPane); I think this might be it?
contentPane.setLayout(null);
setBounds(100, 100, 531, 347);
//***** all the components I am including then add them like so
//******contentPane.add(checkbox1);
//****** contentpane.add(label1);
return contentPane;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Set Workload Factor Information");
frame.getContentPane().add(new WorkloadFactor());
frame.setBounds(100, 100, 531, 347);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
I have tried arranging things in so many ways, putting everything in the constructor and other changes but can't seem to find a reason why instantiating this WorkloadFactor class elsewhere and setting it visible won't work.
Does it have something to do with setContentPane(contentPane) vs contentPane.add(xxxx) and returning it?
Thank you for reading!
WorkloadFactor wf = new WorkloadFactor();
wf.setVisible(true);
To be blunt, this won't display anything. Please understand that WorkloadFactor extends JPanel and like all non-top level components must be placed into a container that is ultimately held by a top-level window in order to be displayed. Look at how you display it in your main method -- you first put it into a JFrame, and then display that JFrame. You must do the same thing if you want to display it on button press -- you need to put it into a JPanel or other container that is held by a JFrame or JDialog, or JOptionPane.
Make sure that you have properly registered the button on your main GUI which opens WorkLoadFactor GUI to an action listener.
Since you have not included code from your main GUI I can't confirm this is the issue. However it is a commonly overlooked issue.
Heres some suggestions from the Java documentation tutorials:
"Problem: I'm trying to handle certain events from a component, but the component isn't generating the events it should.
First, make sure you registered the right kind of listener to detect the events. See whether another kind of listener might detect the kind of events you need.
Make sure you registered the listener on the right object.
Did you implement the event handler correctly? For example, if you extended an adapter class, then make sure you used the right method signature. Make sure each event-handling method is public void, that the name spelled right and that the argument is of the right type."
source: Solving Common Event-Handling Problems
Make a JFrame and add a JButton in it than add action listener in button and add this code in it like this:
This code makes a frame with a button and when button is pressed new window is opened.
public class Example extends JFrame {
public Example() {
super("Title");
setLayout(new FlowLayout());
JButton b = new JButton("Open new Frame");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
newWindow nw = new newWindow();
}
});
add(b);
}
}
newWindow Code:
public class newWindow extends JFrame {
newWindow() {
super("title");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setVisible(true);
}
}
I have class main extends jframe, it has a button that calls /shows another class that extends jdialog.
If the button from jdialog is triggered, it will dispose that dialog and will remove all component of jframe, then add it to a new jpanel.
What should I do?
Here's my new broken code:
public class mainz extends JFrame{
mainz(){
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JToolBar r = new JToolBar();
r.add(Box.createHorizontalGlue());
add(r, BorderLayout.NORTH);
JButton n = new JButton();
r.add(n, BorderLayout.EAST);
n.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
show();
}
});
}
public void show(){
dialogz d = new dialogz(this);
d.setVisible(true);
}
public void lastHope(){
getContentPane().removeAll();
getContentPane().validate();
getContentPane().repaint();
}
public static void main (String[]args){
new mainz().setExtendedState(MAXIMIZED_BOTH);
}
}
public class dialogz extends JDialog{
public dialogz(final mainz owner) {
setSize(300, 300);
JButton n = new JButton("execute");
add(n);
final JFrame ew = (JFrame)super.getOwner();// <<
n.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
dispose();
//owner.lastHope;
ew.removeAll();// <<
ew.validate();// <<
ew.repaint();// <<
}
});
}
void yes(){
getOwner().removeAll();
getOwner().validate();
getOwner().repaint();
}
}
I know I can prevent my main class from extending jframe, and call it from main instead, but I want to do it like that...
Please help me ... T-T
Sorry for my English, I from a far away country ~,~"
update:
the error is
java.lang.ClassCastException: javax.swing.SwingUtilities$SharedOwnerFrame cannot be cast to javax.swing.JFrame
it will be done with delete the line that contain
// <<
then call lastHope();
but i think there's a another way to get that existing jframe to removeall
(by casting it first or something ~,~" )
You are calling getParent() but you never set the parent (or owner). That should happen in the constructor as already pointed out. Also, be mindful that getParent() returns a Container object and getOwner() returns a Window object. Both of these refer to the JFrame which is the parent and owner. If you want to use it as a JFrame, you'll have to cast the output as (JFrame). But removeAll() is in Container class so if that's all you want, there'll be no need for casting.
Update:
JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame);//frame is owner
JFrame parentOfDialog = (JFrame)(dialog.getParent());
//OR
//JFrame parentOfDialog = (JFrame)(dialog.getOwner());
parentOfDialog.removeAll();
If you are using your custom class, pass JFrame in the constructor and call super.
Please read the javadoc on JDialog before you try to use it. Also, read more about inheritance.
I'm not clear on what your goal is, but if you want to change the components that are displayed in a container, such as a JFrame or JDialog's contentPane, then I recommend that you use a CardLayout to do this since it allows you to easily swap "views".
There could be two ways to do this:
Your JDialog class could use a reference to the JFrame that is passed in via its constructor (and you should then pass it immediately into the dialog's super constructor so that your modality will work correctly). You could then call any public methods in the JFrame's class.
Or since the JDialog is modal, the JFrame's code will halt while the dialog is visible. You could swap "views" immediately after the dialog has been disposed of and is no longer visible. this would keep the JFrame manipulating code in the JFrame class.
Edit: note that if you don't use CardLayout, then you're responsible for calling revalidate() and repaint() on any container who gets its components changed.
As an aside: since English is not your first tongue and nor is it the native language of many folks on this forum, please avoid using non-standard abbreviations. The clearer your communication with us, the easier it will be for us to understand you and help you.