Question

I have this code :

    JPanel textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(0,1));
    //JPanel text = new JPanel();
    //text.setBorder(new TitledBorder(new EtchedBorder(), "Disploay Area"));
    MyModel model = new MyModel();
    //sorter = new TableRowSorter<MyModel>(model);
    final JTable table = new JTable(model);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    table.setAutoCreateRowSorter(true);
    //table.setRowSorter(sorter);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent event) {
                    int viewRow = table.getSelectedRow();
                    if (viewRow < 0) {
                        //Selection got filtered away.
                        txtstatus.setText("");
                    } else {
                        int modelRow = 
                            table.convertRowIndexToModel(viewRow);
                        txtstatus.setText(
                            String.format("Selected Row in view: %d. " +
                                "Selected Row in model: %d.", 
                                viewRow, modelRow));
                    }
                }
            }
    );
    JScrollPane scroll = new JScrollPane(table);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    textPanel.add(scroll, BorderLayout.CENTER);
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 0;
    c.ipadx = 800;
    c.gridy = 1;
    c.ipady = 460;
    c.anchor = GridBagConstraints.CENTER;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.insets = new Insets(0,0,0,0);
    con.add(textPanel, c);

I am really confused with what I have done, because the program works that I have devised however the columns if I want to adjust one row so I can see all the information squashes the rest of them. Is there anyway if I want to resize the columns that it won't affect the rest of them?

The GUI I have made connects to a database and retrieves the information and displays within the JTable. Any help would be much appreciated.

Was it helpful?

Solution

You can use:

table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

now a horizontal scrollbar will appear as required.

OTHER TIPS

Take a look at JTable#setAutoResizeMode.

It defines how subsequent columns should be resized when you change the size of a column.

By default, JTable uses AUTO_RESIZE_SUBSEQUENT_COLUMNS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top