
Signature
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Hello:
I have a parcial solution for this but without using GlassPane.
In my application I ADD panels in my left side of my Split Pane, I can
add a rectangle component over the selected panel and drag that
rectangle... the problem is when I am releasing the mouse... the panel
did get the new position but I can't make the other panels to reorder
themselves, the panel I moved is over another one... how can I put the
panel in its new place an realign the others??
My application looks like this:
+---------+------------+
| |JButton(add)|
| | |
|---------. |
|JComponent |
(Rectangle) |
|---------. |
| | |
| | |
.---------. |+
|JPanel | |
.---------.-------------
I have 3 classes and 1 main class:
- objLegend
- objPanel
- objRectangle
- objMainWindow
/*********this is the code for each class*******////
public class objLegend extends JPanel
{
public Container container = new Container();
public objMainWindow mainWindow = null;
public int intTotalPanels = 0;
public int intLastIndexPanel = 0;
private Rectangle rec = null;
public objLegend()
{
super(new GridLayout(0,1));
setFocusable(true);
setAutoscrolls(true);
container.setLayout(new BoxLayout(container,
BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(container);
add(scrollPane);
show();
}
}
public class objRectangle extends JComponent
{
objPanel panel = null;
objMainWindow mainWindow;
Rectangle rec = null;
public objRectangle()
{
setBorder(BorderFactory.createLineBorder(Color.blue));
setVisible(true);
}
}
public class objPanel extends JPanel implements MouseListener,
MouseMotionListener
{
JCheckBox objCheck;
JLabel objLabel;
public objMainWindow mainWindow = null;
public int intOrder = 0;
private static Rectangle rec = null;
objPanel objP;
public objPanel()
{
super(new BorderLayout());
this.addMouseListener(this);
this.addMouseMotionListener(this);
objCheck = new JCheckBox();
objCheck.setSelected(true);
objLabel = new JLabel();
objLabel.setText("Layer");
this.add(objCheck, BorderLayout.LINE_START);
this.add(objLabel, BorderLayout.CENTER);
this.add(objLabel, BorderLayout.SOUTH); //solo para hacer
bulto
setBorder(BorderFactory.createLineBorder(java.awt.Color.black));
this.show(true);
}
public void setOrder()
{
mainWindow.legend.intTotalPanels =
mainWindow.legend.intTotalPanels + 1;
this.intOrder = mainWindow.legend.intTotalPanels;
}
public void mousePressed(MouseEvent e)
{
//mainWindow.rectangle.hide();
rec = e.getComponent().getBounds();
System.out.println("Pressed on: "+rec+" ");
mainWindow.rectangle.setBounds(rec.getBounds().x,
rec.getBounds().y, rec.getBounds().width, rec.getBounds().height);
//mainWindow.rectangle.show();
}
public void mouseDragged(MouseEvent e)
{
rec = e.getComponent().getBounds();
mainWindow.rectangle.setBounds(mainWindow.rectangle.getBounds().x,
e.getY()+ (this.intOrder-1)*this.getHeight()+this.getHeight()/2,
mainWindow.rectangle.getBounds().width,
mainWindow.rectangle.getBounds().height);
mainWindow.rectangle.show();
}
public void mouseReleased(MouseEvent e)
{
Component c = mainWindow.getGlassPane();
c.hide();
objP = (objPanel)mainWindow.map.get(this.getName());
objP.setBounds(objP.getBounds().x,mainWindow.rectangle.getBounds().y,objP.getBounds().width,objP.getBounds().height
);
mainWindow.rectangle.hide();
System.out.println("Location: "+ this.getLocation());
this.setLocation(mainWindow.rectangle.getLocation());
System.out.println("Released:
"+mainWindow.rectangle.getLocation()+" "+mainWindow.map.values());
}
public void mouseClicked (MouseEvent e)
{
objP = (objPanel)mainWindow.map.get(this.getName());
System.out.println("objPanel "+objP.intOrder);
mainWindow.intOrder = objP.intOrder;
}
public void mouseExited (MouseEvent e) {}
public void mouseMoved (MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
}
public class objMainWindow extends JFrame implements ActionListener
{
public static objMainWindow mainWindow = null;
public JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
public objLegend legend = new objLegend();
public JPanel objViewer = new JPanel();
public JTextField textbox1 = new JTextField(20);
public objRectangle rectangle;
public objPanel panel;
public HashMap map = new HashMap();
public int intOrder;
public objMainWindow(String header)
{
super(header);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt)
{
System.exit(0);
}
});
setBounds( 300, 100, 700, 600 );
initGUI();
show();
mainWindow = this;
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = mainWindow.getSize();
mainWindow.setLocation(Math.max(0,(screenSize.width -
windowSize.width)/2),
Math.max(0,(screenSize.height-windowSize.height)/2));
}
/******more code******/
public void actionPerformed(ActionEvent e) {
objPanel objPanelI;
if ("add".equals(e.getActionCommand()))
{
panel = new objPanel();
panel.mainWindow = this;
panel.setOrder();
legend.container.add(panel);
legend.intLastIndexPanel = legend.intLastIndexPanel + 1;
panel.setName("objPanel"+legend.intLastIndexPanel);
map.put(panel.getName(), panel);//
panel.objLabel.setText(panel.getName());
panel.setSize(new Dimension (130,71));
System.out.println("added");
Rectangle rec = panel.getBounds();
System.out.println("PANEL: "+rec);
sp.revalidate();
}
}
/****more code****/
/****more code****/
public static void main(String[] args) throws Exception
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new objMainWindow("Paneles");
}
}
/*****the end********************************************************************/
> > I have created a small aplication in which I have a frame with a
> > splitPane, on the left side I have panels which are added on run time.
[quoted text clipped - 28 lines]
> glass pane and reposition the component based on the coordinates of the
> button up event.
I posted another "Follow up" before with the source code... this is
the missing code for the objMainWindow class:
private void initGUI() {
JButton boton = new JButton("ADD");
boton.setActionCommand("add");
boton.addActionListener(this);
JButton boton2 = new JButton("Get Top");
boton2.setActionCommand("gettop");
boton2.addActionListener(this);
JButton boton3 = new JButton("Set Top");
boton3.setActionCommand("settop");
boton3.addActionListener(this);
JButton boton4 = new JButton("Remove");
boton4.setActionCommand("remove");
boton4.addActionListener(this);
objViewer.add(boton);
objViewer.add(boton2);
objViewer.add(boton3);
objViewer.add(boton4);
objViewer.add(textbox1);
rectangle = new objRectangle();
legend.setAutoscrolls(true);
legend.container.add(rectangle);
sp.setTopComponent(legend);
sp.setBottomComponent(objViewer);
sp.setDividerLocation(130);
getContentPane().add( sp, BorderLayout.CENTER );
}
> > I have created a small aplication in which I have a frame with a
> > splitPane, on the left side I have panels which are added on run time.
[quoted text clipped - 28 lines]
> glass pane and reposition the component based on the coordinates of the
> button up event.