SaintLike
SaintLike

Reputation: 9589

Creating JTabbedPane in a JDialog and making the frame available

I'm working with GUI in Java and I've made several JDialogs opening one above the other. I tried to create a JTabbedPane and I have succeed. However, I have to make the JTabbedPane in a JFrame. I've tried but the JPanel opens all blank.

Second of all when I use JFrame (so the new JTabbedPane became operational) that same frame appears behind the previous one.

So my questions are:

Here's my code, this JFrame opened when I click on a JButton from a previous JDialog

public class AddComponents extends JDialog {    

    private String[] arr = {"House", "Microgrid", "CSP", "VPP"};

     public AddComponents(JDialog pai, String titulo)
    {
        super(pai, titulo);
        frame = new JFrame(titulo);

        // Display the window.

        frame.setSize(500, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        // set grid layout for the frame

        frame.getContentPane().setLayout(new GridLayout(1, 1));


        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        pack();

        for (int i = 0; i < arr.length; i++) {
            String tmp = arr[i];
            tabbedPane.addTab(tmp, makePanel(tmp));


        } 

        frame.getContentPane().add(tabbedPane);
        frame.setMinimumSize(new Dimension(getWidth(), getHeight()));
        frame.setLocation(pai.getX() + 85, pai.getY() + 25);
        frame.setEnabled(true);


    }

    private JPanel makePanel(String text) {
        JPanel p = new JPanel();
        //p.setLayout(new GridLayout(0,1));
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();


        if(text.equals("House"))
        { //CADA UM DOS ifs chama a class correspondente para criar o interface
          p1.setLayout(new GridLayout(4, 2));

          idLabel = new JLabel("Component ID:");
          idText = new JTextField(""); 
          p1.add(idLabel);
          p1.add(idText);



          maxUsageLabel = new JLabel("Max usage per hour:");
          maxUsageText = new JTextField("");
          p1.add(maxUsageLabel);
          p1.add(maxUsageText);

          minUsageLabel = new JLabel("Min usage per hour:");
          minUsageText = new JTextField("");
          p1.add(minUsageLabel);
          p1.add(minUsageText);

          averageUsageLabel = new JLabel("Average usage per hour:");
          averageUsageText = new JTextField("");
          p1.add(averageUsageLabel);
          p1.add(averageUsageText);

       //   emptyLabel = new JLabel("");
          saveButton = new JButton("Save");
         // p.add(emptyLabel);
          p2.add(saveButton);

          p.add(p1);
          p.add(p2);

        }
        if(text.equals("Microgrid"))
        {
            p.setLayout(new GridLayout(5, 2));
            outroLabel = new JLabel(" Microgrid");
            p.add(outroLabel);


        }
        if(text.equals("VPP"))
        {
            p.setLayout(new GridLayout(5, 2));
            outroLabel = new JLabel(" VPP");
            p.add(outroLabel);
        }
        if(text.equals("CSP"))
        {
            p.setLayout(new GridLayout(5, 2));
            outroLabel = new JLabel(" CSP");
            p.add(outroLabel);
        }

        return p;

    }


}

Upvotes: 1

Views: 1833

Answers (2)

Igor Rodriguez
Igor Rodriguez

Reputation: 1246

For creating the JDialog use:

 final JDialog dialog = new JDialog();
 dialog.add(tabbedPane);
 dialog.setVisible(true);

Java applications normally have only one JFrame, so nobody worries about the Z-order. If you like them, you can use JInternalFrame. Here is the tutorial. You can, however, use dialogs instead.

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

"How can I create the tabbed pane in a JDialog ?"

  • same as you would if you added it to a JFrame. There is essentially no difference here whatsoever.

"How do I make the JTabbedPane appear in front of all other frames, if I use JFrame ?"

  • you don't. You use a JDialog if you want to display a window above other windows.

Upvotes: 1

Related Questions