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

  • SEARCH
  • Home
  • 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 7726251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:10:01+00:00 2026-06-01T05:10:01+00:00

My question like this: I have a JTable, I put a JProgressBar in one

  • 0

My question like this:
I have a JTable, I put a JProgressBar in one Column, but it fill the full cell.

I just want to set JProgressBar’s size so that it shows with margin with cell border.

My JProgressBar code:

private final JProgressBar b;
public ProgressBarRender(){
    super();
    setOpaque(true);
    b = new JProgressBar();

    b.setStringPainted(true);
    b.setMinimum(0);
    b.setMaximum(100);

    b.setBorderPainted(true);
    b.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    //b.setPreferredSize(new Dimension(1,1));
    b.setOpaque(true);
    //TODO
    //b.setForeground(Color.magenta);
}
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    Integer i = (Integer) value;
    b.setValue(i);
    return b;
}

I have called JProgressBar’s setPreferredSize method, but it does’t work.
I also called JTable’s setIntercellSpacing method, but when JTable’row selected background not show in the space,it is’t I wanted.
Can any one help me?

  • 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-01T05:10:02+00:00Added an answer on June 1, 2026 at 5:10 am

    The cell renderer will fill in the whole cell, until you implement your TableUI class.
    The reason why setPreferredSize() is not working, it is because the cell renderer not use preferered size. And preferred size should not never to explicitly set, should overwrite the getPreferredSize() method.

    To add the margin is easy, just put the progress bar into the panel, then set the border to panel. And if you want to different color when selected or deselected, you need to implement it too.

    Following is the code.

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setSize(800, 600);
    
        JTable table = new JTable(new Object[][]{{50, 50},{50, 50}} , new Object[]{"1", "2"});
        table.setDefaultRenderer(Object.class, new ProgressBarRender());
        frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    
        frame.setVisible(true);
    }
    
    public static class ProgressBarRender extends DefaultTableCellRenderer
    {
        private final JPanel panel;
        private final JProgressBar b;
    
        public ProgressBarRender()
        {
            super();
            setOpaque(true);
            b = new JProgressBar();
    
            b.setStringPainted(true);
            b.setMinimum(0);
            b.setMaximum(100);
    
            b.setBorderPainted(true);
            b.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
            //b.setPreferredSize(new Dimension(1,1));
            b.setOpaque(true);
    
            panel = new JPanel(new BorderLayout());
            panel.add(b, BorderLayout.CENTER);
            panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
    
            //TODO
            //b.setForeground(Color.magenta);
        }
    
        public Component getTableCellRendererComponent(JTable table, Object value,
                                                       boolean isSelected, boolean hasFocus, int row, int column)
        {
            Integer i = (Integer)value;
            b.setValue(i);
            if (isSelected)
            {
                b.setBackground(Color.BLUE);
                panel.setBackground(Color.DARK_GRAY);
            }
            else
            {
                b.setBackground(Color.GREEN);
                panel.setBackground(Color.LIGHT_GRAY);
            }
            return panel;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know that have alot of question like this one, but i dont know
may be too simple groovy question but....please help i have a list like this:
I have simllar question like here: static-method-invocation , but in PHP. Simply, I want
I know there are several question like this one but I feel I've spent
I have asked a question like this before, and the answer was great but
Another question like this one was asked, but I don't 'like' the answer (in
Kind of like this question I have many text snippets that I use many,
I have a route defined like this: GET /question/:q_id controllers.Questions.viewQuestion(q_id: Long) Then in my
Absolute beginner question: I have a template file index.html that looks like this: ...
I have a schema like this <h1> 5/2009 <br/> Question: This is the question

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.