2

enter image description here

I'm trying To insert a JTable into a JScrollPane and I see a small gap between the borders of the table and the scroll Pane.And on the left side the table looks to be aligned to extreme left.Can someone please help me fix this.?

Removing setAutoResizeMode(JTable.AUTO_RESIZE_OFF) is fixing that.But I Need to turn that off.

this.dataTable = new SortableTable(this);
    this.dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    this.dataTable.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    scrollPane = new JScrollPane(dataTable,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setPreferredSize(new Dimension(900,250));
    scrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
Saikiran
  • 67
  • 1
  • 8
  • Maybe your prefered size of 900 is smaller than the width of the container* ? – 0ddlyoko Apr 06 '18 at 10:20
  • okay.When setting the preferredsize to new Dimension(895, 250)..I see no gap.But I would like to know if there is any way to make the table to fit to the Scrollpane with any size. If I'm setting the table's preferred size as scrollpane preferred size, I see a scrollbar which is not needed. – Saikiran Apr 06 '18 at 10:25
  • Remove the **setAutoResizeMode** line and Java will resize it – 0ddlyoko Apr 06 '18 at 10:26
  • The problem is the dialog should not be allowed to resize.If I remove AutoResizeMode on JTable and If the user want to extend any of the columns as the columns can have too much data , They don't want other columns to change their size(shrink) , instead a horizontal scrollbar should be given.So I've turned it off. – Saikiran Apr 06 '18 at 10:31
  • 1
    1) `scrollPane.setPreferredSize(new Dimension(900,250));` That's probably at the core of the problem. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Apr 06 '18 at 10:39

1 Answers1

0

You simply need to set the resize mode in a listener, when the component is resized. Here is an example:

import java.awt.Window;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.TableColumn;

public class TableTest implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TableTest());
    }

    @Override
    public void run() {
        JTable table = new JTable(10, 3);
        final JScrollPane scroller = new JScrollPane(table);
        // update the resize mode when scroller is resized and window is shown
        scroller.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                Window win = SwingUtilities.windowForComponent(scroller);
                if (win != null && win.isVisible()) {
                    updateColumns(table);
                    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                }
            }
        });
        JFrame frm = new JFrame("Table");
        frm.add(scroller);
        frm.setSize(600, 400);
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    // Transfer column widths from autoresize mode
    private void updateColumns(JTable table) {
        for (int i = 0; i < table.getColumnCount(); i++) {
            TableColumn col = table.getColumnModel().getColumn(i);
            col.setPreferredWidth(col.getWidth());
        }
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48