0

I want to have a horizontal scroll bar to view whats there in "Description" column.

When the data in 2nd column exceeds I want to have a horizontal scrollbar to appear. This is my code: =========Working Example===============

import java.awt.*;

import javax.swing.*;
import javax.swing.plaf.basic.BasicBorders.MarginBorder;
import javax.swing.plaf.metal.MetalBorders.TableHeaderBorder;
import javax.swing.table.*;
import javax.swing.border.*;

public class TableRowRenderingTip extends JPanel
{
    static Object[] columnNames = {"Allowed Commands", "Description"};
    private final static Dimension scrollPaneDimenssion = new Dimension(70, 200);
    public TableRowRenderingTip()
    {       
        Object[][] data =
        {
            {"One",  "Allow proxies to act as tunnels for Allow proxies to act as tunnels for Allow proxies to act as tunnels for"},
            {"Two",  "Allow proxies to act as tunnels for Allow proxies to act as tunnels for Allow proxies to act as tunnels for"},
            {"Three","Allow proxies to act as tunnels for Allow proxies to act as tunnels for Allow proxies to act as tunnels for" },
            {"Four", "Allow proxies to act as tunnels for Allow proxies to act as tunnels for Allow proxies to act as tunnels for"},

        };      
        DefaultTableModel model = new DefaultTableModel(data, columnNames);     
        add( createData(model) );
    }



    private static JComponent createData(final DefaultTableModel model) 
    {       
        JTable inLineTable = new JTable( model );

        inLineTable.setShowGrid(false);
        inLineTable.setShowVerticalLines(false);

        inLineTable.getColumn(columnNames[0]).setMaxWidth(130);
        inLineTable.getColumn(columnNames[0]).setMinWidth(130);
        inLineTable.getColumn(columnNames[0]).setResizable(false);
        inLineTable.getColumn(columnNames[1]).setMinWidth(270);
        inLineTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

        JScrollPane scrollPane = new JScrollPane(inLineTable);
        scrollPane.setMinimumSize(scrollPaneDimenssion);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        return scrollPane;

    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI()
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.add( new TableRowRenderingTip() );
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

================================================== But scrollPane size is fixed: I want to see the data in the 2nd column using a HORIZONTAL SCROLL BAR Image of the current state of the table

Kumar
  • 1
  • 1
  • 1
    despite the fact that big bold font is used in question isn't possible to say something wise without an SSCCE/MCVE, short, runnable, compilable, with hardcoded value for JTable in local variable – mKorbel Nov 27 '14 at 11:06
  • There i added a working example Try to find how can i get the horizontal scroll bar – Kumar Nov 27 '14 at 11:28
  • you have to play with to override get/setPreferredScrollableViewportSize for JScrollPane, but I'm boubt that code posted generating something similair to image posted here – mKorbel Nov 27 '14 at 11:32
  • the code is not even showing vertical scrollbar – Abhishek Choudhary Nov 27 '14 at 11:35
  • To generate the one in image i need a framework. So instead i replicated in a small program. Anyway i got my answer from http://stackoverflow.com/questions/2452694/jtable-with-horizontal-scrollbar Thanks for you help :) – Kumar Nov 27 '14 at 11:36
  • get/setPreferredScrollableViewportSize == JTables view can returns different Dimension than is available from JVierport – mKorbel Nov 27 '14 at 11:37
  • Have you tried `inLineTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);`? From the [documentation](https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html): “To allow for a horizontal scrollbar, invoke setAutoResizeMode(int) with AUTO_RESIZE_OFF.” – VGR Nov 27 '14 at 20:43
  • I did and that worked. Thanks VGR – Kumar Dec 05 '14 at 08:12

0 Answers0