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 8616073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:31:56+00:00 2026-06-12T05:31:56+00:00

I have a JTable with a custom ListSelectionModel , which contains a custom selection

  • 0

I have a JTable with a custom ListSelectionModel, which contains a custom selection mode. This selection mode ensures that a selection is only made in one row, but with multiple cells. When a user clicks selects some cells in the table, the selected cells are all in the same row as the first selected cell.

Now I want to create a small message above the upper-right corner of the leading selection cell, which displays some data, i.e. the count of the selected cells. The message should be moved when the leading selection is changed.

But how do I do that? It is not a Tooltip as it is intended to be shown when user clicks into the table and selects cells and not by hovering over it.

Any suggestions?

best regards,
htz

  • 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-12T05:31:57+00:00Added an answer on June 12, 2026 at 5:31 am

    Alternatively (not very pretty but works quite well), you can take advantage of the fact that JTable is a JComponent like all others and therefore you can add child components to it. All you have to do, is make sure to size and locate properly your component (this is only because JTable uses a null-layout).

    Here is a small demo with a JLabel that displays the number of selected items. The label is automatically positioned on the first visible row, unless it is the current lead selection, in which case, the label is moved to the second visible row:

    import java.awt.BorderLayout;
    import java.awt.Point;
    import java.util.Vector;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    public class TestTable {
    
        protected void initUI() {
            Vector<Vector<Object>> data = new Vector<Vector<Object>>();
            Vector<String> colNames = new Vector<String>();
            for (int i = 0; i < 5; i++) {
                colNames.add("Col-" + (i + 1));
            }
            for (int i = 0; i < 200; i++) {
                Vector<Object> row = new Vector<Object>();
                for (int j = 0; j < 5; j++) {
                    row.add("Cell " + (i + 1) + "-" + (j + 1));
                }
                data.add(row);
            }
            table = new JTable(data, colNames);
            someText = new JLabel();
            someText.setOpaque(true);
            table.add(someText);
            table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    int count = table.getSelectedRowCount();
                    someText.setText("You currently have selected " + count + " item" + (count > 1 ? "s" : ""));
                    layoutLabel();
                }
            });
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            scrollpane = new JScrollPane(table);
            scrollpane.getViewport().addChangeListener(new ChangeListener() {
    
                @Override
                public void stateChanged(ChangeEvent e) {
                    layoutLabel();
                }
            });
            frame.add(scrollpane, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
        }
    
        private JLabel someText;
        private JTable table;
        private JScrollPane scrollpane;
    
        private void layoutLabel() {
            someText.setSize(someText.getPreferredSize());
            Point location = scrollpane.getViewport().getViewRect().getLocation();
            int leadSelectionIndex = table.getSelectionModel().getLeadSelectionIndex();
            if (leadSelectionIndex > -1) {
                if (table.rowAtPoint(location) == leadSelectionIndex) {
                    location.y += table.getRowHeight(leadSelectionIndex);
                }
            }
            someText.setLocation(location);
        }
    
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
                UnsupportedLookAndFeelException {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TestTable().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable for which I have provided a custom TableCellRenderer that colors
I have a JTable . One column holds a JPanel which contains some JLabels
I have a Jtable on which I called the method table1.setAutoCreateRowSorter(true); . So this
I have a JTable in Java that has a custom dataMOdel and custom renderer.
I have a JTable which uses a custom TableModel to display a series of
I have a JTable using a custom DefaultTableModel which has some Booleans in the
I need to have a JToggleButton (that has custom background) that contains a JPanel
I have a JTable with multiple columns that display doubles, some of which are
I have a JTable that holds several JPanels using a custom renderer/editor. The JPanel
Alright. I have implemented a custom JTable model that includes the whole @Override public

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.