Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:19:42+00:00 2026-06-06T02:19:42+00:00

I have a ScrollPane and JTable that should only have one row of data.

  • 0

I have a ScrollPane and JTable that should only have one row of data.

String[] daysOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        String[][] data = {{"None","None","None","None","None","None","None"}};

        JTable mTable = new JTable(data, daysOfWeek);

        JScrollPane scrollPane = new JScrollPane(mTable);
        mTable.setFillsViewportHeight(false);

I want the ScrollPane to fit around the borders of the table but at the moment the height of the Scrollpane takes up the entirety of the container, making it look really ugly. I don’t understand why, since I set mTable.setFillsViewportHeight(false).

Any help how to achieve a ScrollPane that doesn’t stretch beyond the size of the table would be welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:19:44+00:00Added an answer on June 6, 2026 at 2:19 am

    you can use table.setPreferredScrollableViewportSize(table.getPreferredSize());

    edit

    enter image description here

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.util.Random;
    import java.util.concurrent.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.DefaultTableModel;
    
    public class ChangeNotifiersOnEvent extends JFrame implements Runnable {
    
        private static final long serialVersionUID = 1L;
        private boolean runProcess = true;
        private Random random = new Random();
        private javax.swing.Timer timerRun;
        private Executor executor = Executors.newCachedThreadPool();
        private String[] columnNames = {"Source", "Hit", "Last", "Ur_Diff"};
        private JTable table;
        private Object[][] data = {{"Swing Timer", 2.99, 5, 1.01},
            {"Swing Worker", 7.10, 5, 1.010}, {"TableModelListener", 25.05, 5, 1.01}};
        private DefaultTableModel model = new DefaultTableModel(data, columnNames);
    
        public ChangeNotifiersOnEvent() {
            table = new JTable(model) {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public Class getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
            };
            model.addTableModelListener(new TableModelListener() {
    
                @Override
                public void tableChanged(TableModelEvent tme) {
                    if (tme.getType() == TableModelEvent.UPDATE) {
                        if (tme.getColumn() == 1 && tme.getLastRow() == 2) {
                            double dbl = ((Double) table.getModel().getValueAt(2, 1))
                                    - ((Integer) table.getModel().getValueAt(2, 2));
                            table.getModel().setValueAt(dbl, 2, 3);
                        } else if (tme.getColumn() == 1 && tme.getLastRow() == 0) {
                            prepareUpdateTableCell();
                        } else if (tme.getColumn() == 1 && tme.getLastRow() == 1) {
                            executor.execute(new MyTask(MyTask.UPDATE_TABLE_COLUMN));
                        }
                    }
                }
            });
            table.setRowHeight(30);
            table.setFont(new Font("Serif", Font.BOLD, 20));
            table.getColumnModel().getColumn(0).setPreferredWidth(180);
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane, BorderLayout.CENTER);
            new Thread(this).start();
        }
    
        private void prepareUpdateTableCell() {
            timerRun = new javax.swing.Timer(10, UpdateTableCell());
            timerRun.setRepeats(false);
            timerRun.start();
        }
    
        private Action UpdateTableCell() {
            return new AbstractAction("Update Table Cell") {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    double dbl = ((Double) table.getModel().getValueAt(0, 1))
                            - ((Integer) table.getModel().getValueAt(0, 2));
                    table.getModel().setValueAt(dbl, 0, 3);
                }
            };
        }
    
        @Override
        public void run() {
            while (runProcess) {
                try {
                    Thread.sleep(330);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                changeTableValues();
            }
        }
    
        private void changeTableValues() {
            Runnable doRun = new Runnable() {
    
                @Override
                public void run() {
                    table.getModel().setValueAt(random.nextInt(128) + random.nextDouble(), 0, 1);
                    table.getModel().setValueAt(random.nextInt(256) + random.nextDouble(), 1, 1);
                    table.getModel().setValueAt(random.nextInt(512) + random.nextDouble(), 2, 1);
    
                    table.getModel().setValueAt(random.nextInt(128), 0, 2);
                    table.getModel().setValueAt(random.nextInt(128), 1, 2);
                    table.getModel().setValueAt(random.nextInt(128), 2, 2);
                }
            };
            SwingUtilities.invokeLater(doRun);
        }
    
        private class MyTask extends SwingWorker<Void, Integer> {
    
            private static final String UPDATE_TABLE_COLUMN = "update";
            private String namePr;
            private double dbl;
    
            MyTask(String str) {
                this.namePr = str;
            }
    
            @Override
            protected Void doInBackground() throws Exception {
                dbl = ((Double) table.getModel().getValueAt(1, 1))
                        - ((Integer) table.getModel().getValueAt(1, 2));
                return null;
            }
    
            @Override
            protected void done() {
                SwingUtilities.invokeLater(new Runnable() {
    
                    @Override
                    public void run() {
                        table.getModel().setValueAt(dbl, 1, 3);
                    }
                });
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ChangeNotifiersOnEvent frame = new ChangeNotifiersOnEvent();
                    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    frame.setLocation(150, 150);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 windows one window shown the JTable Model data, when double click
I have a find function that locates a string in a JTable with quite
I'm trying to render an specific row of my jtable (it should have a
I have a JTable where I display some string data formatted using html. I
I have a JTable inside a JScrollPane. After calling my function which should populate
I have a JTable inside a JScrollPane. In one of the columns in the
I have this structure: <JFrame> <JPanel backgroundcolor = pink> <JScrollPane> <JTable>!!!Data here !!!</JTable> </JScrollPane>
I have a JTable which is held in a scrollpane, which in turn sits
I am trying to create a JTable that has a row header that looks
I have this scenery: 1 ScrollPane 1 UILoader. And i do that: myScrollPane.source =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.