LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   java swing : vertical slider on jtextpane - no thumb (http://www.linuxquestions.org/questions/programming-9/java-swing-vertical-slider-on-jtextpane-no-thumb-894688/)

rmknox 07-30-2011 05:00 PM

java swing : vertical slider on jtextpane - no thumb
 
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.8) (fedora-53.1.9.8.fc14-i386)
OpenJDK Client VM (build 19.0-b09, mixed mode)



I want to add a vertical slider to a JTextPane component.
When I do, I get the slider but I don't get a thumb - i.e. the slider doesnt do anything

here is my code
Code:

    JPanel dataPane            = new JPanel();                        // create panel dataPane
    ta2 = new JTextPane();                                                  // textpane 

    if (true)
    {
    JScrollPane slider        = new JScrollPane(ta2);        // slider owns textpane
    slider.setVerticalScrollBarPolicy(                                // vertical
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);        // always
    slider.setVisible(true);                                                // added trying to get it to show correctly
    slider.setEnabled(true);                                                // ditto
    dataPane.add(slider);                                                        // datapane owns slider                                                                        //3    new
    }
    else dataPane.add(ta2);

the (true) lets me do something like ifdef to try out my new ideas

as you see, this code comes when I create the object but before I populate it.
maybe I need to do something after I populate the textpane?

any help would be much appreciated

Thor_2.0 07-31-2011 04:17 AM

Okay, for starters, I'd put the textpane in/on a jpanel with a fixed size and put that in a scrollpane.
I think you're confusing this with the known behaviour of a JList where the scroller pops up as soon as the viewport becomes too small to handle the content. A scrollpane is something you use when screen space is at a premium and you nee to show a bit more...

Quote:

JPanel dataPane = new JPanel(); // create panel dataPane
JPanel floater = new JPanel(null); // the floater
ta2 = new JTextPane(); // textpane

if (true)
{
floater.setSize(new Dimension(what,ever));
ta2.setsize(new Dimension(floater.getSize()));
floater.add(ta2);
ta2.setLocation(0,0);

JScrollPane slider = new JScrollPane(floater); // slider owns textpane
slider.setVerticalScrollBarPolicy( // vertical
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // always
slider.setVisible(true); // added trying to get it to show correctly
slider.setEnabled(true); // ditto
dataPane.add(slider); // datapane owns slider //3 new
}
else dataPane.add(ta2);
I'd try something like that...

Lemme know where this thing lands, okay? :)

Thor

rmknox 07-31-2011 01:13 PM

Thor

see if I captured your suggestion

result is a tiny bar maybe 1mm high and 1cm long instead of the textpane and slider

when I do it the other way, I get the full pane but with an inoperable slider

when I do it the old way, I get the fell pane and no slider

and YES - I am confused

Dick

Code:

    JPanel dataPane          = new JPanel();                        // create panel dataPane
    JPanel floater                = new JPanel(null);        // the floater
    JScrollPane slider        = null;
    ta2 = new JTextPane();                                                  // textpane 
    boolean addSlider = (1<2);                                                // set to control flow
    boolean thorsWay  = (1>2);                                                // ditto

    if (addSlider)                                                                        // true to add a slider
    {
            if (thorsWay)                                                                // do it Thors way
            {
                    floater.setSize(new Dimension(100,100));                //
                    ta2.setSize(new Dimension(floater.getSize()));        //
                    floater.add(ta2);                                                                // put ta2 in floater
                    ta2.setLocation(0,0);                                                        //
                    slider        = new JScrollPane(floater);                        // slider owns textpane
            }
            else slider        = new JScrollPane(ta2);        // slider owns ta2
    slider.setVerticalScrollBarPolicy(                                // vertical
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);        // always
    slider.setVisible(true);                                                // added trying to get it to show correctly
    slider.setEnabled(true);                                                // ditto
    dataPane.add(slider);                                                        // datapane owns slider                                                                        //3    new

    }
    else dataPane.add(ta2);


rmknox 07-31-2011 01:35 PM

I found what I needed

my original solution was ok, I just needed to add

Code:

    slider.setPreferredSize(new Dimension(330, 500)); // needed this
now when the thumb is needed it appears

Thor_2.0 07-31-2011 01:52 PM

Cool, glad you found it!

Happy programmin' !

Anything in particular you're working on?

Thor

rmknox 07-31-2011 08:13 PM

for the archive, here is the source that works to put a vertical slider on a JTextPane
Code:

   
    JPanel dataPane          = new JPanel();                        // create panel dataPane
    ta2                = new JTextPane();                  // textpane 
    JScrollPane slider        = new JScrollPane(ta2);                // slider owns ta2
    slider.setVerticalScrollBarPolicy(                        // vertical
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);        // always
    slider.setPreferredSize(new Dimension(330, 500));  // needed this for it to know when to show thumb
    dataPane.add(slider);                                // datapane includes slider

Thor - again, thanks


All times are GMT -5. The time now is 07:10 AM.