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' ? ...
Edit: Don't extend
JScrollPane
, it is not necessary here.You can:
JPanel
JPanel
toJScrollPane
throughJScrollPane(Component view, int vsbPolicy, int hsbPolicy)
JScrollPane
toJTabbedPane
For example: