How to make JScrollPane dynamic adjusting - java

I am trying to make a dynamic tabs with the option to add new ones in the application and some buttons next to it... To do so I have a main class:
private static void initAndDisplayUI() {
frame = new JFrame(...)
tabbedPane = new TabbedPane();
insertTab(tabbedPane, TabFactory.createTab(), true);
insertNewTabButton(tabbedPane);
...
}
}
Container class:
public class TabbedPane extends JPanel {public TabbedPane() {
this.captions = new TabCaptions();
this.tabs = new ArrayList<Tab>();
this.contentContainer = new JPanel(new BorderLayout());
setLayout(new BorderLayout());
add(captions, BorderLayout.NORTH);
add(contentContainer, BorderLayout.CENTER);
}
...
}
And a TabCaptions:
public class TabCaptions extends JPanel {
private TabCaption selectedTab;
private JComponent tabsPane;
private JScrollPane scrollPane;
private JPanel buttonsPane;
public TabCaptions() {
createUI();
}
private void createUI() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBackground(Color.DARK_GRAY);
add(createTabsPane());
add(createButtonsPane());
add(Box.createHorizontalGlue());
}
private JComponent createTabsPane() {
tabsPane = new JPanel();
tabsPane.setOpaque(false);
tabsPane.setLayout(new BoxLayout(tabsPane, BoxLayout.X_AXIS));
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return tabsPane;
}
...
}
As a result I have a region with the tabs and some button next to it. However the app window draws a scrollPane with some weird size... I would like to display this "add new tab" button right next to the tabs that are created, resize it when the new tab is being added BUT with the functionality to display a scrollbar once it hits the maximum window width. I already have the scrolling but how can I make the behaviour of this dynamic position of new page?

I already have the scrolling
Not based on the code you have provided:
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return tabsPane;
You need to add the JScrollPane to the frame, so you need to return the scroll pane from the method:
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//return tabsPane;
return scrollPane;

Related

Scrollpane in multi-panel tab of JTabbedpanes

Good day all,
I have a JTabbedPane of which each tab has its own class. Each of its ActionListener also has it's own seperated class.
One ActionPerformed happens to add a JTable with columns after a certain amount of days is set.
This is one tab:
public class FirstTab {
private JPanel mainPane;
private JPanel weeksPane;
private JButton setWeeksB;
private JLabel matchCheck;
private JPanel modulesPane;
public FirstTab() {
mainPane = new JPanel();
weeksPane = new JPanel();
setWeeksB = new JButton("Voeg weken toe");
matchCheck = new JLabel();
//mainPane.setLayout(new FlowLayout());
mainPane.add(createSchoolDaysPane());
}
public JPanel createSchoolDaysPane() {
JPanel schoolDaysPane = new JPanel();
DefaultTableModel weeksModelTable = new DefaultTableModel();
weeksModelTable.addColumn("Week nummer");
weeksModelTable.addColumn("Dagen");
JTable weeksTable = new JTable(weeksModelTable);
JLabel schoolDaysL = new JLabel("Geef hier het aantal schooldagen (max 200) van het jaar in: ");
JTextField schoolDaysF = new JTextField(10);
SetWeeksPaneAction AWFA = new SetWeeksPaneAction(mainPane, weeksPane, weeksModelTable, weeksTable, matchCheck, setWeeksB);
schoolDaysF.addActionListener(AWFA);
JLabel schoolDaysGivenL = new JLabel();
schoolDaysPane.setLayout(new BoxLayout(schoolDaysPane, BoxLayout.Y_AXIS));
schoolDaysPane.add(schoolDaysL);
schoolDaysPane.add(schoolDaysF);
schoolDaysPane.add(schoolDaysGivenL);
return schoolDaysPane;
}
public JPanel getTab() {
return mainPane;
}
This tab contains two panels, the JTable shown is added to the panel after the amount of days is set.
GUI
As you notice the columns are not visible (but they are set), so I have been told to use a ScrollPane instead.
However, I am not able to pass through a ScrollPane in my JTabbed because a normal pane ('MainPane' - container) is passed through:
public EindopdrachtJava3() {
frame = new JFrame();
jtp = new JTabbedPane();
firstTab = new FirstTab();
secondTab = new SecondTab();
jtp.addTab("Modules", firstTab.getTab());
jtp.addTab("Samenvatting schooljaar", secondTab.getTab());
frame.getContentPane().add(jtp);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(640, 480);
frame.setLocationByPlatform(true);
frame.setVisible(true);
modules = new HashSet<Module>();
weeks = new HashSet<Week>();
}
public static void main(String[] args) {
new EindopdrachtJava3();
}
This tab contains multiple panels and gets added to my JFrame in the Main class
JFrame {
JTabbedPane {
Tab 1 {
Panel 1
ScrollPane(JTabel) - But not possible(?)
}
Tab 2 {
....
}
}
I would like to keep it modular as possible since that is currently my learning curve in GUI

Why does the JScrollPane not appear around my JTextField?

I'm pretty new to GUI but I'm trying to create a simple version of notepad and would like scroll bars to appear around the text area. However, I'm not sure why it isn't appearing.
public class NutPad extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("NutPad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new NutPad(), BorderLayout.CENTER);
frame.setSize(500,300);
frame.setVisible(true);
}
private NutPad() {
add(makeTextAreaPanel());
}
private JPanel makeTextAreaPanel() {
JPanel textAreaPanel = new JPanel();
textAreaPanel.setSize(100,100);
JTextArea textArea = new JTextArea(20, 60); //15,43
JScrollPane scrollPane = new JScrollPane(textArea);
textAreaPanel.add(scrollPane,BorderLayout.CENTER);
textAreaPanel.add(textArea);
return textAreaPanel;
}
}
Thanks
If you're going to use the BorderLayout.CENTER constraint, then the container needs to have its layout set to BorderLayout.
Also you don't need textAreaPanel since you can just add the scrollPane straight into your NutPad panel.
private NutPad() {
setLayout(new BorderLayout());
add(makeScrollPane(), BorderLayout.CENTER);
}
private JScrollPane makeScrollPane() {
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
return scrollPane;
}
Now your text area will fill the frame and the scrollbars will appear when the text takes up more than the available space.
Hope that helps :)

JScrollPane does not show to JTextarea

I am trying to add a scrollbar to my JTextarea but the scrollbar does not show up neither my Jtoolbar
can anyone please tell me what is wrong with this code. so that i can fix it. I have been looking eeverywhere but The scrollpane stil does not show up
public PlayerGui() {
// create main windows
super("Liste");
JTextArea editors = new JTextArea();
editors.setLineWrap(true);
editors.setWrapStyleWord(true);
// scroll bar
JScrollPane scroll = new JScrollPane(editors);
setEditor(editors);
// create center panel
JPanel cent = new JPanel();
//create Panel for the to
JPanel north = new JPanel();
setNorthpanel(north);
// create tool bar
JToolBar toolbar = new JToolBar();
toolbar.add(scroll);
// set center panel and add preferred layout and backgrounds and size
setCenter(cent);
getCenter().setLayout(new BorderLayout());
// add scroll bar and toolbar
add(scroll, BorderLayout.EAST);
add(toolbar, BorderLayout.SOUTH);
//getCenter().setBackground(Color.black);
Dimension size = new Dimension(getCenter().getPreferredSize());
getEditor().setPreferredSize(size);
getCenter().getPreferredSize();
getCenter().setBorder(new CompoundBorder(new EmptyBorder(10,10 ,10,10),new EtchedBorder(Color.BLACK, Color.black)));
//add text editor to the center panel
getCenter().add(getEditor(),BorderLayout.CENTER);
//set layout of the frame
setLayout(new BorderLayout());
menubar1 = new JMenuBar();
//create menu list from a string arrays
for(int i=0; i<list.length; i++){
JMenu menus = new JMenu(list[i]);
menubar1.add(menus);
}
You are using the layout managers incorrectly. Please find your modified code which is working.
public class TestFrame extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestFrame() {
// create main windows
super("Liste");
// scroll bar
JScrollPane scroll = new JScrollPane();
//setEditor(editors);
// create center panel
JPanel cent = new JPanel();
//create Panel for the to
JPanel north = new JPanel();
getContentPane().setLayout(new BorderLayout(0, 0));
//toolbar.add(scroll);
// set center panel and add preferred layout and backgrounds and size
//setCenter(cent);
//getCenter().setLayout(new BorderLayout());
// add scroll bar and toolbar
getContentPane().add(scroll);
JTextArea textArea = new JTextArea();
scroll.setViewportView(textArea);
JToolBar toolBar = new JToolBar();
getContentPane().add(toolBar, BorderLayout.NORTH);
JMenuBar menubar1 = new JMenuBar();
//create menu list from a string arrays
// for(int i=0; i<list.length; i++){
// JMenu menus = new JMenu(list[i]);
// menubar1.add(menus);
// }
}
}
Hope this will be helpful. :-)

JScrollPane not showing up on inherited JPanel

I have a class which extends a JPanel. Code below:
public class Test extends JPanel implements Testnterface {
private JScrollPane listScroller;
DefaultListModel model;
private JList requestList;
public Test() {
String title = "Stackoverflow Question";
setBorder(BorderFactory.createTitledBorder(title));
model = new DefaultListModel();
requestList = new JList(); // create a list
listScroller = new JScrollPane(requestList); // create a scrollbar to the list
listScroller.setPreferredSize(new Dimension(250, 80));
setLayout(null);
add(listScroller); // even though I add the scrollPane, nothing is displayed
}
}
I updated the code. I can't seem to get my JScrollPane to be displayed on my JPanel even though I add it. Anyone got any ideas?
I had the same issue and solved it by wrapping scroll pane with JPanel:
final JScrollPane jScrollPane = new JScrollPane(list,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
final JPanel panel = new JPanel(new BorderLayout(BORDER_SIZE, BORDER_SIZE));
panel.setBorder(BorderFactory.createEmptyBorder(BORDER_SIZE, BORDER_SIZE,
BORDER_SIZE, BORDER_SIZE));
panel.add(jScrollPane);

JScrollPane not resizing when added to JPanel

I cant get the scrollPane to resize correctly when added to the scrollPanel. I want the scrollPane to scale to the size of the scrollPanel. Any tips?
public class MTabbedPane_HomePanel extends JPanel {
private JPanel scrollPanel;
private JScrollPane scrollPane;
public MTabbedPane_HomePanel()
{
init();
makePanel();
}
private void init() {
scrollPanel = new JPanel();
scrollPane = new JScrollPane(scrollPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
private void makePanel() {
IAppWidgetFactory.makeIAppScrollPane(scrollPane);
setLayout(new BorderLayout());
scrollPane.setBorder(null);
add(scrollPane, BorderLayout.CENTER);
}
}
your code is correct, maybe
try to disable UIDelegate from IAppWidgetFactory.makeIAppScrollPane(), if remains unchanged
then
check how you added (if is used LayoutManager) for MTabbedPane_HomePanel to the parent
Call setPreferredSIze() for the panel.

Categories