How to create a custom Swing Component [closed]

≯℡__Kan透↙ 提交于 2019-11-27 07:04:52
John K

This article, How To Write a Custom Swing Component should be really helpful. It covers replicating a Windows slider control that has both discrete steps and a continuous range.

alphazero

The JDK is open source. To understand how you write a Swing component, there is no better place to look than the source for Swing components. In general, when you create custom components, you are interested in one or more of the following: {interaction semantics, visual representation, compositional structure}

Start with the simplest cases:

Passive components: JLabel
Interactive components: JButton

JLabel will demonstrate the barebones extension of JComponent for custom drawing of a component; in this case, it is drawing the 'text' string value as its representation.

JButton will demonstrate the barebones extension for user interaction in addition to drawing the component.

Filthy Rich Clients is an excellent, very readable book on this topic. It's more about improving the look and feel of Swing components than creating entirely new ones, but it does get into the guts of Swing and provides a lot of practical examples.

This is a pretty open question, the short simple answer is that you subclass JComponent or one of it's descendants and add the functionality that you require. Obviously, depending on what you're wanting to do this may vary in difficulty. For starters I recommend you read Sun's tutorial on using Swing components where there are examples on how to use scroll panes and popup menus. Once you have read through those and experimented you might have more specific questions that will be easier for people to provide considered answers to.

If the two components in your mock up are all you are wanting to implement then giving us some better idea of how you want them to function will yield better answers.

Read Swing Hacks: Tips & Tools for Building Killer GUIs (Marinacci, Adamson). IMHO an essential book for effectively working with swing in general.

kralyk

Personally I find the above answers too general or vague. Also, as someone is inevitably going to bump into this thread just as I did, I might as well provide what I found:

Hope this helps...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!