How to immediately show scroll bar in the JScrollPane - java

I added JScrollPane to my JPanel but I wanted to make scroll bar visible at the start of the program not only when the text is going out of the screen is that possible?
jpMiddle = new JPanel();
JTextArea ta = new JTextArea(20,43);
JScrollPane sp = new JScrollPane(ta);
jpMiddle.add(sp);

JScrollPane sp = new JScrollPane(ta,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
That's for the scrollbars to be active always, you can substitute the ALWAYS for AS_NEEDED to ensure they appear only when the contents of ths scrollpane overflows.
The API exists for one reason, though...

Related

how do I add a horizontal and vertical scroll bar to my JTextArea (java)

Can someone help me add a scroll bar, both vertical and horizontal, in the JTextArea?
textarea1 = new JTextArea();
textarea1.setBounds(17,183,208,100);
textarea1.setBackground(new Color(40,40,40));
textarea1.setForeground(new Color(225,228,0));
textarea1.setEnabled(true);
textarea1.setFont(new Font("sansserif",0,12));
textarea1.setText(chat1);
textarea1.setBorder(BorderFactory.createBevelBorder(1));
textarea1.setVisible(true);
Thanks :)
The following is how it could be achieved:
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta); // JTextArea is placed in a JScrollPane.
Once the JTextArea is included in the JScrollPane, the JScrollPane should be added to where the text area should be. In the following example, the text area with the scroll bars is added to a JFrame:
JFrame f = new JFrame();
f.getContentPane().add(sp);

Adding JScrollPane to GridBagLayout

Everytime I click a button in my program, I add 5 new rows to the JPanel. Eventually, the rows overflow and I would like to add a JScrollPane so I can scroll down and see the new rows.
I know how to get it working for a TextArea but I can't seem to figure out how to make it work when I have a GridBagLayout. Below, I will attach the code for setting up my panels.
JPanel panelMain = new JPanel();
getContentPane().add(panelMain);
JPanel panelForm = new JPanel(new GridBagLayout());
panelMain.add(panelForm);
JScrollPane scrollpane = new JScrollPane(panelForm);
panelMain.add(scrollpane);
When I run, my code I get a box enclosing the GridBagLayout, but the scrollpane is nowhere to be seen.
JPanel panelMain = new JPanel();
getContentPane().add(panelMain);
You would typically add the scroll pane directly to the frame. There is no need for the "panelMain".
panelMain.add(panelForm);
That line is not needed. A component can only belong to a single parent. You later add "panelForm" to the scrollpane.
So the basic code would be:
JPanel panelForm = new JPanel(new GridBagLayout());
JScrollPane scrollpane = new JScrollPane(panelForm);
frame.add(scrollpane);
but the scrollpane is nowhere to be seen.
The scrollbars only appear when needed by default. Adding empty panels will not cause the scrollbar to be displayed. Click on your button a few times and add your child components to the "panelForm" using the appropriate GridBagConstraints. You will then need to use:
panelForm.revalidate();
panelForm.repaint();
The revalidate() causes the layout manager to be invoked so the scrollpane can determine is scrollbars are required or not.

How to autoscroll JScrollPane with JTextArea inside to the left

I have private JTextArea opisDiagnoza; I insert it into JScrollPane jsp, like this:
opisDiagnoza = new JTextArea("Opis diagnozy:\n");
JScrollPane jsp = new JScrollPane(opisDiagnoza, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Later on, when I do:
opisDiagnoza.setText(REALLY_LONG_TEXT);
the JScrollPane autorscrolls to right, so to read the content I need to manually(by clicking horizontal scrollbar) scroll it to the left.
How to autoscroll JScrollPane to the left after inserting a text into JTextArea inside of it?
Example:
http://i.stack.imgur.com/fxc4x.png
I can't add image explicitly because of low reputation.
Try this
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

How to add JPanel into another JPanel with vertical scrollpane at runtime?

I'm trying to insert a new panel into another panel in runtime everytime I press a button. My problem is the original panel runs out of space and I can't see the new panels I'm adding.
What I've tried so far:
Using scrollpane for vertical scrolling with no success.
Using flowlayout-no luck. Tried disabling horizontal scrolling-keep pushing the new panel to the right (can't get to it because there is no scrolling).
Tried using borderlayout-no luck.
testpanel t = new testpanel();
t.setVisible(true);
this.jPanel15.add(t);
this.jPanel15.validate();
this.jPanel15.repaint();
This code suppose to insert the t panel into jpanel15.
With flowlayout it pushes the t panel downwards just like I want it to but with no vertical scroll.
PS: I'm using netbeans in order to create my GUI.
My problem is the original panel runs out of space and I cant see the new panels i'm adding. Tried using scrollpane for vertical scrolling with no success.
A FlowLayout adds components horizontally, not vertically so you will never see vertical scrollbars. Instead you can try the Wrap Layout.
The basic code to create the scrollpane would be:
JPanel main = new JPanel( new WrapLayout() );
JScrollPane scrollPane = new JScrollPane( main );
frame.add(scrollPane);
Then when you dynamically add components to the main panel you would do:
main.add(...);
main.revalidate();
main.repaint(); // sometimes needed
Use JScrollPane instead of the (outer) JPanel
Or have a BorderLayout for the JPanel, put in a JScrollPane at BorderLayout.CENTER as the only control. The JScrollPane takes a regular JPanel as view.
In any case you will then add the control to the JScrollPane. Suppose your JScrollPane variable is spn, your control to add is ctrl:
// Creation of the JScrollPane: Make the view a panel, having a BoxLayout manager for the Y-axis
JPanel view = new JPanel( );
view.setLayout( new BoxLayout( view, BoxLayout.Y_AXIS ) );
JScrollPane spn = new JScrollPane( view );
// The component you wish to add to the JScrollPane
Component ctrl = ...;
// Set the alignment (there's also RIGHT_ALIGNMENT and CENTER_ALIGNMENT)
ctrl.setAlignmentX( Component.LEFT_ALIGNMENT );
// Adding the component to the JScrollPane
JPanel pnl = (JPanel) spn.getViewport( ).getView( );
pnl.add( ctrl );
pnl.revalidate( );
pnl.repaint( );
spn.revalidate( );

scrollpane blocks buttons in BorderLayout?

I have a really weird problem with a JScrollPane and a BorderLayout. For short explaination: i have a JTable which is inside the JScrollPane and this is with a JPanel and the JTableHeader on a JTabbedPane. Very Simple Layout. If i add just the JTable to my JPanel, the buttons are working. If i add the JScrollPane, the Buttons are not working anymore, so i cant click them! The ActionLister is never reached and i cant see the click-animation.
Some Sample code to explain:
d_pane = new JPanel();
d_button = new JPanel();
d_pane.add(table.getTableHeader(), BorderLayout.PAGE_START);
dl_scroll = new JScrollPane(table);
d_pane.add(dl_scroll, BorderLayout.CENTER);
// d_button is ridLayouted with 3 Buttons in there
d_pane.add(d_button, BorderLayout.PAGE_END);
1) The JScrollPane takes care of the table header itself. Don't add it to the pane.
2) the button does not seem to get the mouse events, probably because another component is above it - do you have other components/code in the setup?

Categories