-
JScrollPane problem
Ok, I have been googling this for hours now and I haven't found a solution. Help needed.
What I basically want is this:
-I have a JPanel, with on that JPanel 3 other JPanels using a GridLayout(1,3), with each a preferredSize of 300,600.
- On the left one of those panels I want a text and an ImageIcon. That text can be short or long and this is where the problem begins.
When the text is too long to fit in the foreseen 300,600 space I want scrollbars, but only vertical ones. This means the text can't be any wider than 300 unless I manually edited the window's size. The left panel is using a boxLayout. I put the content on the left panel like this:
Code:
public void addProfile(){
String text = model.getArtistModel().getDescription();
text = "<html>" + text + "</html>";
JLabel profile = new JLabel(text);
Box box = Box.createHorizontalBox();
box.add(profile);
scroll.add(box);
}
public void addImage(){
JLabel image = new JLabel(model.getArtistModel().getImageIcon());
Box box = Box.createHorizontalBox();
box.add(image);
scroll.add(box);
);
}
both the base panel (the one where the 3 JPanels are added to) and the Jeft panel are JPanels. I have to fit in a JScrollPane somewhere, but I don't know where. I have tried to do so on a lot of places but it never works the way it should.
I'd be most grateful if anyone can help me asap.
-
I now tried using setMinimumSize(260,20) and setMaximumSize(260,2000000) but that doesn't seem to work either...
does anyone know why?
-
The layout manager in use likely doesn't respect these min and max. You may get a quicker correct answer if you create and post an SSCCE that shows your problem. You can read up on this process here: Short, Self Contained, Correct Example
-
Manually add line breaks to the text yourself. You can us a FontMetrics to determine how wide a line of text will be.
-
1 Attachment(s)
OK,
here are the simplified source codes. I don't really want to use FontMetrics if not absolutely necessary. There should be a better way.
LeftPanel.java:
Code:
public class LeftPanel extends JPanel{
private ArtistVensterModelCollection model;
private JPanel panel;
public LeftPanel(ArtistVensterModelCollection model){
this.model = model;
setMinimumSize(new Dimension(260,200));
setMaximumSize(new Dimension(260,2000000));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(BorderFactory.createTitledBorder(model.getArtistModel().getNaam()));
addProfile();
add(Box.createRigidArea(new Dimension(0, 30)));
addImage();
}
public void addProfile(){
String text = model.getArtistModel().getDescription();
text = "<html>" + text + "</html>";
JLabel profile = new JLabel(text);
profile.setMinimumSize(new Dimension(250, 300));
profile.setMaximumSize(new Dimension(250, 20000));
Box box = Box.createHorizontalBox();
box.add(profile);
add(box);
}
public void addImage(){
JLabel image = new JLabel(model.getArtistModel().getImageIcon());
Box box = Box.createHorizontalBox();
box.add(image); add(box);
}
PanelBase.java:
Code:
public class VensterPanelBase extends JPanel{
private ArtistVensterModelCollection model;
public VensterPanelBase(ArtistVensterModelCollection model){
this.model = model;
setPreferredSize(new Dimension(900,600));
setLayout(new GridLayout(1, 3));
makeLeftPanel();
}
public void makeLeftPanel(){
JScrollPane scroll = new JScrollPane(new LeftPanel(model));
scroll.setPreferredSize(new Dimension(300, 600));
add(scroll);
}
}
which gives me a huge horizontal scrollbar in the left panel. The leftPanel itself is huge (i can see that thanks to the border) but the JLabels are added somewhere in the middle. See attachment. Can it be something with margins or something?
thanks.
-
What if your left panel held the text in a non-editable JTextArea that called setWrapStyleWord and setLineWrap both to true? You could change its background and all to mimic a JLabel if need be.
-
that might be an option, yes. I'll try it asap.
However, I'd really like to know what is causing this problem and how to fix it without using other components:/
thanks for your help so far :)