We help IT Professionals succeed at work.
Get Started

How to align a textfield in a jpanel to jtable columns

neverton
neverton asked
on
1,017 Views
Last Modified: 2013-11-23
Hi,
  I'm creating an application and I'm having issues getting a jpanel with textfields to align to columns in a jtable.

  The application is basically a jtable with 9 columns, from left to right they are;
Description,MonthSales,SameMonthLastYear,Difference,Percentage,YearSales,LastYearSales,Difference,Percentage.

 Beneath the jtable i have created a jpanel of the same width as the table. and in this I have placed 9 textfields, to act as total boxes for the respective columns (the textfield below the Description column just says Totals). I can get the textfields to display the correct values, the only thing I cannot do is get the textfields to line up directly below the corresponding jtable column.

 I have a function which gets the width of the Description column, and sets the first jtextfield to the same width, the problem I have got is that the width of the jtextfield doesnt match the width of the jtable column, its out by almost half.

  I've checked the font and font size, they're all the same, so I'm baffled why my textfield.setWidth function isnt setting the textfield to the same width as the jtable column.

  The application was created using Netbeans, and I've just created a panel and dragged in the textfields.

 I've tried Free Layout and Box Layout, so I've not sure if its a layout issue, netbeans and me not understanding the width on a jtable is different to a width on a textfield
Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE