Question

I have a window with a JFileChooser and a JTextArea. The JFileChooser is in the NORTH part of the BorderLayout. The JTextArea is in the CENTER part of the BorderLayout.

I would like to align to the left ALL my JFileChooser, but it won't move like I want and stay CENTERED. Furthermore, I would like my JFileChooser take all the length of my window.

EDIT

Here is the main code

public class MainServer 
{
    public static void main(String[] args)
    {
        ServerBoard frame=new ServerBoard(1000, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
     }
}

Here is the window code

public class ServerBoard extends JFrame
{
    private JButton startserver;
    private JButton senddata;   
    private JButton sendgps;
    private JTextArea messagearea;

    public ServerBoard(int l, int h)
    {
        super("ServerBoard");
        this.initialize();
        this.setSize(l,h);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void initialize()
    {
        // Define a panel
        Container c=this.getContentPane();

        this.messagearea=new JTextArea(40,60);      

        c.add(this.createNorth(), BorderLayout.NORTH);
        c.add(messagearea, BorderLayout.CENTER);
    }

    public JPanel createNorth()
    {
        JPanel panelnorth=new JPanel();

        JToolBar toolbarnorth=new JToolBar();
        panelnorth.add(toolbarnorth);

        this.startserver=new JButton("START SERVER");
        startserver.addActionListener(new ServerBoardListener());
        toolbarnorth.add(startserver);

        this.senddata=new JButton("SEND DATA");
        senddata.addActionListener(new ServerBoardListener());
        toolbarnorth.add(senddata);

        this.sendgps=new JButton("SEND GPS FRAME");
        sendgps.addActionListener(new ServerBoardListener());
        toolbarnorth.add(sendgps);

        return panelnorth;  
    }
}

Here is my window

enter image description here

I really really want to use this JFileChooser. Can you help me please ?

In advance thank you a lot for the answers.

Was it helpful?

Solution

Simply nest your JPanels. Create a new JPanel, say called northPanel, that uses a BorderLayout, and add it to the main window in the BorderLayout.NORTH position, and then add your JFIleChooser to this northPanel JPanel in its BorderLayout.WEST position.

Option 2: give the northPanel a BoxLayout that is oriented along the line axis, add the JFileChooser, and add glue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top