You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Edvin Syse <ed...@sysedata.no> on 2011/05/13 08:53:21 UTC
How to make middle mousebutton close a closable tab in a TabPane?
I would like a middle mousebutton click to close the active tab in a
TabPane, using the same action that happens when one click the X in the
tab. Where should I attach a listener to make this happen?
-- Edvin
Re: How to make middle mousebutton close a closable tab in a TabPane?
Posted by Edvin Syse <ed...@sysedata.no>.
Right, got it :) Thanks again!
tabPane.getComponentMouseButtonListeners().add(new
ComponentMouseButtonListener.Adapter() {
public boolean mouseClick(Component component, Mouse.Button
button, int x, int y, int count) {
Component descendant = tabPane.getDescendantAt(x, y);
if (descendant instanceof TerraTabPaneSkin.TabButton &&
button == Mouse.Button.MIDDLE)
tabPane.getTabs().remove(descendant.getParent().indexOf(descendant), 1);
return false;
}
});
Den 13.05.2011 14:10, skrev Greg Brown:
> descendant
>
> On May 13, 2011, at 8:06 AM, Edvin Syse wrote:
>
>> Can't get my head around that one... The button is descendant in my example, right? You want me to call descendant.getParent().indexOf(), but what is the argument to indexOf()?
>>
>> Den 13.05.2011 13:56, skrev Greg Brown:
>>> Again, this is a hack, but since you have the tab button, you can call indexOf() on the button's parent to get the tab index. That way you don't need to use reflection.
>>>
>>> On May 13, 2011, at 7:52 AM, Edvin Syse wrote:
>>>
>>>> Got it, thanks :) I had to use reflection to get the correct argument for tabPane.remove(), does it look right to you?
>>>>
>>>>
>>>> tabPane.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener.Adapter() {
>>>> public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
>>>> Component descendant = tabPane.getDescendantAt(x, y);
>>>> if (descendant instanceof TerraTabPaneSkin.TabButton&& button == Mouse.Button.MIDDLE) {
>>>> try {
>>>> Field tabField = TerraTabPaneSkin.TabButton.class.getDeclaredField("tab");
>>>> tabField.setAccessible(true);
>>>> tabPane.getTabs().remove((Component) tabField.get(descendant));
>>>> } catch (Exception ignored) {
>>>> }
>>>> }
>>>> return false;
>>>> }
>>>> });
>>>>
>>>>
>>>> Den 13.05.2011 13:34, skrev Greg Brown:
>>>>> Since the buttons are effectively private to the tab pane's skin, the "right" way to do this would probably be to create a custom subclass of TerraTabPaneSkin that knows about the buttons and can attach a listener to them. However, you could probably hack support for this by registering a mouse listener on the TabPane itself, then calling tabPane.getComponentAt(x, y). If the component is an instance of TerraTabPaneSkin.TabButton, then the user has clicked on the tab button.
>>>>>
>>>>> On May 13, 2011, at 2:53 AM, Edvin Syse wrote:
>>>>>
>>>>>> I would like a middle mousebutton click to close the active tab in a TabPane, using the same action that happens when one click the X in the tab. Where should I attach a listener to make this happen?
>>>>>>
>>>>>> -- Edvin
Re: How to make middle mousebutton close a closable tab in a TabPane?
Posted by Greg Brown <gk...@verizon.net>.
descendant
On May 13, 2011, at 8:06 AM, Edvin Syse wrote:
> Can't get my head around that one... The button is descendant in my example, right? You want me to call descendant.getParent().indexOf(), but what is the argument to indexOf()?
>
> Den 13.05.2011 13:56, skrev Greg Brown:
>> Again, this is a hack, but since you have the tab button, you can call indexOf() on the button's parent to get the tab index. That way you don't need to use reflection.
>>
>> On May 13, 2011, at 7:52 AM, Edvin Syse wrote:
>>
>>> Got it, thanks :) I had to use reflection to get the correct argument for tabPane.remove(), does it look right to you?
>>>
>>>
>>> tabPane.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener.Adapter() {
>>> public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
>>> Component descendant = tabPane.getDescendantAt(x, y);
>>> if (descendant instanceof TerraTabPaneSkin.TabButton&& button == Mouse.Button.MIDDLE) {
>>> try {
>>> Field tabField = TerraTabPaneSkin.TabButton.class.getDeclaredField("tab");
>>> tabField.setAccessible(true);
>>> tabPane.getTabs().remove((Component) tabField.get(descendant));
>>> } catch (Exception ignored) {
>>> }
>>> }
>>> return false;
>>> }
>>> });
>>>
>>>
>>> Den 13.05.2011 13:34, skrev Greg Brown:
>>>> Since the buttons are effectively private to the tab pane's skin, the "right" way to do this would probably be to create a custom subclass of TerraTabPaneSkin that knows about the buttons and can attach a listener to them. However, you could probably hack support for this by registering a mouse listener on the TabPane itself, then calling tabPane.getComponentAt(x, y). If the component is an instance of TerraTabPaneSkin.TabButton, then the user has clicked on the tab button.
>>>>
>>>> On May 13, 2011, at 2:53 AM, Edvin Syse wrote:
>>>>
>>>>> I would like a middle mousebutton click to close the active tab in a TabPane, using the same action that happens when one click the X in the tab. Where should I attach a listener to make this happen?
>>>>>
>>>>> -- Edvin
Re: How to make middle mousebutton close a closable tab in a TabPane?
Posted by Greg Brown <gk...@verizon.net>.
You want to do something like this:
Component descendant = tabPane.getDescendantAt(x, y);
if (descendant instanceof TerraTabPaneSkin.TabButton) {
int tabIndex = descendant.getParent().indexOf(descendant);
tabPane.getTabs().remove(tabIndex);
}
On May 13, 2011, at 8:09 AM, Edvin Syse wrote:
> .. or maybe you meant:
>
> tabs.getTabs().indexOf(descendant.getParent()).. but that returns -1. The parent of descendant isn't the argument to tabPane.remove(), as far as I understand, the descendant has a child (in the field member tab) that is the actual component I can remove.
>
> Den 13.05.2011 14:06, skrev Edvin Syse:
>> Can't get my head around that one... The button is descendant in my example, right? You want me to call descendant.getParent().indexOf(), but what is the argument to indexOf()?
>>
>> Den 13.05.2011 13:56, skrev Greg Brown:
>>> Again, this is a hack, but since you have the tab button, you can call indexOf() on the button's parent to get the tab index. That way you don't need to use reflection.
>>>
>>> On May 13, 2011, at 7:52 AM, Edvin Syse wrote:
>>>
>>>> Got it, thanks :) I had to use reflection to get the correct argument for tabPane.remove(), does it look right to you?
>>>>
>>>>
>>>> tabPane.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener.Adapter() {
>>>> public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
>>>> Component descendant = tabPane.getDescendantAt(x, y);
>>>> if (descendant instanceof TerraTabPaneSkin.TabButton&& button == Mouse.Button.MIDDLE) {
>>>> try {
>>>> Field tabField = TerraTabPaneSkin.TabButton.class.getDeclaredField("tab");
>>>> tabField.setAccessible(true);
>>>> tabPane.getTabs().remove((Component) tabField.get(descendant));
>>>> } catch (Exception ignored) {
>>>> }
>>>> }
>>>> return false;
>>>> }
>>>> });
>>>>
>>>>
>>>> Den 13.05.2011 13:34, skrev Greg Brown:
>>>>> Since the buttons are effectively private to the tab pane's skin, the "right" way to do this would probably be to create a custom subclass of TerraTabPaneSkin that knows about the buttons and can attach a listener to them. However, you could probably hack support for this by registering a mouse listener on the TabPane itself, then calling tabPane.getComponentAt(x, y). If the component is an instance of TerraTabPaneSkin.TabButton, then the user has clicked on the tab button.
>>>>>
>>>>> On May 13, 2011, at 2:53 AM, Edvin Syse wrote:
>>>>>
>>>>>> I would like a middle mousebutton click to close the active tab in a TabPane, using the same action that happens when one click the X in the tab. Where should I attach a listener to make this happen?
>>>>>>
>>>>>> -- Edvin
Re: How to make middle mousebutton close a closable tab in a TabPane?
Posted by Edvin Syse <ed...@sysedata.no>.
.. or maybe you meant:
tabs.getTabs().indexOf(descendant.getParent()).. but that returns -1.
The parent of descendant isn't the argument to tabPane.remove(), as far
as I understand, the descendant has a child (in the field member tab)
that is the actual component I can remove.
Den 13.05.2011 14:06, skrev Edvin Syse:
> Can't get my head around that one... The button is descendant in my
> example, right? You want me to call descendant.getParent().indexOf(),
> but what is the argument to indexOf()?
>
> Den 13.05.2011 13:56, skrev Greg Brown:
>> Again, this is a hack, but since you have the tab button, you can
>> call indexOf() on the button's parent to get the tab index. That way
>> you don't need to use reflection.
>>
>> On May 13, 2011, at 7:52 AM, Edvin Syse wrote:
>>
>>> Got it, thanks :) I had to use reflection to get the correct
>>> argument for tabPane.remove(), does it look right to you?
>>>
>>>
>>> tabPane.getComponentMouseButtonListeners().add(new
>>> ComponentMouseButtonListener.Adapter() {
>>> public boolean mouseClick(Component component,
>>> Mouse.Button button, int x, int y, int count) {
>>> Component descendant = tabPane.getDescendantAt(x, y);
>>> if (descendant instanceof
>>> TerraTabPaneSkin.TabButton&& button == Mouse.Button.MIDDLE) {
>>> try {
>>> Field tabField =
>>> TerraTabPaneSkin.TabButton.class.getDeclaredField("tab");
>>> tabField.setAccessible(true);
>>> tabPane.getTabs().remove((Component)
>>> tabField.get(descendant));
>>> } catch (Exception ignored) {
>>> }
>>> }
>>> return false;
>>> }
>>> });
>>>
>>>
>>> Den 13.05.2011 13:34, skrev Greg Brown:
>>>> Since the buttons are effectively private to the tab pane's skin,
>>>> the "right" way to do this would probably be to create a custom
>>>> subclass of TerraTabPaneSkin that knows about the buttons and can
>>>> attach a listener to them. However, you could probably hack support
>>>> for this by registering a mouse listener on the TabPane itself,
>>>> then calling tabPane.getComponentAt(x, y). If the component is an
>>>> instance of TerraTabPaneSkin.TabButton, then the user has clicked
>>>> on the tab button.
>>>>
>>>> On May 13, 2011, at 2:53 AM, Edvin Syse wrote:
>>>>
>>>>> I would like a middle mousebutton click to close the active tab in
>>>>> a TabPane, using the same action that happens when one click the X
>>>>> in the tab. Where should I attach a listener to make this happen?
>>>>>
>>>>> -- Edvin
Re: How to make middle mousebutton close a closable tab in a TabPane?
Posted by Edvin Syse <ed...@sysedata.no>.
Can't get my head around that one... The button is descendant in my
example, right? You want me to call descendant.getParent().indexOf(),
but what is the argument to indexOf()?
Den 13.05.2011 13:56, skrev Greg Brown:
> Again, this is a hack, but since you have the tab button, you can call indexOf() on the button's parent to get the tab index. That way you don't need to use reflection.
>
> On May 13, 2011, at 7:52 AM, Edvin Syse wrote:
>
>> Got it, thanks :) I had to use reflection to get the correct argument for tabPane.remove(), does it look right to you?
>>
>>
>> tabPane.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener.Adapter() {
>> public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
>> Component descendant = tabPane.getDescendantAt(x, y);
>> if (descendant instanceof TerraTabPaneSkin.TabButton&& button == Mouse.Button.MIDDLE) {
>> try {
>> Field tabField = TerraTabPaneSkin.TabButton.class.getDeclaredField("tab");
>> tabField.setAccessible(true);
>> tabPane.getTabs().remove((Component) tabField.get(descendant));
>> } catch (Exception ignored) {
>> }
>> }
>> return false;
>> }
>> });
>>
>>
>> Den 13.05.2011 13:34, skrev Greg Brown:
>>> Since the buttons are effectively private to the tab pane's skin, the "right" way to do this would probably be to create a custom subclass of TerraTabPaneSkin that knows about the buttons and can attach a listener to them. However, you could probably hack support for this by registering a mouse listener on the TabPane itself, then calling tabPane.getComponentAt(x, y). If the component is an instance of TerraTabPaneSkin.TabButton, then the user has clicked on the tab button.
>>>
>>> On May 13, 2011, at 2:53 AM, Edvin Syse wrote:
>>>
>>>> I would like a middle mousebutton click to close the active tab in a TabPane, using the same action that happens when one click the X in the tab. Where should I attach a listener to make this happen?
>>>>
>>>> -- Edvin
Re: How to make middle mousebutton close a closable tab in a TabPane?
Posted by Greg Brown <gk...@verizon.net>.
Again, this is a hack, but since you have the tab button, you can call indexOf() on the button's parent to get the tab index. That way you don't need to use reflection.
On May 13, 2011, at 7:52 AM, Edvin Syse wrote:
> Got it, thanks :) I had to use reflection to get the correct argument for tabPane.remove(), does it look right to you?
>
>
> tabPane.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener.Adapter() {
> public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
> Component descendant = tabPane.getDescendantAt(x, y);
> if (descendant instanceof TerraTabPaneSkin.TabButton && button == Mouse.Button.MIDDLE) {
> try {
> Field tabField = TerraTabPaneSkin.TabButton.class.getDeclaredField("tab");
> tabField.setAccessible(true);
> tabPane.getTabs().remove((Component) tabField.get(descendant));
> } catch (Exception ignored) {
> }
> }
> return false;
> }
> });
>
>
> Den 13.05.2011 13:34, skrev Greg Brown:
>> Since the buttons are effectively private to the tab pane's skin, the "right" way to do this would probably be to create a custom subclass of TerraTabPaneSkin that knows about the buttons and can attach a listener to them. However, you could probably hack support for this by registering a mouse listener on the TabPane itself, then calling tabPane.getComponentAt(x, y). If the component is an instance of TerraTabPaneSkin.TabButton, then the user has clicked on the tab button.
>>
>> On May 13, 2011, at 2:53 AM, Edvin Syse wrote:
>>
>>> I would like a middle mousebutton click to close the active tab in a TabPane, using the same action that happens when one click the X in the tab. Where should I attach a listener to make this happen?
>>>
>>> -- Edvin
Re: How to make middle mousebutton close a closable tab in a TabPane?
Posted by Edvin Syse <ed...@sysedata.no>.
Got it, thanks :) I had to use reflection to get the correct argument
for tabPane.remove(), does it look right to you?
tabPane.getComponentMouseButtonListeners().add(new
ComponentMouseButtonListener.Adapter() {
public boolean mouseClick(Component component, Mouse.Button
button, int x, int y, int count) {
Component descendant = tabPane.getDescendantAt(x, y);
if (descendant instanceof TerraTabPaneSkin.TabButton &&
button == Mouse.Button.MIDDLE) {
try {
Field tabField =
TerraTabPaneSkin.TabButton.class.getDeclaredField("tab");
tabField.setAccessible(true);
tabPane.getTabs().remove((Component)
tabField.get(descendant));
} catch (Exception ignored) {
}
}
return false;
}
});
Den 13.05.2011 13:34, skrev Greg Brown:
> Since the buttons are effectively private to the tab pane's skin, the "right" way to do this would probably be to create a custom subclass of TerraTabPaneSkin that knows about the buttons and can attach a listener to them. However, you could probably hack support for this by registering a mouse listener on the TabPane itself, then calling tabPane.getComponentAt(x, y). If the component is an instance of TerraTabPaneSkin.TabButton, then the user has clicked on the tab button.
>
> On May 13, 2011, at 2:53 AM, Edvin Syse wrote:
>
>> I would like a middle mousebutton click to close the active tab in a TabPane, using the same action that happens when one click the X in the tab. Where should I attach a listener to make this happen?
>>
>> -- Edvin
Re: How to make middle mousebutton close a closable tab in a TabPane?
Posted by Greg Brown <gk...@verizon.net>.
Since the buttons are effectively private to the tab pane's skin, the "right" way to do this would probably be to create a custom subclass of TerraTabPaneSkin that knows about the buttons and can attach a listener to them. However, you could probably hack support for this by registering a mouse listener on the TabPane itself, then calling tabPane.getComponentAt(x, y). If the component is an instance of TerraTabPaneSkin.TabButton, then the user has clicked on the tab button.
On May 13, 2011, at 2:53 AM, Edvin Syse wrote:
> I would like a middle mousebutton click to close the active tab in a TabPane, using the same action that happens when one click the X in the tab. Where should I attach a listener to make this happen?
>
> -- Edvin