How do I make a list with checkboxes in Java Swing?

0 votes
Hii team,

What would be the best way to have a list of items with a checkbox each in Java Swing?
May 7 in Java by kartik
• 11,890 points
61 views

1 answer to this question.

0 votes

Hii @kartik,

  • Create a custom ListCellRenderer and asign it to the JList.
  • This custom ListCellRenderer must return a JCheckbox in the implementantion of getListCellRendererComponent(...) method.
  • But this JCheckbox will not be editable, is a simple paint in the screen is up to you to choose when this JCheckbox must be 'ticked' or not,

For example, show it ticked when the row is selected (parameter isSelected), but this way the check status will no be maintained if the selection changes. Its better to show it checked consulting the data below the ListModel, but then is up to you to implement the method who changes the check status of the data, and notify the change to the JList to be repainted.

Hope it help!!

answered May 7 by Niroj
• 23,950 points

Related Questions In Java

0 votes
2 answers

How do I get distinct element from list in java?

You should use a Set. it doesn't contain duplicates. If ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 53,110 points
414 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,420 points

edited Nov 6, 2018 by Omkar 22,166 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 53,110 points
390 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,580 points
201 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 963 views
0 votes
0 answers
0 votes
1 answer

how to do global variable declaration in java

Hello,​ Global varriable which is defined after the ...READ MORE

answered Apr 3 in Java by Niroj
• 23,950 points
42 views
0 votes
1 answer

How to run a .java file from command line passing arguments in Maven?

Hello @kartik, You could run: mvn exec:exec -Dexec.args="arg1". This will ...READ MORE

answered 3 days ago in Java by Niroj
• 23,950 points
35 views