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 9157451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:01:11+00:00 2026-06-17T13:01:11+00:00

Possible Duplicate: Swing – Setting the color of a cell based on the value

  • 0

Possible Duplicate:
Swing – Setting the color of a cell based on the value of a cell

I have a Spreadsheet class, containing a JTable and its TableModel.
And my main window contains this spreadsheet and a list of buttons, with for example a bold one.

I can successfully get the selected cell (see code below), but I have no idea of how to change its content and font, color etc.

public void actionPerformed(ActionEvent e)
{
    int rowToUpdate = -1, columnToUpdate = -1;
    for(int i = 0 ; i < tableToUpdate.getRowCount() ; i++)
        for (int j = 0 ; j < tableToUpdate.getColumnCount() ; j++)
            if(tableToUpdate.isCellSelected(i, j)){ rowToUpdate = i; columnToUpdate = j; }

    if(rowToUpdate >= 0 && columnToUpdate >= 0)
    {
        if(e.getSource == boldButton)
        {
             // Here, how to change the bold of the cell(rowToUpdate,columnToUpdate)
        }
    }
}
  • 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-17T13:01:12+00:00Added an answer on June 17, 2026 at 1:01 pm

    Couple things: first, the code you wrote could be a lot simpler. JTable comes with getSelectedRow() and getSelectedColumn() methods out of the box, so no need to write the for loops yourself.

    That being said, if you are just trying to change the way that the selected cell is rendered, you probably don’t actually want to do any of this anyway. The way to change the how a cell is rendered is to use a TableCellRenderer. When JTables need to render a cell, they pass all the information about that cell (its value, whether or not it is selected, etc.) along to a TableCellRenderer. There is a DefaultTableCellRenderer installed by default, which renders your cells as JLabels. You can set your own renderer using setDefaultRenderer(). In your case, it should be very easy to extend DefaultTableCellRenderer, override getTableCellRendererComponent() to call super(), and then once super() returns, set the font to bold if the cell is selected.

    The javadoc for JTable has a link to the JTable tutorial which has a special section on using custom renderers. That tutorial (along with a bunch of other great Swing tutorials) can be found at http://docs.oracle.com/javase/tutorial/uiswing/components/table.html.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Adding multiple JProgressBar to TableColumn of JTable i have a jTable with
Possible Duplicate: How to make a JFrame Modal in Swing java I have 2
Possible Duplicate: How to make a columns in JTable Invisible for Swing Java How
Possible Duplicate: How can I detect if caps lock is toggled in Swing? How
Possible Duplicate: Using Swing with a framebuffer I've been searching all day for solutions
Possible Duplicate: Java AWT/Swing: Get notified on change of position/size of my window Is
Possible Duplicate: Java Full Screen Program (Swing) -Tab/ALT F4 I've got a full screen
Possible Duplicate: Getting a clue at / debug a huge Java Swing App I’ve
Possible Duplicate: How do I make a list with checkboxes in Java Swing? I
Possible Duplicate: Alignment issue in GridBagLayout Please have a look at the following code

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.