From: Jim Janney on
In Eclipse when you display a tool tip you get a different message if
the control or shift keys are pressed. I'm trying to do the same
thing in a Swing program. My first try was to subclass JTextField
and override getToolTipText(MouseEvent), something like this:

public class CustomTextField extends JTextField {
// usual constructors

@Override
public String getToolTipText(MouseEvent event) {
String result;
if (event.isControlDown() && getAlternateText() != null) {
result = getAlternateText();
} else {
result = super.getToolTipText(event);
}
return result;
}

public String getAlternateText() {
return "get your alternate text here";
}
}

This works for text fields but it's hard to extend it to other kinds
of components. If you have a non-editable combo box the class that
receives the method call is some subclass of JButton, for example
com.jgoodies.looks.plastic.PlasticComboBoxButton: exactly which
one you get depends on the look and feel.

I'm wondering if this is the wrong approach and I should be doing
something with a MouseListener instead. Has anyone else done anything
like this?

--
Jim Janney
From: Knute Johnson on
On 3/17/2010 1:59 PM, Jim Janney wrote:
> In Eclipse when you display a tool tip you get a different message if
> the control or shift keys are pressed. I'm trying to do the same
> thing in a Swing program. My first try was to subclass JTextField
> and override getToolTipText(MouseEvent), something like this:
>
> public class CustomTextField extends JTextField {
> // usual constructors
>
> @Override
> public String getToolTipText(MouseEvent event) {
> String result;
> if (event.isControlDown()&& getAlternateText() != null) {
> result = getAlternateText();
> } else {
> result = super.getToolTipText(event);
> }
> return result;
> }
>
> public String getAlternateText() {
> return "get your alternate text here";
> }
> }
>
> This works for text fields but it's hard to extend it to other kinds
> of components. If you have a non-editable combo box the class that
> receives the method call is some subclass of JButton, for example
> com.jgoodies.looks.plastic.PlasticComboBoxButton: exactly which
> one you get depends on the look and feel.
>
> I'm wondering if this is the wrong approach and I should be doing
> something with a MouseListener instead. Has anyone else done anything
> like this?
>

I tried it with a MouseListener and it works fine.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel {
private boolean alt;

public test() {
super(new GridBagLayout());

setPreferredSize(new Dimension(400,300));

JButton b = new JButton("Press Me");
b.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent me) {
JComponent c = (JComponent)me.getSource();
if (me.isAltDown())
c.setToolTipText("ALT is pressed");
else
c.setToolTipText("ALT isn't pressed!");

}
});
add(b);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t);
f.pack();
f.setVisible(true);
}
});
}
}

--

Knute Johnson
email s/nospam/knute2010/

From: Jim Janney on
Knute Johnson <nospam(a)rabbitbrush.frazmtn.com> writes:

> On 3/17/2010 1:59 PM, Jim Janney wrote:
>> In Eclipse when you display a tool tip you get a different message if
>> the control or shift keys are pressed. I'm trying to do the same
>> thing in a Swing program. My first try was to subclass JTextField
>> and override getToolTipText(MouseEvent), something like this:
>>
>> public class CustomTextField extends JTextField {
>> // usual constructors
>>
>> @Override
>> public String getToolTipText(MouseEvent event) {
>> String result;
>> if (event.isControlDown()&& getAlternateText() != null) {
>> result = getAlternateText();
>> } else {
>> result = super.getToolTipText(event);
>> }
>> return result;
>> }
>>
>> public String getAlternateText() {
>> return "get your alternate text here";
>> }
>> }
>>
>> This works for text fields but it's hard to extend it to other kinds
>> of components. If you have a non-editable combo box the class that
>> receives the method call is some subclass of JButton, for example
>> com.jgoodies.looks.plastic.PlasticComboBoxButton: exactly which
>> one you get depends on the look and feel.
>>
>> I'm wondering if this is the wrong approach and I should be doing
>> something with a MouseListener instead. Has anyone else done anything
>> like this?
>>
>
> I tried it with a MouseListener and it works fine.
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
>
> public class test extends JPanel {
> private boolean alt;
>
> public test() {
> super(new GridBagLayout());
>
> setPreferredSize(new Dimension(400,300));
>
> JButton b = new JButton("Press Me");
> b.addMouseListener(new MouseAdapter() {
> public void mouseEntered(MouseEvent me) {
> JComponent c = (JComponent)me.getSource();
> if (me.isAltDown())
> c.setToolTipText("ALT is pressed");
> else
> c.setToolTipText("ALT isn't pressed!");
>
> }
> });
> add(b);
> }
>
> public static void main(String[] args) {
> EventQueue.invokeLater(new Runnable() {
> public void run() {
> JFrame f = new JFrame();
> f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> test t = new test();
> f.add(t);
> f.pack();
> f.setVisible(true);
> }
> });
> }
> }

Thanks. That still doesn't work with a JComboBox, but that turns out
to be because nothing works with JComboBoxes, as is copiously
described in bug ID 4144505, where Sun says "yes we know, but we're
not going to fix it."

RANT If they're not going to fix this they should at least document
it. I read through the tutorial on how to use mouse listeners and
nowhere does it say "oh by the way, this doesn't work with combo
boxes, too bad for you." So I waste a day and a half rediscovering
what everyone else has known for ten years.

And yes, there are workarounds, I'm looking at them now. We let our
users change the L&F at runtime, so I need something that works with
that.

--
Jim Janney
From: Knute Johnson on
On 3/18/2010 9:32 AM, Jim Janney wrote:
> Thanks. That still doesn't work with a JComboBox, but that turns out
> to be because nothing works with JComboBoxes, as is copiously
> described in bug ID 4144505, where Sun says "yes we know, but we're
> not going to fix it."

Sure it does.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel {
String[] items = {"One","Two","Three","Four","Five"};

public test() {
super(new GridBagLayout());

setPreferredSize(new Dimension(400,300));

JComboBox b = new JComboBox(items);
b.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent me) {
JComponent c = (JComponent)me.getSource();
if (me.isAltDown())
c.setToolTipText("ALT is pressed");
else
c.setToolTipText("ALT isn't pressed!");

}
});
add(b);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t);
f.pack();
f.setVisible(true);
}
});
}
}

--

Knute Johnson
email s/nospam/knute2010/

From: Knute Johnson on
On 3/18/2010 9:32 AM, Jim Janney wrote:
> Thanks. That still doesn't work with a JComboBox, but that turns out
> to be because nothing works with JComboBoxes, as is copiously
> described in bug ID 4144505, where Sun says "yes we know, but we're
> not going to fix it."

Sure it does.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel {
String[] items = {"One","Two","Three","Four","Five"};

public test() {
super(new GridBagLayout());

setPreferredSize(new Dimension(400,300));

JComboBox b = new JComboBox(items);
b.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent me) {
JComponent c = (JComponent)me.getSource();
if (me.isAltDown())
c.setToolTipText("ALT is pressed");
else
c.setToolTipText("ALT isn't pressed!");

}
});
add(b);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t);
f.pack();
f.setVisible(true);
}
});
}
}

--

Knute Johnson
email s/nospam/knute2010/

 |  Next  |  Last
Pages: 1 2 3
Prev: Java Type System
Next: Evisu original jeans Wholesale