1

JTable with 10 columns is there. Need to arrange the dynamically created 10 JCheckboxes and arrange it in a row above the JTable. And align each JCheckboxes left position to the jtable first row each cell x coordinate.

Java Swing GUI appearance : JInternalFrame-> with two JScrollpanes (Top & Bottom)

Top Area : JScrollpane->JPanel->JCheckbox's (10 check boxes)

Bottom Area : JScrollpane->JTable (10 columns)

Here I want to arrange each check boxes x coordinate with the tables each cell x coordinate.

I tried the below code and it won't work. (worked like, the 10 check boxes covered with in the table's 5 column widths)

The column count of the table and the creation of check boxes are dynamic and it will change when ever needed.

    panel.setPreferredSize(new Dimension(scrollpane.getPreferredSize().width, scrollpane.getPreferredSize().height));

    int intColCount=table.getColumnCount();
    int intX=0;

    JCheckBox checkboxTop[] = new JCheckBox[intColCount];
    JCheckBox checkboxBottom[] = new JCheckBox[intColCount];
    for(int i=0;i<intColCount;i++){
        intX = (int)table.getCellRect(0, i, true).getX();

        checkboxTop[i]= new JCheckBox("Top "+(i+1));
        panel.add(checkboxTop[i]);
        checkboxTop[i].setBounds(intX, 10, 50, 10);

        checkboxBottom[i]= new JCheckBox("Bottom "+(i+1));
        panel.add(checkboxBottom[i]);
        checkboxBottom[i].setBounds(intX, 30, 50, 10);            
    }
    panel.setPreferredSize(new Dimension(intColCount*(int_each_cell_width), panel.getPreferredSize().height));

Can any one share some idea...

Jeet
  • 1,006
  • 1
  • 14
  • 25
  • And when the column size changes? Do you want to rearrange the checkboxes? You're probably going to need to write your own layout manage which is connected to the ColumnModel of the table... – MadProgrammer Feb 03 '15 at 08:50
  • yes the column count of the table and the creation of check boxes are dynamic and it will change when ever needed – Jeet Feb 03 '15 at 08:55
  • You'll likely need to attach a listener to the table (and monitor for the model property change) and a TableModelListener on the TableModel to monitor for changes to the model – MadProgrammer Feb 03 '15 at 09:01
  • Let me add the image for your reference after reaching 10 reputations. – Jeet Feb 03 '15 at 09:12
  • from code posted isn't posible to see some direction, because JTable isn't designated to nest JComponents in the model, just value for TableCellRenderer – mKorbel Feb 03 '15 at 09:12
  • assignment is 1. wrong designed, 2. lack of knowledge, /// 3. anyway for better help sooenr post an SSCCE/MCVE, short, runnable, compilable with hardcoded value for JTable/XxxTableModel in local variable – mKorbel Feb 03 '15 at 09:15
  • You implement this using the `JTableHeader` – MadProgrammer Feb 03 '15 at 09:41
  • 1
    Here's some pros & cons, for [example](http://stackoverflow.com/q/7137786/230513). – trashgod Feb 03 '15 at 11:08
  • I want the check box in the first two row of the jtable. And next row should have some other text values. so i implemented in separate panel – Jeet Feb 03 '15 at 11:12
  • 1
    @Jeet (I want the check box in the first two row of the jtable) - TableCellRenderer has row and column coordinates as parameters, use those indexes – mKorbel Feb 03 '15 at 11:34
  • yes i tried the tablecellrenderer. But the check boxes were static and not editable. And after checking the internet, it seems it is possible to have a control (component) in column wise only with editable. – Jeet Feb 03 '15 at 11:45

1 Answers1

0

Thanks to all who shared the logic.

It worked fine after the below code changes.

//scrltbl is the scrollpane of the jtable

scrollpane.setPreferredSize(scrltbl.getPreferredSize())
int intColCount = table.getColumnCount();
panel.setLayout(new GridLayout(2, intColCount));
int intColCount=table.getColumnCount();
int intX=0;

JCheckBox checkboxTop[] = new JCheckBox[intColCount];
JCheckBox checkboxBottom[] = new JCheckBox[intColCount];
for(int i=0;i<intColCount;i++){
    checkboxTop[i]= new JCheckBox("Top "+(i+1));
    panel.add(checkboxTop[i]);
 }
 for(int i=0;i<intColCount;i++){
    checkboxBottom[i]= new JCheckBox("Bottom "+(i+1));
    panel.add(checkboxBottom[i]);
 }
panel.updateUI();
Jeet
  • 1,006
  • 1
  • 14
  • 25