Hello Everyone,
I am having a strange/odd problem with the J Scroll Pane. I have created a J Scroll Pane and added a Mouse Listener to it.
The program that I have created is a Split Pane where when the user hovers the mouse over the left side of the split pane, the left side will expand. What I want is to have the left side of the Split Pane to expand whenever the mouse is rolled over.
The real problem that I am having is that I added a mouse listener to the Scroll Pane that is embedded on the left side of the Split Pane. However, when the mouse hovers over the "SCROLL BAR", it collapses. Here is a short compilable code from my program (You can compile these three java files and run it yourself to see what the problem is [it is a bit more clearer when you run the mouse over the scroll bar while the split pane is expanded]):
I have tried all sorts of things such as adding a mouse listener to the Panel that is embedded inside the Scroll Pane. Also, I have tried adding a mouse listener to the the J Tree that is embedded inside the Panel which is embedded inside the Scroll Pane. There seems to be a Java Swing concept that I don't understand fully. If someone can let me know what the problem is, I would appreciate it! Thanks!
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;
import org.jdesktop.swingx.JXTable;
public class GUI
{
private JFrame frame;
private JSplitPane splitPane;
private JTree tree;
public GUI()
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createGUI();
}
});
}
public static void main(String[] args)
{
new GUI();
}
public void createGUI()
{
frame = new JFrame("Table");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setPreferredSize(new Dimension(1200, 600));
frame.setResizable(false);
tree = createTree();
NavigatorPanel drmnav = new NavigatorPanel(tree);
SplitPane drmgui = new SplitPane(drmnav, setupTable());
splitPane = drmgui.insertIntoSplitPane();
tree.addMouseListener(drmgui);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
frame.setContentPane(splitPane);
frame.pack();
frame.setVisible(true);
}
// Arbitrary/Random Data
public JScrollPane setupTable()
{
String[] columnNames = {"Name" , "Age" , "#1", "#2", "#3", "#4", "#5"};
Object[][] data = new Object[1000][7];
JXTable table = new JXTable(data, columnNames);
table.setFillsViewportHeight(true);
JScrollPane sp = new JScrollPane(table);
sp.setSize(1000, 600);
return sp;
}
public JTree createTree()
{
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("DRM Log");
createNodes(rootNode);
JTree jtree = new JTree(rootNode);
return jtree;
}
public void createNodes(DefaultMutableTreeNode root)
{
DefaultMutableTreeNode section = null;
DefaultMutableTreeNode lBlock = null;
DefaultMutableTreeNode qBlock = null;
for(int i=1; i < 40; i++)
{
section = new DefaultMutableTreeNode("Item #" + i);
root.add(section);
lBlock = new DefaultMutableTreeNode("Sub #1");
qBlock = new DefaultMutableTreeNode("Sub #2");
section.add(lBlock);
section.add(qBlock);
}
}
}
That is the first class, here is the second and third classes:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class NavigatorPanel extends JPanel
{
public NavigatorPanel(JComponent component)
{
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints constraint = new GridBagConstraints();
this.setLayout(gbl);
constraint.gridx = 0;
constraint.gridy = 0;
constraint.gridwidth = 1;
constraint.gridheight = 1;
constraint.weightx = 1;
constraint.weighty = 1;
constraint.ipadx = 0;
constraint.ipady = 0;
constraint.fill = GridBagConstraints.BOTH;
constraint.insets = new Insets(0,0,0,0);
this.add(component, constraint);
this.setBackground(Color.BLACK);
}
public NavigatorPanel()
{
this.setBackground(Color.BLACK);
}
}
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
public class SplitPane implements MouseListener
{
private JPanel navigatorPanel;
private JScrollPane dataScrollPane;
private JScrollPane navigatorScrollPane;
private JSplitPane splitPane;
public SplitPane(JPanel navigator, JScrollPane data)
{
navigatorPanel = navigator;
dataScrollPane = data;
}
public JSplitPane insertIntoSplitPane()
{
navigatorScrollPane = new JScrollPane(navigatorPanel);
navigatorScrollPane.addMouseListener(this);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, navigatorScrollPane, dataScrollPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(38);
splitPane.setDividerSize(7);
return splitPane;
}
@Override
public void mouseClicked(MouseEvent e)
{
}
@Override
public void mousePressed(MouseEvent e)
{
}
@Override
public void mouseReleased(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e)
{
System.out.println("Entered Scroll Pane");
splitPane.setDividerLocation(215);
}
@Override
public void mouseExited(MouseEvent e)
{
System.out.println("Exited Scroll Pane");
splitPane.setDividerLocation(38);
}
}