JSlider 标签中的工具提示

发布于 2024-12-03 22:35:49 字数 433 浏览 4 评论 0原文

我想创建一个带有标签的 JSlider,其中有一个工具提示。

JSlider slider = new JSlider();
JLabel label = new JLabel("First");

slider.setPaintLabels(true);
Hashtable<Integer, JLabel> labels = new Hashtable<Integer, JLabel>();

label.setToolTipText("Tooltip");

labels.put(new Integer(0), label);
slider.setLabelTable(labels);

但是,这段代码不起作用。我认为这是因为我们可以向 JSlider 添加工具提示,并且它“覆盖”了所有其他工具提示。

有什么方法可以解决我的问题吗?

I want to create a JSlider with labels, which have a tooltip.

JSlider slider = new JSlider();
JLabel label = new JLabel("First");

slider.setPaintLabels(true);
Hashtable<Integer, JLabel> labels = new Hashtable<Integer, JLabel>();

label.setToolTipText("Tooltip");

labels.put(new Integer(0), label);
slider.setLabelTable(labels);

But, this code does not work. I think it's because we can add tooltip to JSlider, and it "covered" all others.

Is there a method, how I can resolve my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羁绊已千年 2024-12-10 22:35:49

您需要重写 getToolTipText(MouseEvent) 方法。

然后在代码中,您需要确定滑块中的鼠标位置以确定要显示的文本。我从未尝试过,但您也许可以使用 BasicSliderUI 来实现此目的。它的方法 valueForXPositionvalueForYPosition 可能会有所帮助。

You would need to override the getToolTipText(MouseEvent) method.

Then in the code you would need to determine the mouse position in the slider to determine the text to display. I've never tried it but you might be able to use the BasicSliderUI for this. It has methods valueForXPosition and valueForYPosition which might help.

月牙弯弯 2024-12-10 22:35:49

显然第一次尝试不起作用的根本原因是标签没有添加到滑块中(可能应该添加到滑块中,因为它们并没有太多并且地图是真实 JLabels 的地图)但是简单地在 BasicSliderUI 的 PaintHorizo​​ntal/VerticalLabel 中动态渲染。因此,Rob 的建议是自然的方法:计算是否有任何标签位于 mousePosition 下,并返回其工具提示(如果可用)。

令人惊讶的是,没有公共 api(无论是在 JSlider 上还是在 ui delegate 上)来实现标签边界的计算。您需要的是访问 x/yPositionForValue - 但这是受保护的。因此,只有肮脏的方法可以

  • 反射性地访问 x/yPositionForValue
  • 并在 JSlider 子类中实现:仅给出近似值,因为许多 ui 内部结构都是受保护的,甚至是私有的

希望被证明是错误的并看到一个干净的实现(无需子类化 ui 代表:-)

Underlying reason for the obviously first try not working is that the labels are not added to the slider (as probably they should, given they are not too numerous anyway and the map is a map of real JLabels) but simply rendered on-the-fly in the paintHorizontal/VerticalLabel of BasicSliderUI. So Rob's advice the natural way to go: calculate if any of the labels is under the mousePosition and return its tooltip if available.

Astonishing, there is no public api (neither on JSlider nor on the ui delegate) to achieve the calculation of the label boundaries. What you would need is access to the x/yPositionForValue - but that's protected. So there are only dirty ways out

  • access the x/yPositionForValue reflectively
  • c&p and implement in a JSlider subclass: gives approximations only, as many of the ui internals are protected or even private

Would love to be proven wrong and see a clean implementation (without subclassing the ui-delegates :-)

拧巴小姐 2024-12-10 22:35:49

我是这样做的。它有点粗糙,但效果很好。 :)

    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JSlider;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;

    /**
     *
     * @author VirtejF
     */
    public class MYJslider extends JSlider{

MYJslider ref=this;
JPopupMenu info=new JPopupMenu();
JMenuItem value=new JMenuItem();
int Ypos=0;

public MYJslider()
{
    super();
    //this.addMouseListener(new Handlerclass());
    this.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent ce) {
        JSlider slider = (JSlider)ce.getSource();
            slider.setToolTipText(slider.getValue()+"");
            value.setText(slider.getValue()+"");
    }
});
    this.addMouseListener(new Handlerclass());
    this.addMouseMotionListener(new mouseMotion());
    info.add(value);


}

public class Handlerclass implements MouseListener{

    @Override
    public void mousePressed(MouseEvent e) {
         info.setVisible(true);
         Ypos=e.getY()+20;
         info.show(e.getComponent(), e.getX()+15, Ypos); 
        }

    @Override
    public void mouseReleased(MouseEvent e) {
        info.setVisible(false);
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

}

public class mouseMotion implements MouseMotionListener
{

    @Override
    public void mouseDragged(MouseEvent e) {
        //if(e.getX() > ref.getX() && e.getX() < ref.getX()+ref.getWidth())
        info.show(e.getComponent(), e.getX()+15, Ypos);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

}

}

Here is how I did it. It is a little rough but it works fine. :)

    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JSlider;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;

    /**
     *
     * @author VirtejF
     */
    public class MYJslider extends JSlider{

MYJslider ref=this;
JPopupMenu info=new JPopupMenu();
JMenuItem value=new JMenuItem();
int Ypos=0;

public MYJslider()
{
    super();
    //this.addMouseListener(new Handlerclass());
    this.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent ce) {
        JSlider slider = (JSlider)ce.getSource();
            slider.setToolTipText(slider.getValue()+"");
            value.setText(slider.getValue()+"");
    }
});
    this.addMouseListener(new Handlerclass());
    this.addMouseMotionListener(new mouseMotion());
    info.add(value);


}

public class Handlerclass implements MouseListener{

    @Override
    public void mousePressed(MouseEvent e) {
         info.setVisible(true);
         Ypos=e.getY()+20;
         info.show(e.getComponent(), e.getX()+15, Ypos); 
        }

    @Override
    public void mouseReleased(MouseEvent e) {
        info.setVisible(false);
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

}

public class mouseMotion implements MouseMotionListener
{

    @Override
    public void mouseDragged(MouseEvent e) {
        //if(e.getX() > ref.getX() && e.getX() < ref.getX()+ref.getWidth())
        info.show(e.getComponent(), e.getX()+15, Ypos);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

}

}

影子的影子 2024-12-10 22:35:49

这很好用。这是源链接

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.plaf.basic.BasicSliderUI;

class JSliderEX extends JFrame
{
  JSlider slider = new JSlider(0,100,0);
  public JSliderEX()
  {
    super("JSliderEX");
    setSize(300,80);
    setLocation(400,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MySliderUI ms = new MySliderUI(slider,this);
    slider.setUI(ms);
    slider.setMajorTickSpacing(10);
    slider.setMinorTickSpacing(5);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    getContentPane().add(slider);
  }
  public static void main(String args[]){new JSliderEX().setVisible(true);}
}
class MySliderUI extends BasicSliderUI implements MouseMotionListener, MouseListener
{
  JPopupMenu pop = new JPopupMenu();
  JFrame parent;
  JLabel lblPop = new JLabel("",SwingConstants.CENTER);
  public MySliderUI(JSlider slider,JFrame p)
  {
    super(slider);
    slider.addMouseMotionListener(this);
    slider.addMouseListener(this);
    parent = p;
    JPanel jp = new JPanel();
    lblPop.setPreferredSize(new Dimension(30,12));
    jp.add(lblPop);
    pop.add(jp);
  }
  public void setPop(MouseEvent me)
  {
    lblPop.setText(""+slider.getValue());
    pop.setLocation((int)(parent.getX()+slider.getX()+me.getX()-10),
                   (int)(parent.getY()+slider.getY()));
  }
  public void mouseDragged(MouseEvent me){setPop(me);}
  public void mouseMoved(MouseEvent me){}
  public void mousePressed(MouseEvent me){pop.setVisible(true);setPop(me);}
  public void mouseClicked(MouseEvent me){}
  public void mouseReleased(MouseEvent me){pop.setVisible(false);}
  public void mouseEntered(MouseEvent me){}
  public void mouseExited(MouseEvent me){}
}

This works fine. Here is the source link

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.plaf.basic.BasicSliderUI;

class JSliderEX extends JFrame
{
  JSlider slider = new JSlider(0,100,0);
  public JSliderEX()
  {
    super("JSliderEX");
    setSize(300,80);
    setLocation(400,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MySliderUI ms = new MySliderUI(slider,this);
    slider.setUI(ms);
    slider.setMajorTickSpacing(10);
    slider.setMinorTickSpacing(5);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    getContentPane().add(slider);
  }
  public static void main(String args[]){new JSliderEX().setVisible(true);}
}
class MySliderUI extends BasicSliderUI implements MouseMotionListener, MouseListener
{
  JPopupMenu pop = new JPopupMenu();
  JFrame parent;
  JLabel lblPop = new JLabel("",SwingConstants.CENTER);
  public MySliderUI(JSlider slider,JFrame p)
  {
    super(slider);
    slider.addMouseMotionListener(this);
    slider.addMouseListener(this);
    parent = p;
    JPanel jp = new JPanel();
    lblPop.setPreferredSize(new Dimension(30,12));
    jp.add(lblPop);
    pop.add(jp);
  }
  public void setPop(MouseEvent me)
  {
    lblPop.setText(""+slider.getValue());
    pop.setLocation((int)(parent.getX()+slider.getX()+me.getX()-10),
                   (int)(parent.getY()+slider.getY()));
  }
  public void mouseDragged(MouseEvent me){setPop(me);}
  public void mouseMoved(MouseEvent me){}
  public void mousePressed(MouseEvent me){pop.setVisible(true);setPop(me);}
  public void mouseClicked(MouseEvent me){}
  public void mouseReleased(MouseEvent me){pop.setVisible(false);}
  public void mouseEntered(MouseEvent me){}
  public void mouseExited(MouseEvent me){}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文