2

When I launch my application, it launches the JFrame and loads up the JTabbedPane which contains the JScrollPane, yet it only shows one component inside it at a time. I have tried everything, and still I cannot solve the problem...

Here is my code:

package test;

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

public class Main extends JFrame{

public Main()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400,500);

    JPanel pane=new JPanel();
    pane.setLayout(new BorderLayout());
    UIManager.put("TabbedPane.contentOpaque", false);
    JTabbedPane tabbedPane=new JTabbedPane();
    JScrollPane scrollPane=new JScrollPane(pane);

    tabbedPane.setPreferredSize(new Dimension(getWidth(),getHeight()));

    for(int i = 0; i < 10; i++) pane.add(new JLabel("label22222222222222222222222222222222222222222222222222222222"+i));

   //pane.add(scrollPane,BorderLayout.CENTER);
    tabbedPane.add("Test",scrollPane);      
    add(tabbedPane);
}

public static void main(String[] args) {
    Main main=new Main();
    main.setVisible(true);
}
}

Please help me, I have no idea what I am doing wrong.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
gedr
  • 316
  • 1
  • 2
  • 12
  • 2
    BTW - remove `tabbedPane.setPreferredSize(new Dimension(getWidth(),getHeight()));` and `setSize(400,500);` & put `pack()` last.. – Andrew Thompson Jan 11 '14 at 14:27

1 Answers1

2

Your pane JPanel uses BorderLayout and you're adding components in a default fashion, or BorderLayout.CENTER. This is the expected behavior to show only the last component added.

You should consider using another layout such as GridLayout. Also, Google and read the "laying out components in a container" tutorial and understand the layouts that you're using.

Also, consider using a JList to display your data rather than a grid of JLabels.

As an aside, you should format your code for readability, not compactness. Don't put for loops on one line only. In fact all loops and blocks should go into curly braces to prevent your later editing your code, adding another line and thinking that it's in the loop when it's not.


Edit
For example, using a JList:

import javax.swing.*;

public class Main2 {
   private static final int MAX_CELLS = 30;

   private static void createAndShowGUI() {
      final DefaultListModel<String> listModel = new DefaultListModel<>();
      final JList<String> myList = new JList<>(listModel);
      myList.setVisibleRowCount(8);

      for (int i = 0; i < MAX_CELLS; i++) {
         listModel.addElement("label22222222222222222222222222222222222222222222222222222222" + i);
      }

      JTabbedPane jTabbedPane = new JTabbedPane();

      jTabbedPane.add("Test", new JScrollPane(myList));

      JFrame frame = new JFrame("Main2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(jTabbedPane);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks- help is much appriciated... I only started programing 6 months back so I am still learning! – gedr Jan 13 '14 at 11:05