JohnnyGat
JohnnyGat

Reputation: 335

Java swing diffrent layouts on the same component

I've got a bit of a problem. I need to use two diffrent types of layouts on diffrent tabs in JTabbedPane component.Here's the code:

MyPanel.java

 import java.awt.*;
 import javax.swing.*;
 import java.util.*;

 public class myPanel extends JPanel{
    myPanel(){
        super(new GridLayout(1, 1));

        JTabbedPane tabbedPane = new JTabbedPane();
        myYear year = new myYear();
        myDate mydate = new myDate();
        String s_year = Integer.toString(myDate.year);
        tabbedPane.addTab(s_year,year);
        myMonth month = new myMonth();
        tabbedPane.addTab(mydate.getMonth(),month);


        add(tabbedPane);

        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }


 }

GridLayout() on year-pane is necessary because it's displaying 4x4 squares (for each month). But when it comes to month-pane I need to use JList component and I want to put it on the "west border". I've tried to do this like :

 package Lista8;

 import java.awt.*;
 import javax.swing.*;

 class myMonth extends JPanel{

    myMonth(){
        String[] data = {"oneone"};
        JList month = new JList(data);
        month.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        month.setLayoutOrientation(JList.VERTICAL_WRAP);
        month.setVisibleRowCount(5);
        month.setPreferredSize(new Dimension(100,200));
        add(month,BorderLayout.WEST); // HERE
    }
 } 

But month-panel keeps stucking in the center. To be honest I don't quite understand how layouts works

Upvotes: 1

Views: 48

Answers (1)

camickr
camickr

Reputation: 324147

add(month,BorderLayout.WEST); // HERE

The default layout manager of a JPanel is a FlowLayout.

If you want to use a BorderLayout, then you need to set the layout of the panel to a BorderLayout.

myMonth()
{
    setLayout( new BorderLayout() );

    String[] data = {"oneone"};
    ...

To be honest I don't quite understand how layouts works

Start by reading the Swing tutorial on Layout Managers. You will find working code for each layout manager you can download and play with to learn the basics.

Upvotes: 6

Related Questions