Hi Marcello,

setPosition() is not a Swing API but a ULC API.

You will need to extend ULCScrollBar and use the extended scroll bar as
shown in the snippet below.

In the snippet, enter the position value in the text field and select menu
to set position.

Thanks and regards,

Janak

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of
[EMAIL PROTECTED]
Sent: Wednesday, October 03, 2007 12:04 PM
To: [email protected]
Subject: [ULC-developer] ULCScrollbar #setPosition



Good morning

I have a problem by centering the scrollbar with a large label.
I made a Scrollbar with a label witch has a large text in it. With an menu I
try to set the position off the bar at the center off the text. (see the
example).

It seems that setPosition sets the postion off the beginning off the bar and
not really centered off the text.
Has anybody an idea how I can set the scrollbar centered?

Regards
Marcello

------------------


import javax.swing.SwingUtilities;

import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ULCBorderLayoutPane;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCLabel;
import com.ulcjava.base.application.ULCMenu;
import com.ulcjava.base.application.ULCMenuBar;
import com.ulcjava.base.application.ULCMenuItem;
import com.ulcjava.base.application.ULCScrollBar;
import com.ulcjava.base.application.ULCScrollPane;
import com.ulcjava.base.application.ULCTextField;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.util.Dimension;
import com.ulcjava.base.client.UIScrollBar;
import com.ulcjava.base.development.DevelopmentRunner;

public class ScrolBarApplication extends AbstractApplication {

    private ULCScrollPane scrollPane;
    private ULCTextField text;

    private static final long serialVersionUID = -361433820639246090L;

    public void start() {

        ULCFrame frame = new ULCFrame("Scrollbar Position");
        frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);

        frame.setMenuBar(getMenuBar());

        scrollPane = new ULCScrollPane();
        scrollPane.setHorizontalScrollBar(new ULCMyScrollBar());
        scrollPane
                .setViewPortView(new ULCLabel(
                        "123456789012345678901234567890123456789012345678901
23456789012345678901234567890123456789012345678901234567890 END"));

        frame.setSize(new Dimension(400, 300));
        text = new ULCTextField(10);

        frame.add(text, ULCBorderLayoutPane.NORTH);
        frame.add(scrollPane, ULCBorderLayoutPane.CENTER);

        frame.setVisible(true);
    }

    private ULCMenuBar getMenuBar() {
        ULCMenuBar menuBar = new ULCMenuBar();

        ULCMenu menu = new ULCMenu("Position");
        menu.add(getPositionMenuItem());
        menuBar.add(menu);

        return menuBar;
    }

    private ULCMenuItem getPositionMenuItem() {
        ULCMenuItem menuItem = new ULCMenuItem("Set Position");
        menuItem.addActionListener(new IActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.out.println(scrollPane.getHorizontalScrollBar().getPo
sition());
                scrollPane.getHorizontalScrollBar().setPosition(Double.value
Of(text.getText()).doubleValue());
                System.out.println(scrollPane.getHorizontalScrollBar().getPo
sition());
            }
        });

        return menuItem;
    }

    public static void main(String[] args) {

        DevelopmentRunner.setApplicationClass(ScrolBarApplication.class);
        DevelopmentRunner.main(args);
    }

    public static class ULCMyScrollBar extends ULCScrollBar {
        public ULCMyScrollBar() {
            super();
            setOrientation(ULCScrollBar.HORIZONTAL);
        }

        protected String typeString() {
            return UIMyScrollBar.class.getName();
        }
    }

    public static class UIMyScrollBar extends UIScrollBar {
        public void setPosition(final double position) {
            super.setPosition(position);
            // this SwingUtitlities.invokeLater() ensures that the scrollbar
is added to the component tree
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    getBasicScrollBar().revalidate();
                    // this SwingUtitlities.invokeLater() ensures that the
scrollbar is layouted before the position is
                    // set
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            getBasicScrollBar()
                                    .setValue(
                                            (int)((getBasicScrollBar().getMa
ximum() - getBasicScrollBar().getWidth()) * position));
                        }
                    });
                }
            });
        }
    }
}

_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer

Reply via email to