How to present a hierarchical String list with checkboxes in Java Swing?
-
06-07-2021 - |
Question
So. The ideas have been a few.. no good ones.. So here is the problem i have incountered and can not figure out a solution for.
I need to have a txt file containing a hierarchical list of things to do, looking something like this:
- Ask a question
- Before question asked, check similar questions
- Formulate the question so that the problem is clearly shown.
- Post the question
- Hold your thumbs that this might save your day.
The txt file should in the program fill a list shown in a JPanel and after each step have a checkbox to comfirm "I have been a good boy and done this". The list can change over time and grow and shrink, so the solution needs a scrollbar. The list should also contaion a capability to show more the one row.
The solution im working on is done in Jave compiler 1.6 and im using Swing for my layout. Ideas i have had so far are: Modified JTable... and the other solutions i am asshamed of.. And thats about it.. ..
Solution
JTree
with custom renderer. The renderer should be a panle with checkbox and text area.
OTHER TIPS
have look at JTree
have to override JTree Renderer and Editor (comcept is similair for
JTable
andJTree
)all data are stored in the
DefaultTreeModel
examples about Tree Model,Tree Renderer Editor, Tree and here
Also consider org.netbeans.swing.outline.Outline
, discussed here and illustrated below. It's not apparent in the screenshot, but the the Outline
instance has been added to a JScrollPane
.
I would say JTreeTable is a right choice. It will provide a lot of power and flexibility. Please see some very basic examples:
- http://www.ibm.com/developerworks/java/library/j-5things14/index.html
- http://www.java2s.com/Code/Java/Swing-Components/JTreeTablecomponent.htm
For any serious purpose please use a mature implementation provided, for example, by a SwingLab project:
Please also see the following answer: