4

Does anyone have a coded implementation of SplitButton?

I've tried searching some implementations but none of them work. (e.g. JSplitButton).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1551085
  • 61
  • 1
  • 3
  • Every implementation I've seen doesn't transpose will across UIs – MadProgrammer Jul 25 '12 at 09:11
  • 1
    what's wrong with it? Note I didn't try the one you linked to - but for any reasonable suggestion it would be useful to know waht exactly you aren't satisfied with :-) – kleopatra Jul 25 '12 at 09:20
  • I can't click the arrow button. I just run the included demo and it doesn't work. Try it. – user1551085 Jul 25 '12 at 09:27
  • For anyone searching for a simple single-file implementation in 2020. I found one originally posted by [MadProgrammer](https://stackoverflow.com/users/992484/madprogrammer) and modified it to work for me with [FlatLaf](https://www.formdev.com/flatlaf/) L&F, but it seems to work rather well with others too. [Link to StackOverflow question](https://stackoverflow.com/a/66303093/3452003) [![Component image](https://i.stack.imgur.com/zAMue.png)](https://i.stack.imgur.com/zAMue.png) – DUDSS Feb 21 '21 at 14:15

2 Answers2

5

Demo not working != splitButton not working :-)

splitButton = new JSplitButton("Split Button Demo");
splitButton.setPopupMenu(jPopupMenu2);
add(splitButton);

(Note: this is not a recommendation of that particular splitButton implementation, just a reminder that you should be a bit more careful in evaluating the projects you are finding. If you can't for some reason, you might consider using a commercial product, like Synthetica or JIDE, just to name two)

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • After reading the answer, I wanted to ask "why wouldn't you recommend it?". But right now, after actually reading the source, and seeing how it's done... I have my answer. No proper UI class, no proper size handling (putting it in a BoxLayout gives funny results), no actual separation of the "pressed" or hover state. It's really a basic hack, done in one class. Well, off to improve that myself! – Gnoupi Jan 11 '13 at 11:01
0

You should try using a JComboBox. They work for more than just simple drop down, but it should do the trick.

  • It seems many (including me) are not familar with this feature. Could you give an example? – Queeg Jul 26 '23 at 06:15