Scroll problem - doesn't scroll when I press - java

I have strange problem in Java. I have JScrollPane
paneScroll=new JScrollPane(nav,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
where nav is JPanel
public class ScrollableNavigationPanelZones extends JPanel {
private ButtonGroup buttonGroup = new ButtonGroup();
private static final long serialVersionUID = -455651438039139284L;
protected JViewport viewport;
private JPanel panel;
private int offset = 100;
public ScrollableNavigationPanelZones() {
super();
setLayout(new BorderLayout());
panel = new JPanel();
panel.setPreferredSize(new Dimension(0,160));
FlowLayout fl = new FlowLayout();
fl.setVgap(5);
fl.setAlignment(FlowLayout.LEFT);
panel.setLayout(fl);
add(panel,BorderLayout.CENTER);
}
}
and it shows scroll on right side but doesn't have that small box which to move ( I have arrows on top and bottom of scroll, but when I press I cannot scroll ). When I change in ScrollableNavigationPanelZones panel.setPreferredSize(new Dimension(0,16000)); it works but shows empty space. Why JScrollPane doesn't read real height of panel ? What is mistake ?
Can anybody help please ?

The scroll box is displayed only if there is something to scroll, i.e. if the preferred height of the panel exceeds the actual height of the scroll pane. Of course, if you set the preferred height to 16000 it will show much of empty space if the panel doesn't fill all the 16000 pixels with controls.
If the panel fits in the scroll pane, the scroll bar is by default hidden. The flag VERTICAL_SCROLLBAR_ALWAYS implies that the scroll bar is visible always, even if not needed. If you set the panel height to 160 pixels and the scroll pane is larger, you see the scroll bar (because it's ALWAYS) but no scroll box (because there is actually nothing to scroll).
It's a bit strange that the up and down buttons on the scroll bar are clickable even if there is nothing to scroll. Usually they should be greyed out in this case. Maybe there is some additional parameter in JScrollPane for this...

Related

Have to move screen to get JTextArea to appear

I have a desktop pane program where a user inputs data and a jtextarea appears with their results. Instead of having just the text area, i wanted to add it to a scroll pane, which i did. So I created a new scroll pane, and added the text area to it. Now, when I put in data the text area in the scroll pane does not appear until I move the page. In other words, everything works, but I have to move the page a little in order for the results and the scroll pane to show up on the screen.
Any ideas on why this is happening?
private JTextArea matchListResults = new JTextArea();
private JPanel matchPanelBase = new JPanel(new BorderLayout());
private JScrollPane mResults = new JScrollPane();
private void matchResFrame(String[] matchResultArray) throws IOException, SQLException {
Dimension size = new Dimension();
size.setSize(400, 300);
matchListResults.setPreferredSize(size);
matchListResults.setFont(font);
.
.
.
mResults.getViewport().add(matchListResults);
matchListResults.setVisible(true);
matchPanelBase.add(mResults, BorderLayout.CENTER);
}
When you add components to a visible GUI the basic code is:
panel.add(...);
panel.revalidate();
panel.repaint();
By default all components have a size of (0, 0) so there is nothing to paint. The revalidate() will invoke the layout manager which will determine the components size and location.
After you add the new components, call repaint() on the panel.
matchPanelBase.repaint();

JScrollPanel JPanel and Layouts

What I have:
A split panel with a scroll panel in the right part.
In this scroll panel, I have a JPanel.
I want to have in this JPanel a series of others JPanels stacked one under the other one.
I set the Layout to be a BoxLayout. Now it stacks multiple JPanels, but I have 2 problems:
If my content from JPanel take less space then my frame, then will be lot of space between Jpanels.
If my content from JPanel it's bigger then my frame, the Pannels will go over each other and my scroll from scrollPanel wont activate.
frame = new Frame();
splitPane = new SplitPane();
scrollPane = new ScrollPane();
frame.add(splitPane);
scrollPane.setViewportView(new Lesson());
splitPane.setRightComponent(scrollPane);
splitPane.setLeftComponent(new JTree());
Where Frame, SplitPane, ScrollPane() are classes that extends JFrame, JSplitPane, JScrollPane. Atm they only have a constructor, after it will work, I want to make some customization there.
public class Lesson extends JPanel {
private static final long serialVersionUID = 1L;
public Lesson() {
customize();
String text = "text from pictures";
add(new Paragraph(text));
add(new Paragraph(text));
}
private void customize() {
BoxLayout boxLayout = new BoxLayout(this, BoxLayout.PAGE_AXIS);
setLayout(boxLayout);
}
}
public class Paragraph extends JPanel {
private static final long serialVersionUID = 1L;
public Paragraph(String text) {
setLayout(new FlowLayout(FlowLayout.LEFT));
setPreferredSize(new Dimension());
StringTokenizer splitStringTokenizer = new StringTokenizer(text, " ");
while(splitStringTokenizer.hasMoreTokens()){
add(label(splitStringTokenizer.nextToken().toString()));
}
}
private JLabel label(String string){
JLabel jlabel= new JLabel(string);
return jlabel;
}
}
Any hints about how I can resolve this ? Ty in advance.
A BoxLayout respects the maximum and minimum sizes of components added to it. You are using a FlowLayout o the Paragraph panel. The preferred size is always one line of components.
The panel will shrink until there is only one line displayed or grow to occupy all the space.
When there is more space the panels are allowed to grow.
Override the getMaximum/MinimumSize() of your Paragraph panel to return the preferred size.
The question is why are you using a panel of labels to display text. Why are you not using a text area.
Or another option may be to use the WrapLayout which will wrap components automatically and recalculate the preferred size based on the wrapping. You will still want to override the getMinimum/Maximum size calculations to return the preferred size.
I want later to add some mouse listener to some of jlabels.
Why? Again if you use a text area, you can add the MouseListener directly to the text area and then you can use the caret position (or convert the mouse position to an offset in the text area) to determine what word the mouse is over and then do your processing.

Change Background and size of panels in card layout

I have some panels in a card layout container (no idea if that is correct terminology). I can't find a way to set the location, or size of these panels inside the container. I tried setBounds and setLayout(null) and I still can't get anything to change.
These are my fields and the constructor. I've gotten my frame working and I can see and use the buttons to change cards, but I really can't change much else about the cards. I set the two card panels two have different backgrounds, but they only make a small boarder of color around the button and leave it in the centre of the screen.
I also don't understand why this isn't pasting my code properly... So sorry!
public class TestPanel extends JPanel implements ActionListener {
CardLayout cl = new CardLayout();
private JPanel panelCont = new JPanel();
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private static JButton but1 = new JButton("Change panels");
private static JButton but2 = new JButton("Change back");
public TestPanel() {
panelCont.setLayout(cl);
panel1.add(but1);
panel2.add(but2);
panel1.setBackground(Color.black);
panel2.setBackground(Color.blue);
panelCont.add(panel1, "1");
panelCont.add(panel2, "2");
cl.show(panelCont, "1");
but1.addActionListener(this);
but2.addActionListener(this);
add(panelCont);
}
}
Thanks. I apologise in advance. I'm finding it hard to understand card layout.
A CardLayout respects the preferred size of the panels added to the layout. That is the size will be the size of the largest panel added to the layout.
I set the two card panels two have different backgrounds, but they only make a small boarder of color around the button and leave it in the centre of the screen.
The default layout for a panel is the FlowLayout. A FlowLayout by default has a 5 pixel horizontal/vertical gap around each component. So the preferred size of your panel is the size of the button plus the 5 pixel gap.
The panel is displaying correctly. When you add other components to the panel the size will change as required.
It's not clear where you pack() the enclosing Window. By default, pack() causes a panel having CardLayout to adopt the the size of the largest panel's preferred size, which is determined by the size of its contents. This example uses setPreferredSize() to specify an arbitrary size, but you can override getPreferredSize() as shown here.

Resizing a JScrollPane based on window size

After much searching, I hope this will bring me an answer.
Ok, I have a JFrame, which has a a JPanel across the top, and one across the bottom. It also has one on the side, which contains a JScrollPane. The top and bottom panels should remain a consistent size with the window resizing, but the side panel should change vertically. Unfortunately, no scrollbar shows up at all when the JScrollPane has too many items. Rather, the entire window is enlarged, pushing the bottom panel and all excess within the JScrollPane off-screen.
I have been using MigLayout, but if I need to use another layout for the side panel I can. Here is my most recent failed iteration of code.
This is where I add the JScrollPane:
public MenuPanel(){
this.setLayout(new BorderLayout());
innerPanel = new InnerPanel();
jsp = new JScrollPane(innerPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.add(jsp, BorderLayout.CENTER);
}
This is inside the main window:
private void addSideSelectionPane() {
side = new SelectionPanel();
this.add(side, "wmax 200, growy");
}
And here is the code where I create the main window:
public InsWindow(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState( this.getExtendedState()| java.awt.Frame.MAXIMIZED_BOTH );
this.setLayout(new MigLayout("debug, nogrid, fill", "[grow, fill]", "[pref!]10[grow, fill]"));
this.addTestLabel();
this.addSideSelectionPane();
this.addMainWindow();
this.addBottomPanel();
this.setVisible(true);
}

Java - How to set the height and width of the component using box layout

I am creating the applet using the BoxLayout. In this layout i have 3 components(i.e, 2 text areas and one button). I want to set the height and width of the button.Please can anybody help me.
code
public class parsetextdata extends Applet
{
TextArea ta1,ta2;
Button parse;
public void init()
{
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
ta1 = new TextArea();
add(ta1);
parse = new Button();
parse.setLabel("parse");
parse.setBackground(Color.DARK_GRAY);
parse.setForeground(Color.WHITE);
add(parse);
ta2 = new TextArea();
ta2.setEditable(false);
ta2.setBackground(Color.GRAY);
ta2.setForeground(Color.WHITE);
add(ta2);
}
}
Do not add the JButton directly. Instead, add it to a JPanel, and then add that JPanel to the applet's content pane. The reason for this is the layout manager of the applet's content pane is causing the components to take up as much space as possible. By adding the button to the panel first, and then adding the panel to the applet's content pane, the panel will be resized and the button will keep it's preferred size.
EDIT -
I just noticed that you're using AWT components. Therefore, here are the component translations:
JButton = Button
JPanel = Panel

Categories