How can i add JScrollPane inside a JTabbedPane's Tab

2.1k views Asked by At

I want to wrap a 'Tab' inside a JTabbedPane with JScrollPane.But i can't find a way to do it.This is my code so far ...

"HomeDelivery.java"

import java.sql.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.LineBorder;

class HomeDelivery extends JFrame
{
JTabbedPane menu;
JPanel pizza,pastry,drinks;

public HomeDelivery()
{
    setSize(800,800);
    setVisible(true);
    setLayout(new BorderLayout());

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    menu = new JTabbedPane();

    menu.addTab("Burger",new Burger());

    pizza = new JPanel();
    menu.addTab("Pizza",pizza);

    pastry = new JPanel();
    menu.addTab("Pastry",pastry);

    drinks = new JPanel();
    menu.addTab("Drinks",drinks);

    add(menu);
}

public static void main(String args[])
{
    HomeDelivery h = new HomeDelivery();
}
}

"Burger.java"

import java.sql.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.LineBorder;

class Burger extends JPanel
{
JFrame f;
//JPanel s;
JPanel p[];
JLabel image[];
JLabel name[];
JLabel price[];
JButton b[];
JScrollPane scroll;
int row;

public Burger()
{   
    p = new JPanel[40];
    image = new JLabel[40];
    name = new JLabel[40];
    price = new JLabel[40];
    b = new JButton[40];

    for(int i=0;i<40;i++)
    {
        p[i] = new JPanel();
        p[i].setLayout(new BoxLayout(p[i],BoxLayout.LINE_AXIS));
        p[i].add(Box.createRigidArea(new Dimension(100,50)));
        image[i] = new JLabel("Image");
        b[i] = new JButton("ADD");
        name[i] = new JLabel("Burger");
        price[i] = new JLabel("150");

        p[i].add(image[i]);
        p[i].add(name[i]);
        p[i].add(price[i]);
        p[i].add(b[i]);

        add(p[i]);
    }

    scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setPreferredSize(new Dimension(100,50));
    add(scroll);
}
}

At the last of the portion of "Burger.java",i tried to add a JScrollPane,though i know it is not the right way i did it . If i was not extending JPanel in "Burger.java" , i'll able to add a scrollpane by doing this : "JScrollPane scroll = new JScrollPane(JPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);"

But,how can i do it in "Burger.java" now ? How can i add a JScrollPane in my "Burger" 'Tab' ? ...

2

There are 2 answers

1
MasterBlaster On BEST ANSWER

Edit: Don't extend JScrollPane, it is not necessary here.

You can:

For example:

import java.awt.EventQueue;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class TabExample {

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            final JFrame frame = new JFrame("Example");
            final JTabbedPane tp = new JTabbedPane();
            final BurgerTab burgerTab = new BurgerTab();
            final JScrollPane scrollPane = new JScrollPane(burgerTab, 
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            tp.addTab("Burgers", scrollPane);
            frame.getContentPane().add(tp);
            frame.pack();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        });
    }

    private static class BurgerTab extends JPanel {

        BurgerTab() {
            setLayout(new BoxLayout(this, SwingConstants.VERTICAL));
            final JPanel[] burgers = new JPanel[40];

            for(int i = 0; i < burgers.length; i++) {
                burgers[i] = new JPanel();
                burgers[i].add(new JLabel("Burger #" + (i + 1)));
                add(burgers[i]);
            }
        }

    }

}
6
Timothy Truckle On

First stop for problems like this is the public Java API documentation. There you would find another constructor of JScrollPane:

http://docs.oracle.com/javase/8/docs/api/javax/swing/JScrollPane.html#JScrollPane-java.awt.Component-int-int-

According to this your problem could be solved by:

scroll = new JScrollPane(p,  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);