Microsoft
Software
Hardware
Network
Question : GUI Tree Problem
The best way to explain my problem is as follows...
Please compile the two files MonitorPanel and RunMonitorPanel and run the RunMonitorPanel class.
You will see a small GUI pops up with a tree with two nodes "panels" and "modes".
Step 1. Right click on any of these branches it will popup "Create alarm panel" and "Create Alarm".
Step 2. Now expand any of the nodes ( you r will see app1, app2 etc). Do step 1.
Why is that the pop up is not coming out now?
import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent
;
import java.awt.event.ActionListe
ner;
import java.awt.event.MouseAdapte
r;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSele
ctionEvent
;
import javax.swing.event.TreeSele
ctionListe
ner;
import javax.swing.tree.DefaultMu
tableTreeN
ode;
import javax.swing.tree.DefaultTr
eeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelec
tionModel;
public class MonitorPanel extends Panel implements TreeSelectionListener {
private JPanel configPanel = new JPanel();
private JLabel configPathLabel = new JLabel("Path");
private JTextField configPathField = new JTextField(20);
private JLabel configModeLabel = new JLabel("Mode");
private JTextField configModeField = new JTextField(2);
//
private DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("to
p");
private DefaultMutableTreeNode apModes = new DefaultMutableTreeNode("pa
nels");
private DefaultMutableTreeNode alarmsNode = new DefaultMutableTreeNode("mo
des");
private DefaultTreeModel treeModel = new DefaultTreeModel(rootNode)
;
private JTree tree;
private JScrollPane treePane;
private CAPopupMenu caPopup = new CAPopupMenu();
private CAPPopupMenu capPopup = new CAPPopupMenu();
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//
/** Constructs an instance. */
public MonitorPanel() {
// wire the config panel
configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.LINE_AXIS));
configPanel.add(configPath
Label);
configPanel.add(configPath
Field);
configPanel.add(configMode
Label);
configPanel.add(configMode
Field);
configPathField.setEditabl
e(false);
configModeField.setEditabl
e(false);
// wire the tree
rootNode.add(apModes);
rootNode.add(alarmsNode);
tree = new JTree(treeModel);
tree.setRootVisible(false)
;
tree.setShowsRootHandles(t
rue);
tree.getSelectionModel().s
etSelectio
nMode(Tree
SelectionM
odel.SINGL
E_TREE_SEL
ECTION);
treePane = new JScrollPane(tree);
//
setLayout(new BorderLayout());
add(configPanel, BorderLayout.NORTH);
add(treePane, BorderLayout.CENTER);
//
tree.addMouseListener(new MyMouseListener());
}
public void setConfigPath(String path) {
configPathField.setText(pa
th);
}
public void setConfigMode(String mode) {
configModeField.setText(mo
de == null ? "" : mode);
}
public void setAlarmPanelNames(List<St
ring> names) {
apModes.removeAllChildren(
);
// set names to current list
for (String name : names) {
addAPName(name, false);
}
}
public DefaultMutableTreeNode addAPName(String alarmPanelName, boolean shouldBeVisible) {
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(ala
rmPanelNam
e);
treeModel.insertNodeInto(c
hildNode, apModes, apModes.getChildCount());
if (shouldBeVisible) {
tree.scrollPathToVisible(n
ew TreePath(childNode.getPath
()));
}
return childNode;
}
public void setAlarmNames(List<String>
names) {
alarmsNode.removeAllChildr
en();
// set names to current list
for (String name : names) {
addAlarmName(name, false);
}
}
public DefaultMutableTreeNode addAlarmName(String alarmName, boolean shouldBeVisible) {
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(ala
rmName);
treeModel.insertNodeInto(c
hildNode, alarmsNode, alarmsNode.getChildCount()
);
if (shouldBeVisible) {
tree.scrollPathToVisible(n
ew TreePath(childNode.getPath
()));
}
return childNode;
}
@Override
public void valueChanged(TreeSelection
Event e) {
//Returns the last path element of the selection.
//This method is useful only when the selection model allows a single selection.
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathCo
mponent();
if (node == null) //Nothing is selected.
{
return;
}
System.out.println("TreeSe
lectionLis
tener.valu
eChanged()
: node=" + node.getUserObject());
}
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//
private static class MyDefaultMutableTreeNode extends DefaultMutableTreeNode {
public MyDefaultMutableTreeNode(O
bject userObject) {
super(userObject);
}
@Override
public boolean isLeaf() {
return false;
}
}
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//
/**
* Listens for request to show popup
*/
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
TreePath selPath = tree.getPathForLocation(e.
getX(), e.getY());
if (selPath != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathCompone
nt();
if (node.equals(alarmsNode)) {
System.out.println("select
ed alarms node");
caPopup.show(e.getComponen
t(), e.getX(), e.getY());
} else if (node.equals(apModes)) {
System.out.println("select
ed alarm panels node");
capPopup.show(e.getCompone
nt(), e.getX(), e.getY());
} else if (alarmsNode.isNodeChild(no
de)) {
System.out.println("select
ed alarm node");
} else if (apModes.isNodeChild(node)
) {
System.out.println("select
ed alarm panel node");
}
}
}
}
}
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//
private class CAPopupMenu extends JPopupMenu {
private JMenuItem selectAlarmMenuItem = new JMenuItem("Create Alarm");
public CAPopupMenu() {
add(selectAlarmMenuItem);
selectAlarmMenuItem.addAct
ionListene
r(new ActionListener() {
public void actionPerformed(ActionEven
t e) {
System.out.println("creati
ng alarm");
}
});
}
} //////////////////////////
//////////
//////////
//////////
//////////
//////////
//
private class CAPPopupMenu extends JPopupMenu {
private JMenuItem selectAlarmMenuItem = new JMenuItem("Create Alarm Panel");
public CAPPopupMenu() {
add(selectAlarmMenuItem);
selectAlarmMenuItem.addAct
ionListene
r(new ActionListener() {
public void actionPerformed(ActionEven
t e) {
System.out.println("creati
ng alarm panel");
}
});
}
}
}
==========================
==========
==========
==========
==========
====
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Arrays;
import javax.swing.JFrame;
public class RunMonitorPanel {
/** Construct an instance. */
public RunMonitorPanel() {
}
/**
* Center a Component on the screen.
* @param target the component to be centered.
*/
private static void centerOnScreen(Component target) {
if (target != null) {
Dimension screenSize = Toolkit.getDefaultToolkit(
).getScree
nSize();
Dimension targetSize = target.getSize();
if (targetSize.height > screenSize.height) {
targetSize.height = screenSize.height;
}
if (targetSize.width > screenSize.width) {
targetSize.width = screenSize.width;
}
int x = (screenSize.width - targetSize.width) / 2;
int y = (screenSize.height - targetSize.height) / 2;
target.setLocation(x, y);
}
}
/**
* Create and show the GUI.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("MonitorPanel");
frame.setDefaultCloseOpera
tion(JFram
e.EXIT_ON_
CLOSE);
// Set up the content pane.
MonitorPanel panel = new MonitorPanel();
frame.getContentPane().add
(panel);
// set some values for display
panel.setConfigPath("/tmp/
config");
panel.setConfigMode("rw");
String[] alarmNames = {"a1","a2","a3"};
panel.setAlarmNames(Arrays
.asList(al
armNames))
;
String[] alarmPanelNames = {"ap1", "ap2", "ap3"};
panel.setAlarmPanelNames(A
rrays.asLi
st(alarmPa
nelNames))
;
// Pack and show the window.
frame.pack();
centerOnScreen(frame);
frame.setVisible(true);
}
/**
* Create and show the GUI.
*/
public static void main(String[] args) {
javax.swing.SwingUtilities
.invokeLat
er(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
==========================
==========
==========
==========
==========
Answer : GUI Tree Problem
I guess it should be minus 45:
WHERE providerID IN ( 'A000000015210B','A0000000
03731B' )
AND Input_Date < DATEADD(day, -45, Received_Date)
or
WHERE providerID IN ( 'A000000015210B','A0000000
03731B' )
AND Received_Date > DATEADD(day, 45, Input_Date)
Random Solutions
Windows 2008 64bit system showing only 4GB usable...
Scheduling script to run every 10 minuts
website
Exchange server 2007 large mailing
the system cannot end this program because it is waiting for a response from you
get latest file in the directory in unix
MSExchangeIS Event ID 9582 every hour - on Exchange 2003
How do I make a database that stores data from a form with sections that have an unknown number of entries
After Effect
Javascript / JSP not working properly