JScrollPane not resizing when added to JPanel - java

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.

Related

How to make JScrollPane dynamic adjusting

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;

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 :)

How to arrange components in a JFrame

I'm trying to get a JTextArea with a "save" JButton centered underneath it, maybe with a small bit of padding between the components as well as the components to the frame if possible. I've tried messing around with layout managers, panels, etc. and can't seem to get the result i want. Just looking for the simplest way to do this. Thanks.
Suggestions:
The overall layout of the GUI container could be BorderLayout.
Add the JScrollPane that holds your JTextArea BorderLayout.CENTER.
Create a JPanel just to hold the JButton and don't give it a specific layout manager. It will now use JPanel's default FlowLayout and will center components in the horizontal direction.
Add your JButton to this last JPanel.
Add that same JPanel to the GUI in the BorderLayout.PAGE_END (bottom) position.
For example:
import java.awt.BorderLayout;
import javax.swing.*;
public class SimpleLayout extends JPanel {
private static final int ROWS = 20;
private static final int COLS = 60;
private JTextArea textArea = new JTextArea(ROWS, COLS);
private JButton button = new JButton("Button");
public SimpleLayout() {
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
setLayout(new BorderLayout());
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
SimpleLayout mainPanel = new SimpleLayout();
JFrame frame = new JFrame("SimpleLayout");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}

JScrollPane won't work

Hey guys I wanted to create a JScrollPane but it won't work... and I don't know why... here's my code...
public class test extends JFrame{
public test(){
setSize(1000,600);
}
private static JButton[] remove;
private static JPanel p = new JPanel();
public static void main(String[]args){
p.setLayout(null);
JFrame t=new test();
remove = new JButton[25];
for(int i=0;i<25;i++){
remove[i]=new JButton("Remove");
remove[i].setBounds(243,92+35*i,85,25);
p.add(remove[i]);
}
JScrollPane scrollPane = new JScrollPane(p);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
t.add(scrollPane);
t.setVisible(true);
}
Umm and Im pretty sure the frame isn't big enough for these 25 buttons... But if i delete that p.setLayout(null); A horizontal scroll bar will be created automatically... I don't really know what is wrong with my code... Pls help thank you very much!
You need to set p's preferredSize for this to work.
p.setPreferredSize(new Dimension(800, 2000));
Or you could have p extend JPanel and then override the getPreferredSize() method to return the proper dimension.
And I agree -- get rid of your null layouts. Learn about and use the layout managers if you want to use Swing correctly and have robust Swing GUI's.
e.g.,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Foo extends JFrame {
private static final int BUTTON_COUNT = 25;
public static void main(String[] args) {
JPanel btnPanel = new JPanel(new GridLayout(0, 1, 0, 20));
btnPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
AbstractAction removeAction = new AbstractAction("Remove") {
#Override
public void actionPerformed(ActionEvent evt) {
JButton src = (JButton) evt.getSource();
JPanel container = (JPanel) src.getParent();
container.remove(src);
container.revalidate();
container.repaint();
}
};
for (int i = 0; i < BUTTON_COUNT; i++) {
JButton removeBtn = new JButton(removeAction);
btnPanel.add(removeBtn);
}
JPanel borderPanel = new JPanel(new BorderLayout());
borderPanel.add(btnPanel, BorderLayout.NORTH);
JScrollPane scrollpane = new JScrollPane(borderPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollpane.setPreferredSize(new Dimension(400, 800));
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
The issue is that a scroll pane checks the component inside it for a "preferred size" so a pane with a null layout has a preferred size of (0,0). Which it ignores.
You should do something along the lines of:
p.setPreferredSize(1000,600);
And you should see some scroll bars appear, I'm not sure how accurate they will be though.

Line wrap in a JTextArea causes JScrollPane to missbehave with MiGLayout

I am having trouble with the same thing as this guy:
MigLayout JTextArea is not shrinking when used with linewrap=true
and I used the solution described in one of the answers; to set the minimum size explicitly. This works fine if one places the JPanel which contains the JTextArea directly in a JFrame, and then resizes the window.
However, when placing the panel which contains the JTextArea inside a JScrollPane,
the same problem occurs again. Why is this, and how can one fix it?
Cheers
EDIT: An example
public class MiGTest2 extends JFrame{
public MiGTest2(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
panel.add(textArea, "wmin 10");
//panel.add(new JTextField());
JScrollPane scrollPane = new JScrollPane(panel);
//add(panel);
add(scrollPane);
pack();
}
public static void main(String[] args){
new MiGTest2().setVisible(true);
}
}
If you uncomment //add(panel);, and comment add(scrollPane);, shrinking the window size will also shrink the JTextArea. That is, it does not work with a JScrollPane. Also note how the layout manager seems to flip out and starts "shaking" all its contents when shrinking the size of the window after first enlarging it
I had a very similar problem and following the answer in the mentioned question did not help me either. However, it did provide a valuable idea -- the problem is in the width of the JTextArea with wrap enabled.
What worked for me was setting both minimum and preferred width at the component level using command width. For example, width 10:500:.
I've had similar problems with JTextAreas and wrapping when used with JScrollPanes.
A solution that worked for me was to create a custom panel that implements the Scrollable interface and overrides the getScrollableTracksViewportWidth() method to return true. This forces causes the scroll pane to only scroll vertically and lets line wrapping in the JTextArea work as expected.
/**
* A panel that, when placed in a {#link JScrollPane}, only scrolls vertically and resizes horizontally as needed.
*/
public class OnlyVerticalScrollPanel extends JPanel implements Scrollable
{
public OnlyVerticalScrollPanel()
{
this(new GridLayout(0, 1));
}
public OnlyVerticalScrollPanel(LayoutManager lm)
{
super(lm);
}
public OnlyVerticalScrollPanel(Component comp)
{
this();
add(comp);
}
#Override
public Dimension getPreferredScrollableViewportSize()
{
return(getPreferredSize());
}
#Override
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation, int direction)
{
return(10);
}
#Override
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation, int direction)
{
return(100);
}
#Override
public boolean getScrollableTracksViewportWidth()
{
return(true);
}
#Override
public boolean getScrollableTracksViewportHeight()
{
return(false);
}
}
and MigTest2 becomes:
public class MiGTest2 extends JFrame
{
public MiGTest2()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
panel.add(textArea, "wmin 10");
//panel.add(new JTextField());
//Wrap panel with the OnlyVerticalScrollPane to prevent horizontal scrolling
JScrollPane scrollPane = new JScrollPane(new OnlyVerticalScrollPanel(panel));
//add(panel);
add(scrollPane);
pack();
}
public static void main(String[] args)
{
new MiGTest2().setVisible(true);
}
}
Normally, you would put the JTextArea into your JScrollPane. Like this:
JTextArea area = new JTextArea();
JScrollPane scroll = new JScrollPane(area);
JPanel panel = new JPanel();
panel.add(scroll);
Not really sure what you're trying to achieve here, try running this and see if it fits your need?
public class MiGTest2 extends JFrame {
public MiGTest2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
panel.add(new JScrollPane(textArea), "wmin 10, grow, push");
setLayout(new MigLayout("fill"));
JScrollPane scrollPane = new JScrollPane(panel);
add(scrollPane, "grow, push");
pack();
}
public static void main(String[] args) {
new MiGTest2().setVisible(true);
}
}

Categories