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);
}
}
Related
I have a panel with layout (for example, BorderLayout) and a JScrollPane on its center. JScrollPane has content inside it (a JPanel)
The thing is, that when this JScrollPane resizes, I do not want its content to resize. For example, if layout increases the JScrollPane, I want its content to be as small it was (and occupy only part of the pane), but it resizes to fit pane.
I also need an opportunity to reduce content inside the pane and increase it manually (there is no problems with increasing, they are in reducing).
So, how can I achieve content size independency? Of course, I need to save scrolling features, if content will be bigger than JScrollPane.
Here is a simple example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestScroll extends JFrame {
public TestScroll() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500, 500);
init();
}
private void init() {
setLayout(new BorderLayout());
// Inner panel
final JPanel innerPanel = new JPanel();
innerPanel.setOpaque(true);
innerPanel.setBackground(Color.BLUE);
innerPanel.setPreferredSize(new Dimension(200, 200));
// Scroll
final JScrollPane scrollPane = new JScrollPane(innerPanel);
add(scrollPane, BorderLayout.CENTER);
// Buttons
JPanel buttonPanel = new JPanel();
JButton extendButton = new JButton("Extend inner panel");
extendButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
innerPanel.setPreferredSize(new Dimension(innerPanel.getWidth() * 2,
innerPanel.getHeight() * 2));
innerPanel.revalidate();
}
});
buttonPanel.add(extendButton);
JButton reduceButton = new JButton("Reduce inner panel");
reduceButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
innerPanel.setPreferredSize(new Dimension(innerPanel.getWidth() / 2,
innerPanel.getHeight() / 2));
innerPanel.revalidate();
}
});
buttonPanel.add(reduceButton);
add(buttonPanel, BorderLayout.NORTH);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new TestScroll();
}
});
}
}
So, problem statement:
I do not want inner panel to stretch to the pane (but pane can be resized by outer layout, so the panel must just keep its size).
I want to be able to reduce inner panel manually so it can occupy only a part of the scroll pane.
And, of course, I want to save scrolling functionality when inner panel is larger than scroll pane.
Did you follow Andrew's link about using layout managers to achieve your goal?
Here is another simple example:
//final JScrollPane scrollPane = new JScrollPane(innerPanel);
JPanel outer = new JPanel();
outer.add( innerPanel );
final JScrollPane scrollPane = new JScrollPane(outer);
i have a JScrollPane.
But default it displays JTextArea.
JTextArea jTextArea = new JTextArea();
JScrollPane pane = new JScrollPane(jTextArea);
so here everything is fine. But now i would like to change JScrollPane component by user action:
pane.remove(jTextArea);
pane.add(new JTable(data[][], columns[]));
pane.revalidate();
frame.repaint();
frame in my main Window. JScrollPane is added to main window with GridBagLayout.
But this doesn't work. After running action JScrollPane becomes grey.
jScrollPane.getViewport().remove/add
One alternative is to put a JPanel with a CardLayout1 into the JScrollPane, add both the components to the panel, then simply flip between the text area and table as needed.
How to Use CardLayout
Given the components might be of vastly different size, it might be better to do it this way:
JPanel with CardLayout contains many JScrollPane instances, each of which contains a single component. That will also work inherently better for a JTable.
Edited my answer after receiving one valuable suggestion by His Majesty #camickr, setViewportView(componentObject); is used to do such things.
A sample code to help your cause :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScrollPaneExample extends JFrame
{
private JPanel panel;
private JScrollPane scrollPane;
private JTextArea tarea;
private JTextPane tpane;
private JButton button;
private int count;
public ScrollPaneExample()
{
count = 0;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
panel = new JPanel();
panel.setLayout(new BorderLayout());
tarea = new JTextArea();
tarea.setBackground(Color.BLUE);
tarea.setForeground(Color.WHITE);
tarea.setText("TextArea is working");
scrollPane = new JScrollPane(tarea);
tpane = new JTextPane();
tpane.setText("TextPane is working.");
button = new JButton("Click me to CHANGE COMPONENTS");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (count == 0)
{
scrollPane.setViewportView(tpane);
count++;
}
else if (count == 1)
{
scrollPane.setViewportView(tarea);
count--;
}
}
});
setContentPane(panel);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
pack();
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ScrollPaneExample();
}
});
}
}
Hope this might help you in some way.
Regards
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.
Right, I have a JTabbedPane that has a JPanel that contains a JLabel and a JTextField.
my code
JTabbed Pane declaration :
this.tabPane = new JTabbedPane();
this.tabPane.setSize(750, 50);
this.tabPane.setLocation(10, 10);
tabPane.setSize(750,450);
tabPane.add("ControlPanel",controlPanel);
textfield declaration :
this.channelTxtFld = new JTextField("");
this.channelTxtFld.setFont(this.indentedFont);
this.channelTxtFld.setSize(200, 30);
this.channelTxtFld.setLocation(200, 10);
JLabel :
this.channelLabel = new JLabel("Channel name : ");
this.channelLabel.setSize(150, 30);
this.channelLabel.setLocation(10,10);
private void createPanels() {
controlPanel = new JPanel();
controlPanel.setSize(650,500);
}
private void fillPanels() {
controlPanel.add(channelLabel);
controlPanel.add(channelTxtFld);
}
So what my plan is, was to have a tabbed pane that has a JPanel where I have some Labels, textfields and buttons on fixed positions, but after doing this this is my result:
http://i.stack.imgur.com/vXa68.png
What I wanted was that I had the JLabel and next to it a full grown JTextfield on the left side not in the middle.
Anyone any idea what my mistake is ?
thank you :)
What kind of Layout Manager are you using for your controlPanel, you probably want BorderLayout, putting the Label in the West, and the TextField in the center.
BTW, setting the size and position of various components doesn't make sense unless you are using a Null Layout, which isn't a good idea. So i'd remove all that stuff and let the Layout Manager do it for you.
Use a LayoutManager and consider also the methods setPreferredSize, setMinimumSize, setMaximumSize to adjust components bounds according on which is your desired effect.
Assuming the default JPanel layout, FlowLayout, give the JTextField a non-zero number of columns, and give the JLabel a JLabel.LEFT constraint.
Addendum:
a full grown JTextField
Something like this?
import java.awt.*;
import javax.swing.*;
/**
* #see http://stackoverflow.com/questions/5773874
*/
public class JTabbedText {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
private final JTabbedPane jtp = new JTabbedPane();
#Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtp.setPreferredSize(new Dimension(400, 200));
jtp.addTab("Control", new MyPanel("Channel"));
f.add(jtp, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
private static class MyPanel extends JPanel {
private final JLabel label = new JLabel("", JLabel.LEFT);
private final JTextField text = new JTextField();
public MyPanel(String name) {
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
label.setText(name);
label.setAlignmentY(JLabel.TOP_ALIGNMENT);
text.setAlignmentY(JTextField.TOP_ALIGNMENT);
this.add(label);
this.add(text);
}
}
}
I have a class that extends a JPanel called Row. I have a bunch of Row added to a JLabel, the code is the following:
JFrame f=new JFrame();
JPanel rowPanel = new JPanel();
//southReviewPanel.setPreferredSize(new Dimension(400,130));
rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.Y_AXIS));
rowPanel.add(test1);
rowPanel.add(test1);
rowPanel.add(test2);
rowPanel.add(test3);
rowPanel.add(test4);
rowPanel.setPreferredSize(new Dimension(600, 400));
rowPanel.setMaximumSize(rowPanel.getPreferredSize());
rowPanel.setMinimumSize(rowPanel.getPreferredSize());
f.setSize(new Dimension(300,600));
JScrollPane sp = new JScrollPane(rowPanel);
sp.setSize(new Dimension(300,600));
f.add(sp);
f.setVisible(true);
where test1...etc is a Row. However when I resize the window the layout of the Row somehow becomes messy (it resizes as well)... how can I prevent this from happening?
Read the Swing tutorial on Using Layout Managers. Each layout manager has its own rules about what happens when the container is resized. Experiment and play.
In the case of a BoxLayout it should respect the maximum size of the components added to the panel so you can do:
childPanel.setMaximumSize( childPanel.getPreferredSize() );
If you need more help post your SSCCE demonstrating the problem.
I took the code in http://download.oracle.com/javase/tutorial/uiswing/examples/layout/BoxLayoutDemoProject/src/layout/BoxLayoutDemo.java and adapted it with what you are trying to do, only using buttons instead of custom JPanels:
public class BoxLayoutDemo {
public static void addComponentsToPane(Container pane) {
JPanel rowPanel = new JPanel();
pane.add(rowPanel);
rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.Y_AXIS));
rowPanel.add(addAButton("Button 1"));
rowPanel.add(addAButton("Button 2"));
rowPanel.add(addAButton("Button 3"));
rowPanel.add(addAButton("Button 4"));
rowPanel.add(addAButton("5"));
rowPanel.setPreferredSize(new Dimension(600, 400));
rowPanel.setMaximumSize(rowPanel.getPreferredSize());
rowPanel.setMinimumSize(rowPanel.getPreferredSize());
}
private static JButton addAButton(String text) {
JButton button = new JButton(text);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
return button;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("BoxLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The end result is this:
As you can see, the button row is perfectly aligned. If you resize the JFrame, they stay aligned. Is that what you are looking for?