Results 1 to 11 of 11
Thread: passing references
- 05-26-2013, 09:48 AM #1
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
passing references
-I am now at the following situation:
There are three classes A, B and C
A contains several dialogs
B is the base class that holds the main method
C has a large series of panels that should be painted
-At B I initialise objects of A and C: called a, c
-I get the reference of the dialog´s OK buttons of A at the base class B which
permits me to say:
if (a.OKbuttonPushed) get a.getDialogmaterial
-Next I can paint the panels at C according the dialog settings of A by saying (at B, the base class) c.paintPanels.
-But I also have a mouselistener installed at the C class which readsout an individual click on each of the panels and returns the number of that particular panel.
All the above is working just fine (with help of you at the forum)
Now I would like to readout these panel-numbers of C: in real time when they are click selected!
This means I should have the mouselistener of C at B and pass all the references of the panels at C to B as well, right ? What a fuss......
Or might there by another way doing this (like using a interface)?Last edited by willemjav; 05-26-2013 at 11:52 AM.
- 05-26-2013, 12:03 PM #2
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
Re: passing references
Just fantasising some more in having this class that
implements a interface that has the method:
public void readPanelnumber(panelnumber pn) {}
Yes, that makes me think what are these actionlisteners etc anyway...
Probably they are just a bunch for declared variables that are
set by a clock pulse at a background threat, right?
- 05-26-2013, 05:19 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: passing references
Does each panel have its own mouseListener? If so, then the only the mouseListener for that panel should be receiving events when the mouse is detected by that particular panel. And you already have the object (source of events). Perhaps I am still not quite certain what you want to do when the mouse is detected.
Regards,
JimLast edited by jim829; 05-26-2013 at 05:24 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 05-26-2013, 05:59 PM #4
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
Re: passing references
No jim just on listener that I am adding to:
for (int i = 0; i < 366; i++)
paneldatref[i].getPanelRef().addMouseListener(Mhandler);
check eg an info string at each day-panel when clicked!
- 05-26-2013, 06:47 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: passing references
It's a given that you can determine which panel you are in when you click the mouse. What I don't understand is why you want to pass all the references and not just the reference of the clicked panel?
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 05-26-2013, 09:41 PM #6
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
Re: passing references
The panel is a calendar of the academic your of which each day is like a button,
and I need to reed out the clicks from within an other class.
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package studyrooms; /** * * @author willem */ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Calendar; import java.util.Locale; import javax.swing.*; import javax.swing.border.Border; /** * * */ class CalendarPanel extends JFrame { private Font theFont, smallFont; private Color bluecolor, blackcolor; private Calendar cal, calprint; private MouseHandler Mhandler; PanelData paneldata[]; private String monthlabel[], days[]; private int monthCons[]; private int daycount, thisyear, maxvacationfields; CalendarPanel() { cal = Calendar.getInstance(Locale.GERMANY); calprint = Calendar.getInstance(Locale.GERMANY); setBounds(30, 30, 1700, 900); setTitle("Calendar Window"); bluecolor = Color.BLUE; blackcolor = Color.BLACK; theFont = new Font("SansSerif", Font.PLAIN, 14); smallFont = new Font("SansSerif", Font.PLAIN, 11); monthlabel = new String[] {"SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST"}; days = new String[] {"lunes", "martes", "miércoles", "jueves", "viernes", "sábado", "domingo"}; monthCons = new int[] {Calendar.SEPTEMBER, Calendar.OCTOBER, Calendar.NOVEMBER, Calendar.DECEMBER, Calendar.JANUARY, Calendar.FEBRUARY, Calendar.MARCH, Calendar.APRIL, Calendar.MAY, Calendar.JUNE, Calendar.JULY, Calendar.AUGUST}; Mhandler = new MouseHandler(); // mousehandler for the panels paneldata = new PanelData[370]; // initialize day panel data array for (int i = 0; i < paneldata.length; i++) { paneldata[i] = new PanelData(); } daycount = 0; // this counts the days of the year for the panels // set the actual year as acedemic year year or year-1 thisyear = cal.get(Calendar.YEAR); thisyear = thisyear-1; Container contentPane = getContentPane(); contentPane.add(setupYearCalenderPanel(thisyear)); // System.out.println(" first year day (sunday=1) " + resetFirstLastDay(thisyear, 0)); // int maxday = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // int firstday = cal.getActualMinimum(Calendar.DAY_OF_MONTH); // System.out.println(" max day of that month " + maxday); // System.out.println(" first day of that month " + firstday); } public static void main(String[] args) { CalendarPanel frame = new CalendarPanel(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public int ThisYear() { return thisyear; } public PanelData[] Panelref() { return paneldata; } public JPanel setupYearCalenderPanel(int year) { // the 12 month of the year JPanel yearPanel = new JPanel(new GridLayout(3,4,2,2)); yearPanel.setBackground(Color.WHITE); for (int i = 0; i < 12; i++) { // set the month cicle if (i == 4) year = year +1; // shift for the acedemic year yearPanel.add(setupSingleCalenderPanel(resetFirstLastDay(year, i), year, i)); } return yearPanel; } public int resetFirstLastDay(int year, int month){ // month 0-11 // the acedemic year runs from sept to sept // one year shift for the leap-year of the month feb year=year-1; calprint.set(year, monthCons[month], 1); int weekday = calprint.get(Calendar.DAY_OF_WEEK); int maxday = calprint.getActualMaximum(Calendar.DAY_OF_MONTH); // scaling for the calendar graph weekday = weekday + 5; if (weekday > 6) weekday = weekday - 7; // System.out.println(" year >" + year + " month number >" + month + // " day of the week sunday=1 >" + weekday); // System.out.println("the mont " + month + " daynumber " + weekday + // " last day of the month " + maxday); return weekday; // the first day of the month 0 = monday until 6 } public JPanel setupSingleCalenderPanel(int day, int year, int month) { // for layout it is important that all of the grids get filled JPanel mainPanel = new JPanel(new BorderLayout()); mainPanel.setBackground(Color.WHITE); JPanel monthPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); monthPanel.setBackground(Color.WHITE); // first row, set the text of that particular month JLabel monthLabel = new JLabel(year + "/ " + monthlabel[month]); monthPanel.add(monthLabel); JPanel daysPanel = new JPanel(new GridLayout(7, 7, 1, 1)); daysPanel.setBackground(Color.WHITE); JPanel dPanel; JLabel dayLabel; // sets the second row that contains the week days for (int i = 0; i < 7; i++) { dPanel = new JPanel(new GridLayout(1, 1)); dPanel.setBackground(Color.WHITE); // dPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); dayLabel = new JLabel("", SwingConstants.CENTER); dayLabel.setBackground(Color.WHITE); dayLabel.setFont(smallFont); // dayLabel.setVerticalTextPosition(JLabel.BOTTOM); dayLabel.setVerticalAlignment(SwingConstants.BOTTOM); dayLabel.setText("" + days[i]); dPanel.add(dayLabel); daysPanel.add(dPanel); } // the first empty month day panels for (int i = 0; i < day; i++) { dPanel = new JPanel(); dPanel.setBackground(Color.WHITE); daysPanel.add(dPanel); } // set the actual month for getting the max day of that month // calculate the day panels and the rest panels to the fill the month // month 0-11, days 1-31 days of the week 1-7 sunday is 1 resetFirstLastDay(year, month); // this sets a particular date // this gives the max days of that particular month int maxday = calprint.getActualMaximum(Calendar.DAY_OF_MONTH); int restday = 31 - maxday; // setup the actual daypanels of the month for (int i = 0; i < maxday; i++) { dPanel = new JPanel(); dPanel.setBackground(Color.WHITE); daysPanel.add(dPanel); JLabel dateLabel = new JLabel("" + (i+1)); dPanel.add(dateLabel); dPanel.addMouseListener(Mhandler); paneldata[daycount].setPanelRef(dPanel); paneldata[daycount].setDateString(monthlabel[month] + "/ " + (i+1)); daycount++; Border etched = BorderFactory.createEtchedBorder(blackcolor, blackcolor); dPanel.setBorder(etched); } for (int i = 0; i < 11-day+restday; i++) { dPanel = new JPanel(); dPanel.setBackground(Color.WHITE); daysPanel.add(dPanel); } mainPanel.add(monthPanel, BorderLayout.NORTH); mainPanel.add(daysPanel, BorderLayout.CENTER); Border etched = BorderFactory.createEtchedBorder(blackcolor, blackcolor); mainPanel.setBorder(etched); return mainPanel; } class PanelData { // the year array for each day private boolean dayset; private JPanel panelreference; private String datestring; public void setDayAvailable(boolean dt) { dayset = dt; } public boolean getDayAvailable() { return dayset; } public void setDateString(String st) { datestring = st; } public String getDateString() { return datestring; } public void setPanelRef(JPanel pr) { panelreference = pr; } public JPanel getPanelRef() { return panelreference; } } public int CalcDaynumber(int year, int month) { int maxday = 0; year = year + 1; // concerning the leap year month = month + 4; // shift for the acedemic year sept-sept if (month >= 11) month = month - 11; for (int i = 0; i < month; i++) { resetFirstLastDay(year, i); // this sets a particular date // this gives the max days of that particular month int x = calprint.getActualMaximum(Calendar.DAY_OF_MONTH); maxday = maxday + x; } System.out.println(" month " + month + " days " + maxday); return maxday; } public class MouseHandler implements MouseListener { public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { int indx = 0; if (e.getClickCount() >= 2) { // roominfopanrl array for (int i = 0; i < paneldata.length; i++) { if (paneldata[i].getDayAvailable()) { indx = i; } else { indx = i; } if (e.getSource() == paneldata[i].getPanelRef()) { System.out.println(paneldata[i].getDateString() + " day number " + i); } } System.out.println(paneldata[indx].getDateString()); } } } }
- 05-26-2013, 09:42 PM #7
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
Re: passing references
pull it through the compiler and watch the monitor
if you know a way how to pass the ref of the clicked panel
(in real time when it is clicked so to speak), tell me.Last edited by willemjav; 05-26-2013 at 09:45 PM.
- 05-26-2013, 11:23 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: passing references
Here you go. All the panels are using the same mouseListener. The source of the event is the panel you want.
Java Code:private class MyMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent me) { MyPanel panel = (MyPanel) me.getSource(); /** Now you can do what you want with panel */ } }
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 05-26-2013, 11:35 PM #9
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
Re: passing references
That looks interesting and brings my java-understanding on a different level
thanks jim
ps I´ll print out panel first
- 05-26-2013, 11:39 PM #10
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: passing references
Here is a quick program I wrote to demonstrate what I believe you want.
Java Code:public class PanelReferenceTest { JFrame frame; public PanelReferenceTest() { frame = new JFrame("Panel Reference Test"); frame.setLayout(new FlowLayout()); MyMouseListener ml = new MyMouseListener(); for (int j = 1; j < 10; j++) { MyPanel mp = new MyPanel("P" + j); mp.addMouseListener(ml); frame.add(mp); } frame.setPreferredSize(new Dimension(300,300)); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new PanelReferenceTest(); } private class MyMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent me) { MyPanel panel = (MyPanel) me.getSource(); System.out.println("Panel " + panel.name + " clicked"); } } private class MyPanel extends JPanel { String name; MyPanel(String name) { this.name = name; setBorder(new LineBorder(Color.blue, 2)); setPreferredSize(new Dimension(50, 50)); } public void paintComponent(Graphics g) { super.paintComponent(g); // added g.drawString(name, 25, 25); } } }
Regards,
JimLast edited by jim829; 05-27-2013 at 02:04 AM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 05-27-2013, 01:02 AM #11
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
Similar Threads
-
About references.
By fatabass in forum New To JavaReplies: 8Last Post: 03-03-2012, 09:17 AM -
References and passing them.
By jammas615 in forum Advanced JavaReplies: 7Last Post: 01-07-2012, 11:01 AM -
Soft References
By rickcharles_b in forum Advanced JavaReplies: 0Last Post: 06-20-2007, 09:27 PM
Bookmarks