Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JRadioButton in JTable

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

i wan to make a JTable with JRadioButon such that one or two cells will not have any JRadio Button depending upon value I get from Database. When same code is run with Metal look and feel it works..but with Nimbus it does not.. If getValueAt() method returns null I get NullPointerException... I tried to change the code..(basic code i got after googling)

Please see the code:



when I run this code I get NullPointerException if return(Object)new JRadioButton("NoData"); is removed from method public Object getValueAt(int row, int col). The NullPointerException is removed by adding line return(Object)new JRadioButton("NoData"); in the code but it makes a JRadioButton in the table with lable No data..What I want is the cellshould not have any value at all..
I changed the code of public Object getValueAt(int row, int col). to

where useLessobj is an object created earlier..But it gives me ClassCastException in

public Component getTableCellRendererComponent(
JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column)
{
if (value==null) return null;
return (Component)value;
}
}

I tried returning a JLable from getTableCellRendererComponent method but again ClassCastException is what i get.


Please suggest correct way of doing this..

 
Sheriff
Posts: 22575
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should never return null from getTableCellRenderer / getTableCellEditor methods. If returning a JLabel throws an exception, please post that exception.
 
priyas sakorikar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was getting ClassCastException JLable cannot be cast to JRadioButton.

I have done some more experiments.Please see the code below..


This code creates a JTextField where array data has null value..It creates the table without exceptions .Now when i click on JRadioButton just below JTextBox..The textbox converts to RadioButton..
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have simply mixed up several things. A table has three things:
1) A table model:
This contains the data of the table. Data would usually be a String/Integer or your custom object.
You have added a JRadioButton itself to the model. This is wrong.

2) Renderer:
This helps you to display the model (data). In the getTableCellRendererComponent method, you have to return a component. In your case, if the value (which comes from the model) is null, then you have create a JLabel, set an empty text and return it. Otherwise, create the radio button and return it.

3) Editor:
This is the component that handles editing when the cell is clicked on. Here, you are extending DefaultCellEditor (which is a JTexField), then you pass a JCheckBox to the constructor of this editor, and finally you return a JRadioButton from the getTableCellEditorComponent method. This is total confusion.

I would suggest you read How to use Tables and then set about correcting your code.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic