Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20646

Why Doesn't Parent Jpanel with two Jpanels only showing the first Show itself without its Two Child Jpanels?

I'm trying to create a JPanel with two more JPanels, however, at the time, when I'm going to show up the main JPanel, it only shows the first Jpanel on the main Jpanel.

With the following example, you'll understand the question better:

I want something like this:

Figure 1

The two different things that I want to do works fine; even so, it does not, if I try to put them on the same JPanel.

On the first JPanel, I want to add a JFileChooser, and in the other part, I want to add a Drag & Drop TextArea.

I'm trying to do the mentioned things in above with this bunch of code, and only shows the first Jpanel. What I'm missing?

I've created a Main Jpanel:

JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));

Then, I've created two Jpanels to put them inside of the Main Jpanel:

JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());

JButton button = new JButton("Selecciona el arxiu .txt");
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        JFileChooser fileChooser = new JFileChooser("C:/Users/Joan/Desktop/");
        int returnValue = fileChooser.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            SelectedFile = fileChooser.getSelectedFile();
            //Amb el SelectedFile.getName(); Sabem el nom del arxiu.
            BufferedReader br = null;

            try {
                String sCurrentLine;
                br = new BufferedReader(new FileReader(SelectedFile));

                while ((sCurrentLine = br.readLine()) != null) {
                    System.out.println(sCurrentLine);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
});
panel1.add(button);

And the second Jpanel looks like:

JPanel panel2 = new JPanel();
panel2.setBackground(Color.red);
panel2.add(
    new JLabel("Drop a list from your file chooser here:"),
    BorderLayout.NORTH
);
ta = new JTextArea();
ta.setBackground(Color.white);
panel2.add(ta, BorderLayout.CENTER);

dt = new DropTarget(ta, this);

At the time, I want to put those two Jpanels inside of the Main Jpanel. I do by this way. Then, I put it visible to show up:

container.add(panel1, BorderLayout.NORTH);
container.add(panel2, BorderLayout.SOUTH);
container.setVisible(true);

But, the problem is for when I try to launch the program, it only shows the first Jpanel (Jpanel1), and the other Jpanels don't.

What I'm doing wrong or what I've misunderstood?

Upvotes: 0

Views: 459

Answers (1)

Jack
Jack

Reputation: 21183

Instead of using a BoxLayout on your container you might want to try using a GridLayout instead.

container.setLayout(new GridLayout(0, 2));

then just this to add them:

container.add(panel1);
container.add(panel2);

Edit:

Full example:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Side {


    public static void main(String[] args) {

        JFrame frame = new JFrame("JFrame");
        JPanel container = new JPanel();
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        panel1.setBackground(Color.blue);
        panel2.setBackground(Color.red);

        container.setLayout(new GridLayout(0, 2));

        container.add(panel1);
        container.add(panel2);

        frame.getContentPane().add(container, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);

    }
}

Upvotes: 3

Related Questions