1

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!

3
  • 1
    why are you using a null layout for your contentPane? Commented Feb 13, 2015 at 21:54
  • Ditto on @chancea's comment -- you should avoid null layouts if at all possible. Using them marks your program as a likely "newbie" program, since most folks who have created a lot of Swing GUI's know that it is easier to create and maintain GUI's that use layout managers. Also, you're not showing us the most important code -- where your button is supposed to make this class display. Just setting it visible isn't nearly enough -- you have to put it into a container that is ultimately held by a top level window such as a JFrame. Commented Feb 13, 2015 at 22:24
  • @ Hovercraft Full Of Eels Yes I am a newbie. I was able to open the GUI in question with the following line: WorkloadFactor.main(null); From there, a JFrame is created and set visible in the main method of WorkloadFactor. Can you give me a brief explanation what layout managers do? Thanks for your multiple replies. Commented Feb 15, 2015 at 4:29

3 Answers 3

2
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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);
 }  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.