Results 1 to 9 of 9
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default JScrollBar Listener

    Hello

    I would like to ask if it's able to detect button clicks on the JScrollBar (horizontal/vertical)

    JScrollBar Listener-scrollbar.png

    not on the entire JScrollBar but only when the user click the button.

    Thanks in advance

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: JScrollBar Listener

    Does this work for you? java - MouseListener for JScrollBar arrow buttons - Stack Overflow

    Edit: since the given example is changing the UI classes which I don't recommend unless you really know what you're doing, can I ask what you want to achieve? Something an AdjustmentListener could solve?
    Last edited by SurfMan; 10-27-2015 at 04:17 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: JScrollBar Listener

    Thanks for the reply.

    I am working on a project to create a small version of the Microsoft excel in java from the scratch.
    Right now i am trying to implement dynamic spread sheet table so when the user has reach the end of the JTable
    and click the arrow either from the horizontal or the vertical JScrollBar to append rows or columns on my table.
    I have done then vertical by MouseWheelListener so when the user try to scroll the vertical bar my table add rows
    and the effect is really nice. Now i want to do the same thing but by clicking the arrows. I want to do that for two reasons
    first one cause i want to give the user the ability to be able to scroll by hand the bar (by clicking it to the end) and not
    increase the size of the table and second the horizontal bar is little tricky.. If i try to do the same by adding a listener to the
    JScrollBar itself when someone try to reach the end of the bar my tables add multiple columns at ones(line 5 or more ) so i want the user
    to be able to add only on or two by just clicking the arrow one or two times (of course when the bar is in it's maximum value)

    My program looks like this :

    JScrollBar Listener-myexcel.png

    I hope my English is good enough to make you understand of what i am trying to achieve here


    Edit:

    I just want to detect if the bar(horizontal or vertical) is in it's maximum value and the user try to go further by clicking the arrow so then i can add more columns or rows
    for example i call this method to increase the rows in my implementation of mouseWheelListener :

    Java Code:
    private void checkForNewRows(JScrollPane scrollPane) {
    	JScrollBar y_bar = scrollPane.getVerticalScrollBar();
    	int position = y_bar.getValue() + y_bar.getHeight();
    	if (position >= y_bar.getMaximum() - 20) {
    		// create new row inside currentSheet table
    		SpreadSheetModel model = (SpreadSheetModel) currentSheet.getModel();
    		model.addEmptyRow();
    
    		// Add new row in rowTable too
    		DefaultTableModel model2 = (DefaultTableModel) rowTable.getModel();
    		model2.addRow(new String[] {});
    	}
    }
    Last edited by ShadowWalker; 10-27-2015 at 04:44 PM.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: JScrollBar Listener

    You can add an AdjustmentListener that checks for getValueIsAdjusting():

    Java Code:
            scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
                @Override
                public void adjustmentValueChanged(AdjustmentEvent e) {
                    if (e.getValueIsAdjusting()) {
                        //user is still scrolling 
                    }
                    else {
                        //user is done scrolling
                    }
                }
            });
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: JScrollBar Listener

    Good idea but I can't see where this can be helpful. Cause the Vertical is not really the problem cause let's say i will do it by mouseWheelListener which is good enough. The main problem is the Horizontal bar in which if i try to do it with Adjustment Listener then i will only add columns one at a time and only if the user stop scrolling but i want the table to add rows sequentially without waiting the user to stop scrolling. Imagine that the user for example want to add 10 more columns then with Adjustment Listener would have to scroll up to the end and then stop (just to add one) and to do this thing 9 more times but by clicking the button he could just click the button until 10 columns have been created or he could just do multiple clicks. I can't really explain it better cause the problem is more visual and i can't just post my entire project here. Maybe i will make a small example later when i have time and to give you so you can see the problem. Until then i will check the UI implementation ;)



    EDIT:

    if inside the adjustment Listener in:
    [CODE]if (e.getValueIsAdjusting()) {
    //user is still scrolling
    }[CODE]

    I put the code there inside this if then because the maximum value of the scrollBar is not changed even if i add rows when the user still scrolling then by scrolling to the end
    and try to add one single row the result is to add multiple rows in one like 10-20 rows just by one scrolling
    and that's the same problem
    Last edited by ShadowWalker; 10-27-2015 at 05:08 PM.

  6. #6
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: JScrollBar Listener

    I made a really fast SSCCE so i can demonstrate the problem

    the main class :

    Java Code:
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestTable {
    	public static void main(String args[]) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				try {
    					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    				} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
    						| UnsupportedLookAndFeelException e) {
    					e.printStackTrace();
    				}
    				
    				JFrame testFrame = new JFrame();
    				testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				testFrame.setSize(new Dimension(800,600));
    				testFrame.setLocationRelativeTo(null);
    				testFrame.add(new CustomTablePanel());
    				testFrame.setVisible(true);
    			}
    			
    		});
    	}
    }
    And the customTable class :

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    
    public class CustomTablePanel extends JPanel {
    
    	public CustomTablePanel() {
    		setLayout(new BorderLayout());
    
    		DefaultTableModel model = new DefaultTableModel(50, 26){
    
    			@Override
    			public void addColumn(Object arg0) {
    				String columnName = super.getColumnName(getColumnCount());
    				super.addColumn(columnName);
    			}
    			
    		};
    		JTable mainTable = new JTable(model);
    
    		// Enable single cell selection
    		mainTable.setCellSelectionEnabled(true);
    		
    		// Disable AutoResize function of the JTable
    		mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    
    		// Set each row height to 21 for better look
    		mainTable.setRowHeight(21);
    
    		JScrollPane scrollPane = new JScrollPane(mainTable);
    		
    		scrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener(){
    
    			@Override
    			public void adjustmentValueChanged(AdjustmentEvent arg0) {
    				JScrollBar bar = scrollPane.getHorizontalScrollBar();
    				int pos = bar.getValue();
    				int width = bar.getWidth();
    				if(bar.getMaximum() == pos + width){
    					int numOfColumns = model.getColumnCount();
    					String columnName = model.getColumnName(numOfColumns);
    					model.addColumn(columnName);
    				}
    			}
    			
    		});
    		add(scrollPane);
    	}
    }
    in this example i focus on horizontal JScrollBar which is the main problem. To understand what is going on try to scroll the horizontal bar to it's maximum value and see how many columns are gonna added to the main table. You will see that the effect is not very good. So i am trying to achieve the same thing by clicking the arrows of the JScrollBar instead. I tried to change the UI and it's works fine but the then the appearance of the bar is very bad so i have to make a custom UI from the scratch and i would like to avoid that. If there is not any other way to do it then i guess i will have to find another way to add columns dynamically

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: JScrollBar Listener

    but the then the appearance of the bar is very bad
    Yep. Happened to me. But you need to change the look and feel of the scroll bar.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: JScrollBar Listener

    If you add the getValueIsAdjusting() like I suggested previously, it sure looks a lot better:
    Java Code:
    @Override
                public void adjustmentValueChanged(AdjustmentEvent arg0) {
                    if (!arg0.getValueIsAdjusting()) {
                        JScrollBar bar = scrollPane.getHorizontalScrollBar();
                        int pos = bar.getValue();
                        int width = bar.getWidth();
                        if (bar.getMaximum() == pos + width) {
                            int numOfColumns = model.getColumnCount();
                            String columnName = model.getColumnName(numOfColumns);
                            model.addColumn(columnName);
                        }
    
                    }
                }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: JScrollBar Listener

    SurfMan , yes i have to admit it's looks better. Jim thanks for the answer too. So as i understand the only way to improve this is by implement a custom Feel and look for my JScrollBar hmm in that case i am going to find another way to add columns. Thanks for your time both of you.

Similar Threads

  1. JscrollBar not working
    By GeorgyBoy in forum AWT / Swing
    Replies: 2
    Last Post: 12-14-2012, 02:44 PM
  2. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  3. JScrollbar not working
    By Tastosis in forum New To Java
    Replies: 7
    Last Post: 03-22-2011, 06:22 AM
  4. Problem with JScrollBar
    By Arthur in forum AWT / Swing
    Replies: 3
    Last Post: 02-19-2010, 02:59 AM
  5. JScrollBar
    By solomon_13000 in forum AWT / Swing
    Replies: 1
    Last Post: 07-01-2009, 07:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •