Make scrollable table

15,078

Solution 1

You need to use JTable with JScrollPane

Updated reference example links:

this reference example and and this might help you

Solution 2

I think that you can accomplish this with a JTable component in swing .

Solution 3

Use JTable with JScrollPane in it for scrolling from top to down or from left to right.

Solution 4

You need a JTable in a JScrollPane.

Solution 5

You need to use JTable and JScrollPane

Sample Code:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class ProductTableExample {

    public static void main( String[] str ) {
        String[] colName = new String[] { "Product Name" ,"Price" };
        Object[][] products = new Object[][] { 
                { "Galleta" ,"$80" },
                { "Malta" ,"$40" },
                { "Nestea" ,"$120" },
                { "Tolta" ,"$140" } 
            };

        JTable table = new JTable( products, colName );

        JFrame frame = new JFrame( "Simple Table Example" );

        // create scroll pane for wrapping the table and add
        // it to the frame
        frame.add( new JScrollPane( table ) );
        frame.pack();
        frame.setVisible( true );       
    }

}
Share:
15,078
Jesus Rodriguez
Author by

Jesus Rodriguez

Content creator at devpragmatico.com Currently working in Munich, Germany

Updated on June 05, 2022

Comments

  • Jesus Rodriguez
    Jesus Rodriguez almost 2 years

    I'm making this program in Java in which I sell stuff to those connected via socket. I'm loading the products and prices via JDBC just fine, but I want to display the product with the price next to it in a scrollable table. Right now I just have this JList in which I load the names of the products:

    Please, click the link below to understand the question.

    enter image description here

    What elements should I use and how to accomplish what I'm needing?

    Thanks for reading.

  • exexzian
    exexzian over 11 years
    @Granola pleasure to help you
  • 404 User not found
    404 User not found about 7 years
    Unfortunately that link is no longer available.
  • exexzian
    exexzian about 7 years
    @Iain thanks for notifying. Updated with another example url
  • Shubham A.
    Shubham A. about 6 years
    @exexzian Usually, we should try to include absolutely necessary code in the answer itself to avoid 'dead link' issues.