Setting my component in specified place

What I want to do is a put my component close to the right side of my application. Second option is to put it in specified x,y coordinates. However I tried setBounds() setLocation() and add(component, BorderLayout.LINE_END) funcions. Nothing works for me. What is wrong is with my code?

public class MapaSwiata extends JPanel implements ActionListener {

    private BufferedImage mapa;
    private File imageFile;

    public MapaSwiata(){
        super();

        imageFile = new File("C:\\Users\\Katie\\Documents\\Eclipse\\Samolot\\src\\Pics\\img_mapa.jpg");

        try {
            mapa = ImageIO.read(imageFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JOutlookBar outlookBar = new JOutlookBar();

        outlookBar.addBar( "Pogoda", getDummyPanel("One"));
        outlookBar.addBar( "Informacje o locie", getDummyPanel("Two" ));
        outlookBar.addBar( "Rozkład Lotow", getDummyPanel( "Three" ) );
        outlookBar.setVisibleBar( 2 );

        add(outlookBar, BorderLayout.LINE_END); 
    }

    public static JPanel getDummyPanel( String name )
    {
        JPanel panel = new JPanel( new BorderLayout() );
        panel.add( new JLabel( name, JLabel.CENTER ) );
        return panel;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(mapa, 0, 0, getWidth()-300, getHeight()-150, null);
    }
}

I also tried to put in a JButton or something else. Everything always appears in the top center of the application. Sorry for polish names of variables.

Thanks in advance

Answers


If you want that BorderLayout.LINE_END has an effect, you have to apply the BorderLayout to the Container. To do that, simply add

setLayout(new BorderLayout());

to your constructor MapaSwiata().

NOTE: Your child panels don't require the BorderLayout.


Need Your Help

ShieldUI filtering when using remote data

gridview grid shieldui

The documentation is kind of sparse on using certain functionality on the ShieldUI datasource controls as far as filtering is concerned.

CSV Helper parser throw IOException. Unable to read data from the transport connection

c# csv azure

I'm using CSVHelper in my c# project and reading a big csv data file (about 2000 records) into memory.