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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:09:24+00:00 2026-06-09T04:09:24+00:00

I’m using MS Access database as backend. I got a problem. After retrieval and

  • 0

I’m using MS Access database as backend. I got a problem. After retrieval and shown in the applet, The contents are automatically center-aligned, which looks awkward. I’m confused which part of my code made this happen. So, kindly help me diagnose the problem.

public void addTable8(String query8)
{
   SimpleDateFormat formatter8 = new SimpleDateFormat("dd/MM/yyyy");
   try
   {
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection conn8=DriverManager.getConnection("jdbc:odbc:vasantham","","");
     Statement st8=conn8.createStatement();
     ResultSet rs8=st8.executeQuery(query8);
     ResultSetMetaData md8=rs8.getMetaData();
     int cols8=md8.getColumnCount();
     model8=new DefaultTableModel()
     {
       public Class getColumnClass(int col)
       {
          Object o = getValueAt(0, col);
          if(o == null)
             return Object.class;
          else
             return o.getClass();
       }
     };

model8.addColumn("Purpose");
model8.addColumn("Name");
model8.addColumn("Composition");
model8.addColumn("Expiry");
model8.addColumn("Stock");
model8.addColumn("Cost");
model8.addColumn("Cost/Tab");
model8.addColumn("Type");
model8.addColumn("Supplier");
model8.addColumn("Supplier Number");
model8.addColumn("Rack");

table8=new JTable(model8);
table8.setOpaque(true);
table8.setFillsViewportHeight(true);
table8.setBackground(new Color(255,255,208));
table8.getTableHeader().setBackground(new Color(255,228,181));

String[] tabledata8=new String[cols8];
int i=0;

while(rs8.next())
{
 for(i=0;i<cols8;i++)
 {
  if(i==3)
  {   
   Date intr8=(rs8.getDate(i+1));
   tabledata8[i]=formatter8.format(intr8);
  }
  else
   tabledata8[i]=rs8.getObject(i+1).toString();

 }
 model8.addRow(tabledata8);
}


panel8.removeAll();

table8.setDefaultRenderer(String.class, new CustomRenderer());
table8.setRowSelectionAllowed(true);
table8.setColumnSelectionAllowed(true);
table8.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

JScrollPane scroll8 = new JScrollPane(table8); 
panel8.setLayout(new BorderLayout());

panel8.add(scroll8,BorderLayout.CENTER);
frame8.add(panel8,BorderLayout.CENTER);
conn8.close();


 }
  catch(Exception e8)
  {
  e8.printStackTrace();
  }
}

 class CustomRenderer implements TableCellRenderer
 {
public CustomRenderer()
{
  label = new JLabel();
  label.setHorizontalAlignment(JLabel.CENTER);
  label.setOpaque(true);
  targetRow = -1;
  targetCol = -1;
}

public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
  if(isSelected)
  {
    label.setBackground(table.getSelectionBackground());
    label.setForeground(table.getSelectionForeground());
  }
  else
  {
    label.setBackground(table.getBackground());
    label.setForeground(table.getForeground());
  }
  if(row == targetRow && column == targetCol)
  {
    label.setBackground(new Color(176,196,222));

    label.setFont(table.getFont().deriveFont(Font.BOLD));
  }
  else
  {
    label.setBorder(null);
    label.setFont(table.getFont());
  } 
  label.setText((String)value);
  return label;
}

public void setTargetCell(int row, int col)
{
  targetRow = row;
  targetCol = col;
}
} 
  • 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-09T04:09:26+00:00Added an answer on June 9, 2026 at 4:09 am
    • Your code line label.setHorizontalAlignment(JLabel.CENTER); from class CustomRenderer implements TableCellRenderer is centering the column’s contents to the CENTER

    • Read Renderer and Editor concepts in the Oracle tutorial

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

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build

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.