Java Usage of MouseListener and MouseMotionListnener #1
Part of the code is stolen from cited sources.
In general, what is shown in blue is stuff generated by the IDE (not completely, however), and what is shown in black is my typing (I hope you know what I mean).
I will annotate the text in color comments which should be self evident
as to whether they are code or comments.
It takes some fooling around to get the split pane to work, since the
properties indicated are not what one expects, but with some trial and error
one can change the alignment of the two split panels, and move the separator
bar.
It is wierd that the IRE puts "buttons" in the SplitPane to start with, and
you have to ADD your own components to the panes to make it what you expect.
/*
* Combo1.java
*
* Created on August 25, 2003, 2:46 PM
*/
/**
*
* @author Administrator
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class Combo1 extends JFrame implements WindowListener{
/** Creates new form Combo1 */
public Combo1() {
initComponents();
What follows is older code which initializes a tree
DefaultMutableTreeNode root=new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child, grandchild;
for (int childIndex=1;childIndex<4;childIndex++){
child=new DefaultMutableTreeNode("Child "+childIndex);
root.add(child);
for (int grandChildIndex=1;grandChildIndex<4;grandChildIndex++){
grandchild = new DefaultMutableTreeNode("GrandChild "+childIndex+"."+grandChildIndex);
child.add(grandchild);
}
}
DefaultTreeModel treeModel = new DefaultTreeModel(root);
jTree1.setModel(treeModel);
What follows is code which is requied to get a scrollable drawing panel up
MyComponent figure = new MyComponent();
jScrollPane2.add(figure);
//the following are all required to get scrolling to take place:
figure.setPreferredSize(new Dimension(600,600));
jScrollPane2.setViewportView(figure);
//end of scrolling required matters.
int frameWidth = 400;
int frameHeight = 400;
this.setSize(frameWidth, frameHeight);
this.setVisible(true);
Note that the tree is not inserted into ScrollPane1, since that is done in the IDE.
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
jSplitPane1 = new javax.swing.JSplitPane();
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jScrollPane2 = new javax.swing.JScrollPane();
addWindowListener(this);
jSplitPane1.setDividerLocation(150);
jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
Inside the IDE, the JTree is embedded inside the ScrollPane
jScrollPane1.setViewportView(jTree1);
Inside the IDE, the JScrollPane is embedded inside the SplitPane
jSplitPane1.setLeftComponent(jScrollPane1);
Inside the IDE, the other JScrollPane is also embedded inside the SplitPane
on the other side
jSplitPane1.setRightComponent(jScrollPane2);
getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
pack();
}
// Code for dispatching events from components to event handlers.
public void windowActivated(java.awt.event.WindowEvent evt) {
}
public void windowClosed(java.awt.event.WindowEvent evt) {
}
public void windowClosing(java.awt.event.WindowEvent evt) {
if (evt.getSource() == Combo1.this) {
Combo1.this.exitForm(evt);
}
}
public void windowDeactivated(java.awt.event.WindowEvent evt) {
}
public void windowDeiconified(java.awt.event.WindowEvent evt) {
}
public void windowIconified(java.awt.event.WindowEvent evt) {
}
public void windowOpened(java.awt.event.WindowEvent evt) {
}//GEN-END:initComponents
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new Combo1().show();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JTree jTree1;
// End of variables declaration//GEN-END:variables
class MyComponent extends JComponent{
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
int x=0;
int y=0;
int width = getSize().width - 1;
int height = getSize().height - 1;
g2d.drawLine(0,0, width,height);
g2d.drawOval(x,y, width,height);
}
}
}
Return to the main book TOC.