Results 1 to 2 of 2
- 09-23-2008, 07:14 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
Menu item not working properly for mouse events
i have added popupmenu to the applet...based on the menu selected methods 'll be
called..now the problem is,for all menu selection only a particular method is called..can anyone
say me where i'm gng wrong..
Java Code:import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.font.FontRenderContext; import java.awt.font.LineMetrics; import java.text.ParseException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.logging.Level; import java.util.logging.Logger; public class TestApplet extends JApplet { JScrollPane scroll; @Override public void init() { Container con = getContentPane(); con.setLayout(new BorderLayout()); setPreferredSize(new Dimension(300, 0)); scroll = new JScrollPane(new ImagePanel()); getContentPane().add(scroll, BorderLayout.CENTER); setBackground(Color.WHITE); scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);//ScrollBar();//iewportView(new ImagePanel()); } private class ImagePanel extends JPanel implements MouseListener { Rectangle [][] r ; boolean candrag = false; int position; boolean change = false; JPopupMenu popupmenu; JMenuItem menu; JMenuItem WeeklyView; JMenuItem fitAll; JMenuItem MonthlyView; private boolean monthly_view = false; private boolean fit_all = false; public ImagePanel() { r = new Rectangle[2][]; prepareData(); addMouseListener(this); //addMouseMotionListener(this); popupmenu = new JPopupMenu(); this.add(popupmenu); WeeklyView = new JMenuItem("Weekly View"); fitAll = new JMenuItem("Fit All"); MonthlyView = new JMenuItem("Monthly View"); popupmenu.add(WeeklyView); popupmenu.add(MonthlyView); popupmenu.add(fitAll); popupmenu.setSize(60,70); add(popupmenu); popupmenu.addMouseListener(this); MonthlyView.addMouseListener(this); fitAll.addMouseListener(this); WeeklyView.addMouseListener(this); } public void monthlyView(Graphics2D g2) { int x1 = 200; int x2=215; int x = 288; int no_of_months =8; Font font = new Font("Ariel", Font.PLAIN, 7); for(int i=0;i<3 ;i++) { g2.drawRect(x1, 10, x, 45); int j1=1; //g2.drawLine(x1+x+2, 10, x+x1+2, 10); for(int j=0;j<18;j++) { g2.drawLine(x2,50,x2,55); g2.setFont(font); g2.drawString(""+j1,x2,40); j1+=2; x2 +=16; } //x2 +=15; //x2 -=30; x1 +=x; //x1 +=180; } } public int weeklyView(Graphics2D g2) { int panel_width =(30*27)+300; try { int x = 200; int y = 10; int width = 35; int ht = 45; int x1 = 200; String date = "d1"; Font font = new Font("Ariel", Font.PLAIN, 7); long from; long to; int n = 168; //int x = 300; int x1_cal = 200; int x3 = x; int width1 = 30; int daydx = width1 / 2; int dayx = (daydx + x); int f2 = x + n; int total_no_days = 156; long days = (long) total_no_days; //no_ofday = (int) days ; g2.setPaint(Color.BLACK); Calendar c = Calendar.getInstance(); // current date* Calendar c1 = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy"); String strtdate = "05/28/08"; String end = "10/30/08"; java.util.Date local_fromDate = sdf.parse(strtdate); java.util.Date toDate = sdf.parse(end); //System.out.println(toDate); c.setTime(local_fromDate); c1.setTime(local_fromDate); //SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); SimpleDateFormat sdf1 = new SimpleDateFormat("MMM"); Font font1 = new Font("Ariel", Font.BOLD, 9); g2.setFont(font1); for (int i = 0; i < 27; i++) { //String str = srt_date_str.get(i); int str_date = c.getTime().getDate(); String str = sdf1.format(c.getTime()); int str_year = c.getTime().getYear();//.getDate()); int year = str_year+1900; //g2.drawRect(x, y, width, ht); g2.setPaint(new Color(0, 128, 192)); g2.fillRect(x, y, width, ht); g2.setPaint(Color.BLACK); g2.drawRect(x, y, width, ht); // g2.drawString("" + i, x + 15, 25); g2.setPaint(Color.white); g2.drawString(""+str_date+"-"+str, dayx - 10, 30); g2.drawString(""+year,dayx-8,40); //for (int j = 0; j < 6; j++) // { // //g2.drawLine(x1, 55, x1, 50); // g2.setFont(font); // //g2.drawString(date, x1, 50); // x1 += 5; // } int nextdate = c.get(Calendar.DATE) + 6; c.set(Calendar.DATE, nextdate); //String srt1 = sdf.format(c.getTime()); //g2.drawString("To", dayx-8, 35); //g2.drawString(srt1, dayx-15, 45); //int nextdate1 = c1.get(Calendar.DAY_OF_WEEK) + 1; //c1.set(Calendar.DAY_OF_WEEK, nextdate1); dayx += width; x += width; } } catch (ParseException ex) { } return panel_width; } public void drawPanel(Graphics2D g2) { int margin = 10; int imgborder = 10; int imgline1 = 200; int titlebarx = 200; int titlebary = 50; int w= getWidth(); Font font = new Font("Ariel", Font.PLAIN, 16); FontRenderContext frc = g2.getFontRenderContext(); float width = (float)font.getStringBounds("hai", frc).getWidth(); LineMetrics lm = font.getLineMetrics("hai", frc); float sx = (w - width)/2; //g2.setPaint(new Color(0, 128, 192)); g2.setPaint(new Color(0, 128, 192)); g2.fillRect(imgborder, imgborder, 190, 45); g2.setPaint(Color.BLACK); g2.drawRect(imgborder, imgborder, 190, 45); g2.drawLine(imgline1, 0, imgline1, getHeight()); //g2.setPaint(Color.WHITE); g2.drawLine(margin, margin, titlebarx, titlebary); System.out.println(titlebarx); g2.drawString("RESOURCE", 40, 40); g2.drawString("TIME", 80, 25); } public void prepareData() { r[0] = new Rectangle[2]; r[1] = new Rectangle[3]; int x = 100; int y = 50; int width = 35; int h = 10; int count = 2; for(int i=0;i<r.length;i++) { r[i] = new Rectangle[count]; r[i][0] = new Rectangle(x,y,width,h); y += 50; x += 50; int itr=0; for(int j=0;j<r[i].length-1;j++){ itr +=1; r[i][itr] = new Rectangle(x+40,y+40,width,h); x +=40; y +=40; } count +=1; } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.create(); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(getBackground()); g2.fillRect(0, 0, getWidth(), getHeight()); g2.setColor(getForeground()); g2.setBackground(Color.WHITE); drawPanel(g2); if(monthly_view) { monthlyView(g2); } if(fit_all) { weeklyView(g2); } } public void mouseClicked(MouseEvent e) { Point p = e.getPoint(); if(monthly_view){ repaint(); } if(fit_all){ repaint(); } } public void mousePressed(MouseEvent e) { Point p = e.getPoint(); for(int i=0;i<r.length;i++){ if(r[i][0].contains(p)){ candrag = true; position = i; break; } } /*if the rectangle is clicked twice,the 'change' will set to true..and x and width of the rectangle will be changed*/ for(int k=0;k<r.length;k++) { if(r[k][0].contains(p) && e.getClickCount() == 2){ change = true; } } if(e.getButton() == 3) { popupmenu.show(this,e.getX(),e.getY()); popupmenu.setVisible(true); } else { popupmenu.disable(); } if(MonthlyView.contains(p)) { monthly_view =true; repaint(); } else if(fitAll.contains(p)){ fit_all = true; repaint(); } } public void mouseReleased(MouseEvent e) { candrag = false; } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } }
- 09-23-2008, 08:56 AM #2
When you get deprectaion warnings from the compiler, follow the instructions to
receive the details, like this:
Then look up each deprecated method in its class api in the javadocs to find outJava Code:C:\jexp>javac ta.java Note: ta.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. C:\jexp>javac -Xlint:deprecation ta.java ta.java:158: warning: [deprecation] getDate() in java.util.Date has been deprecated int str_date = c.getTime().getDate(); ^ ta.java:160: warning: [deprecation] getYear() in java.util.Date has been deprecated int str_year = c.getTime().getYear();//.getDate()); ^ ta.java:334: warning: [deprecation] disable() in javax.swing.JComponent has been deprecate d popupmenu.disable(); ^ 3 warnings C:\jexp>
what is recommended/used to replace it. For instance, the disable method
in the JPopupMenu class is replaced by the Component method setEnabled.
The answer to your question is to add the menuItems to an ActionListener instead
of a MouseListener.
Java Code:// <applet code="TA" width="400" height="400"></applet> import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.font.FontRenderContext; import java.awt.font.LineMetrics; import java.text.ParseException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.logging.Level; import java.util.logging.Logger; public class TA extends JApplet { JScrollPane scroll; @Override public void init() { Container con = getContentPane(); con.setLayout(new BorderLayout()); setPreferredSize(new Dimension(300, 0)); scroll = new JScrollPane(new ImagePanel()); getContentPane().add(scroll, BorderLayout.CENTER); setBackground(Color.WHITE); scroll.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); } private class ImagePanel extends JPanel implements MouseListener, ActionListener { Rectangle[][] r ; boolean candrag = false; int position; boolean change = false; JPopupMenu popupmenu; JMenuItem menu; JMenuItem WeeklyView; JMenuItem fitAll; JMenuItem MonthlyView; private boolean monthly_view = false; private boolean fit_all = false; public ImagePanel() { r = new Rectangle[2][]; prepareData(); addMouseListener(this); //addMouseMotionListener(this); popupmenu = new JPopupMenu(); // this.add(popupmenu); WeeklyView = new JMenuItem("Weekly View"); fitAll = new JMenuItem("Fit All"); MonthlyView = new JMenuItem("Monthly View"); popupmenu.add(WeeklyView); popupmenu.add(MonthlyView); popupmenu.add(fitAll); popupmenu.setSize(60,70); add(popupmenu); MonthlyView.addActionListener(this); fitAll.addActionListener(this); WeeklyView.addActionListener(this); // popupmenu.addMouseListener(this); // MonthlyView.addMouseListener(this); // fitAll.addMouseListener(this); // WeeklyView.addMouseListener(this); } public void monthlyView(Graphics2D g2) { System.out.println("drawing monthlyView"); int x1 = 200; int x2=215; int x = 288; int no_of_months =8; Font font = new Font("Ariel", Font.PLAIN, 7); for(int i=0;i<3 ;i++) { g2.drawRect(x1, 10, x, 45); int j1=1; //g2.drawLine(x1+x+2, 10, x+x1+2, 10); for(int j=0;j<18;j++) { g2.drawLine(x2,50,x2,55); g2.setFont(font); g2.drawString(""+j1,x2,40); j1+=2; x2 +=16; } //x2 +=15; //x2 -=30; x1 +=x; //x1 +=180; } } public int weeklyView(Graphics2D g2) { System.out.println("drawing weeklyView"); int panel_width =(30*27)+300; try { int x = 200; int y = 10; int width = 35; int ht = 45; int x1 = 200; String date = "d1"; Font font = new Font("Ariel", Font.PLAIN, 7); long from; long to; int n = 168; //int x = 300; int x1_cal = 200; int x3 = x; int width1 = 30; int daydx = width1 / 2; int dayx = (daydx + x); int f2 = x + n; int total_no_days = 156; long days = (long) total_no_days; //no_ofday = (int) days ; g2.setPaint(Color.BLACK); Calendar c = Calendar.getInstance(); // current date* Calendar c1 = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy"); String strtdate = "05/28/08"; String end = "10/30/08"; java.util.Date local_fromDate = sdf.parse(strtdate); java.util.Date toDate = sdf.parse(end); //System.out.println(toDate); c.setTime(local_fromDate); c1.setTime(local_fromDate); //SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); SimpleDateFormat sdf1 = new SimpleDateFormat("MMM"); Font font1 = new Font("Ariel", Font.BOLD, 9); g2.setFont(font1); for (int i = 0; i < 27; i++) { //String str = srt_date_str.get(i); int str_date = c.getTime().getDate(); String str = sdf1.format(c.getTime()); int str_year = c.getTime().getYear();//.getDate()); int year = str_year+1900; //g2.drawRect(x, y, width, ht); g2.setPaint(new Color(0, 128, 192)); g2.fillRect(x, y, width, ht); g2.setPaint(Color.BLACK); g2.drawRect(x, y, width, ht); // g2.drawString("" + i, x + 15, 25); g2.setPaint(Color.white); g2.drawString(""+str_date+"-"+str, dayx - 10, 30); g2.drawString(""+year,dayx-8,40); //for (int j = 0; j < 6; j++) // { // //g2.drawLine(x1, 55, x1, 50); // g2.setFont(font); // //g2.drawString(date, x1, 50); // x1 += 5; // } int nextdate = c.get(Calendar.DATE) + 6; c.set(Calendar.DATE, nextdate); //String srt1 = sdf.format(c.getTime()); //g2.drawString("To", dayx-8, 35); //g2.drawString(srt1, dayx-15, 45); //int nextdate1 = c1.get(Calendar.DAY_OF_WEEK) + 1; //c1.set(Calendar.DAY_OF_WEEK, nextdate1); dayx += width; x += width; } } catch (ParseException ex) { System.out.println("parse error: " + ex.getMessage()); } return panel_width; } public void drawPanel(Graphics2D g2) { int margin = 10; int imgborder = 10; int imgline1 = 200; int titlebarx = 200; int titlebary = 50; int w= getWidth(); Font font = new Font("Ariel", Font.PLAIN, 16); FontRenderContext frc = g2.getFontRenderContext(); float width = (float)font.getStringBounds("hai", frc).getWidth(); LineMetrics lm = font.getLineMetrics("hai", frc); float sx = (w - width)/2; //g2.setPaint(new Color(0, 128, 192)); g2.setPaint(new Color(0, 128, 192)); g2.fillRect(imgborder, imgborder, 190, 45); g2.setPaint(Color.BLACK); g2.drawRect(imgborder, imgborder, 190, 45); g2.drawLine(imgline1, 0, imgline1, getHeight()); //g2.setPaint(Color.WHITE); g2.drawLine(margin, margin, titlebarx, titlebary); System.out.println(titlebarx); g2.drawString("RESOURCE", 40, 40); g2.drawString("TIME", 80, 25); } public void prepareData() { r[0] = new Rectangle[2]; r[1] = new Rectangle[3]; int x = 100; int y = 50; int width = 35; int h = 10; int count = 2; for(int i=0;i<r.length;i++) { r[i] = new Rectangle[count]; r[i][0] = new Rectangle(x,y,width,h); y += 50; x += 50; int itr=0; for(int j=0;j<r[i].length-1;j++){ itr +=1; r[i][itr] = new Rectangle(x+40,y+40,width,h); x +=40; y +=40; } count +=1; } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.create(); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(getBackground()); g2.fillRect(0, 0, getWidth(), getHeight()); g2.setColor(getForeground()); g2.setBackground(Color.WHITE); drawPanel(g2); if(monthly_view) { monthlyView(g2); } if(fit_all) { weeklyView(g2); } } public void actionPerformed(ActionEvent e) { JMenuItem item = (JMenuItem)e.getSource(); System.out.println("item = " + item.getActionCommand()); if(item == WeeklyView) { monthly_view = false; } if(item == MonthlyView) { monthly_view = true; } if(item == fitAll) { fit_all = !fit_all; } repaint(); } public void mouseClicked(MouseEvent e) { /* Point p = e.getPoint(); if(monthly_view){ repaint(); } if(fit_all){ repaint(); } */ } public void mousePressed(MouseEvent e) { Point p = e.getPoint(); for(int i=0;i<r.length;i++){ if(r[i][0].contains(p)){ candrag = true; position = i; break; } } /*if the rectangle is clicked twice,the 'change' will set to true..and x and width of the rectangle will be changed*/ for(int k=0;k<r.length;k++) { if(r[k][0].contains(p) && e.getClickCount() == 2){ change = true; } } if(e.getButton() == 3) { popupmenu.show(this,e.getX(),e.getY()); popupmenu.setVisible(true); } else { popupmenu.disable(); } /* if(MonthlyView.contains(p)) { monthly_view =true; repaint(); } else if(fitAll.contains(p)){ fit_all = true; repaint(); } */ } public void mouseReleased(MouseEvent e) { candrag = false; } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e) { } } }
Similar Threads
-
Checking toggle state of a menu item
By xsive in forum SWT / JFaceReplies: 1Last Post: 09-22-2008, 02:42 PM -
My code is not working properly ..modify it
By Shyam Singh in forum New To JavaReplies: 14Last Post: 07-16-2008, 05:48 PM -
Demonstration of mouse events
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:45 PM -
Detecting Mouse Down In SWT Tree Item
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:51 PM -
Log4j not working properly....
By prakash_dev in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 12:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks