Reputation: 845
It's my first time working on Swing. Probably I am doing something seriously wrong.
I have one tabbed UI where each tab is JPanel
(this tab is added to JTabbedPane
). I add the number of JTextField
s in that JPanel
based on user input. I need that JPanel
to be scrollable. I have tried following solution:
Jscrollpane
to Tab
Jpanel innerPanel
to JscrollPane
innerPanel
There are no textFields displayed.
public Tab extends JPanel {
private TipTailoringTab() {
JPanel innerPanel =new JPanel();
int y_cord = 20;
for (int i = 0; i < USER_INPUT; i++) {
JTextField TextField = new JTextField();
TextField.setBounds(42, y_cord, 100, 20);
innerPanel.add(TextField);
y_cord = y_cord + 40;
}
Dimension preferredSize = new Dimension(400, 600);
innerPanel.setPreferredSize(preferredSize);
JScrollPane sPane = new JScrollPane(innerPanel);
Dimension preferredSize1 = new Dimension(400, 300);
sPane.setPreferredSize(preferredSize1);
this.add(sPane);
}
}
Any Help is much appreciated.
EDIT
for every loop I create One TextField, one Slider, On Label.
I want the following structure vertically scrollable when n is large
TextField1 Slider1 Label1
TextField2 Slider2 Label2
TextFieldn Slidern Labeln
Upvotes: 0
Views: 2364
Reputation: 403
Here's a very simple example. I set the preferred sizes so that it forces the scroll bars to become visible/needed. Any questions just ask.
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
public class Test {
private static final int USER_INPUT = 10;
public Test() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JTabbedPane tabs = new JTabbedPane();
tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
for (int i = 0; i < 5; i++) {
tabs.addTab("Tab"+i, new TabPanel());
}
frame.add(tabs);
frame.pack();
frame.setVisible(true);
}
class TabPanel extends JPanel {
public TabPanel() {
JPanel innerPanel = new JPanel();
innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));
for (int i = 0; i < USER_INPUT; i++) {
JPanel p = new JPanel(new BorderLayout());
JLabel label = new JLabel("Label"+i);
JTextField textArea = new JTextField();
p.add(label, BorderLayout.NORTH);
p.add(textArea, BorderLayout.CENTER);
innerPanel.add(p);
}
JScrollPane scrollPane = new JScrollPane(innerPanel);
scrollPane.setPreferredSize(new Dimension(400, 200));
this.add(scrollPane);
}
}
public static void main(String[] args) {
new Test();
}
}
Upvotes: 1