Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7778751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:28:33+00:00 2026-06-01T18:28:33+00:00

I’m still fairly new to Java and have a question in regards to JTable

  • 0

I’m still fairly new to Java and have a question in regards to JTable (more specifically JXTable) and sorting rows by column class with mixed data type… Let me elaborate…

I have a JXTable that holds data for a product listing. This table has a column for price which I have set to String.class only so that I can display a price with a ‘$’ prepended.

The problem I’m having is when the rows are sorted by price, they are not sorted as Doubles, but rather they are sorted as strings so these values:

89.85, 179.70, 299.40, 478.80

are sorted as:

179.70, 299.40, 478.80, 89.85 (Ascending)
and
89.85, 478.80, 299.40, 179.70 (Descending)

What I would like to do is remove the ‘$’ at the time of sorting and sort the column as Doubles. How would I accomplish this?

EDIT:

Thank you very much Jiri Patera for your response. It was a great help in helping me to understand that the tablecellrenderer is responsible for manipulating values in these types of situations. Below is the finished excerpt that has finally accomplished what I want.

public Component getTableCellRendererComponent(JTable pTable, Object pValue, boolean pIsSelected, boolean pHasFocus, int pRow, int pColumn) {

        // Use the wrapped renderer
        Component renderedComponent = mWrappedRenderer.getTableCellRendererComponent(pTable, pValue, pIsSelected, pHasFocus, pRow, pColumn);
        Component renderedComponentHeader = pTable.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(pTable, pValue, pIsSelected, pHasFocus, pRow, pColumn);

        if (pColumn == 4 && pValue instanceof Double){
            DecimalFormat df = new DecimalFormat("$###,##0.00");
            Double d = (Double) pValue;
            String s = df.format(d);
            renderedComponent = mWrappedRenderer.getTableCellRendererComponent(pTable, s, pIsSelected, pHasFocus, pRow, pColumn);
        }

        // Set the alignment
        Integer alignment = mSpecialColumnAlignmentMap.get(pColumn);
        Integer width = mSpecialColumnWidthMap.get(pColumn);
        if (alignment != null) {
            ((JLabel) renderedComponent).setHorizontalAlignment(alignment);
            ((JLabel) renderedComponentHeader).setHorizontalAlignment(alignment);
        } else {
            ((JLabel) renderedComponent).setHorizontalAlignment(mDefaultAlignment);
            ((JLabel) renderedComponentHeader).setHorizontalAlignment(mDefaultAlignment);
        }

        if (width != null){
            pTable.getColumnModel().getColumn(pColumn).setPreferredWidth(width);
            pTable.getColumnModel().getColumn(pColumn).setMaxWidth(width);
        }

        return renderedComponent;
    }

As you can see, I already had a custom tablecellrenderer. I used the DecimalFormat to format the price as I want it.

Hope this helps someone else out in the future.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:28:35+00:00Added an answer on June 1, 2026 at 6:28 pm

    HFOE is right. However, this may be tricky for a Java newbie. Pardon me for using anonymous inner classes. See the following example to get some hints…

    
    package test;
    
    import java.awt.Component;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    
    
    public class TableTest {
    
      public static void main(String[] args) {
        TableTest tt = new TableTest();
        tt.start();
      }
    
      private void start() {
        JTable t = new JTable(new AbstractTableModel() {
          private static final long serialVersionUID = 1L;
          private List<Double> values = new ArrayList<Double>();
          {
            values.add(Double.valueOf(179.70d));
            values.add(Double.valueOf(299.40d));
            values.add(Double.valueOf(478.80d));
            values.add(Double.valueOf(89.85d));
          }
          @Override
          public String getColumnName(int column) {
            return "Double";
          }
          @Override
          public Class<?> getColumnClass(int column) {
            return Double.class;
          }
          @Override
          public int getRowCount() {
            return values.size();
          }
          @Override
          public int getColumnCount() {
            return 1;
          }
          @Override
          public Object getValueAt(int rowIndex, int columnIndex) {
            return values.get(rowIndex);
          }
        });
        t.setDefaultRenderer(Double.class, new DefaultTableCellRenderer() {
          private static final long serialVersionUID = 1L;
          @Override
          public Component getTableCellRendererComponent(JTable table,
              Object value, boolean isSelected, boolean hasFocus, int row,
              int column) {
            Double d = (Double)value;
            String s = "$" + String.valueOf(d.doubleValue());
            Component c = super.getTableCellRendererComponent(table, s, isSelected, hasFocus,
                row, column);
            return c;
          }
        });
        t.setAutoCreateRowSorter(true);
        JFrame f = new JFrame();
        f.setSize(320, 200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane sp = new JScrollPane(t);
        f.getContentPane().add(sp);
        f.setVisible(true);
      }
    
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.