About Java and xBaseJ- P7 - Pdf 76

Chapter 2 – Mega­Zillionaire Application
191)
192) dueRptArea.append( "Date: " + heading_format.format( c.getTime()
)
193) + "\n "
194) + "Due NumbersReport\n");
195)
196) dueRptArea.append("\n Mega
Numbers\n\n");
197) dueRptArea.append(" NO Hits Since Pct_Hits Ave_Btwn
\n");
198) dueRptArea.append(" -- ---- ----- -------- --------
\n");
199)
200) l_x = 1;
201) while ( l_x < ELM_COUNT ) {
202) if ((double)m_elms[l_x].sinceLast > m_elms[l_x].aveBtwn) {
203) detail_line = " " + nf_elm.format( m_elms[ l_x].elmNo)
204) + " " + nf_hits.format( m_elms[ l_x].hitCount)
205) + " " + nf_since.format( m_elms[l_x].sinceLast)
206) + " " + nf_pct.format( m_elms[ l_x].pctHits)
207) + " " + nf_ave.format( m_elms[ l_x].aveBtwn)
208) + "\n";
209) dueRptArea.append( detail_line);
210) }
211) l_x++;
212) } // end while loop
213)
214) dueRptArea.append( "\n\n");
215) dueRptArea.setCaretPosition(0); // scroll back to top
216) } catch (xBaseJException x) {

This particular panel has only a refresh button to display at the top of the screen and a text
area which gets displayed inside of a scroll pane.  In truth, it looks much like the browse screen
snapshot I've provided you, only it has regular text instead of a spreadsheet inside of the scroll
pane.
Our panel uses a GridBagLayout.  Don't ask me to tell you where the name came from or all
of the details.  GridBagLayout is one of the few functional layouts existing in Java, at least in the
days of version 1.4.  The topic of layouts has become so heated and debated that the C++ cross
platform   GUI   library   named   Qt   has   released   a   Java   wrapper   for   its  library   and   many   Java
developers have begun migrating away from Swing.    When it comes to the subject of layout
managers and Java, it looks like the subject was “tabled until later” and later still hasn't come.
You position objects in a GridBagLayout via a GridBagConstraints object.  Ordinarily you
will fill in only the anchor and gridwidth values, leaving the rest of the GridBagConstraints fields
at their default values.  Normally gridwidth is a numeric constant such as 1 or 2, but it can be a
couple of “special”  values.  One such value is REMAINDER as shown on listing line 52.  This
tells the layout manager the object is the last one on the current line of objects it is building.
The anchor portion of GridBagConstraints has a lot of direction­sounding constants, which
can be a little confusing.    Most people assume that all of the NORTH­based constraints have
something to do with the top of the display and the SOUTH­based constraints have something to
do with the bottom of the display.  In general, this is true, but not as true as it sounds.  To start
with, the enclosing object, in this case our JPanel, can change the component orientation  and
instead of left­to­right­top­to­bottom the screen might be displayed right­to­left­bottom­to­top.  In
any case, NORTH is associated with the starting point and SOUTH with the finishing point.  We
will discuss this topic in greater detail when we cover the Entry screen.  I just wanted to get you
thinking about it now.  Layouts in Java are not an easy subject to discuss in a book.  Some readers
will have ten years of coding under their belts and a few hundred source templates saved on disk;
others will have only written HelloWorld.java.
We create our text area at listing lines 55 through 62, then place it in a scroll pane at listing
line 63.  Most of what I did was pretty self­explanatory.  We do, however, need to talk about the
setFont() call.  I chose to use the font name “Courier .”    Most systems which have some kind of
Web browser will have some kind of “Courier”  font installed on them.  When you are creating

-- ---- ----- -------- --------
30 0,035 00,010 000.092 009.886
16 0,036 00,010 000.094 009.583
52 0,041 00,009 000.108 008.293
23 0,027 00,014 000.071 013.111
25 0,040 00,010 000.105 008.525
48 0,042 00,010 000.110 008.071
43 0,038 00,011 000.100 009.026
45 0,031 00,014 000.081 011.290
03 0,030 00,015 000.079 011.700
46 0,044 00,011 000.116 007.659
04 0,031 00,016 000.081 011.290
123
Chapter 2 – Mega­Zillionaire Application
Not exactly what I would call human­friendly output.  We discussed the Formatter class on
page 54.  This class added something which was required to bring Java into the business world,
the ability to create a columnar report.  Not one of you would pay your credit card bill if the text
swam all over the page, but that is just what the Java developers at Sun thought we should put up
with until Java 1.5 came out.  Our output will look as follows, and we will live with it for now.
Mega Numbers
NO Hits Since Pct_Hits Ave_Btwn
-- ---- ----- -------- --------
02 13 11 0.000 10.222
27 7 15 0.000 11.333
22 13 16 0.000 9.946
32 7 18 0.000 11.333
31 7 19 0.000 10.686
08 6 22 0.000 12.931
19 5 23 0.000 11.750
05 6 21 0.000 9.146

5) import java.awt.event.*;
6) import javax.swing.*;
7) import java.util.*;
8) import java.text.*;
9)
10) import org.xBaseJ.*;
11) import org.xBaseJ.fields.*;
12) import org.xBaseJ.Util.*;
13) import org.xBaseJ.indexes.NDX;
14)
15) import com.logikal.megazillxBaseJ.MegaDBF;
16)
17)
18) public class MegaXbaseBrowsePanel extends JPanel
19) implements ActionListener {
20)
21) private JPanel mainPanel;
22) private JScrollPane sp;
23) private JButton refreshButton;
24) private JTable drawTable;
25) private DateFormat file_date_format = new SimpleDateFormat( "yyyyMMdd");
26) private DateFormat out_date_format = new SimpleDateFormat( "yyyy/MM/dd");
27)
28)
29) final static String columnNames [] = {"Draw_Dt ", "No_1", "No_2"
30) , "No_3", "No_4", "No_5", "Mega_No"};
31)
32) //;;;;;;;;;;
33) // Constructor
34) //;;;;;;;;;;

63) private Object[][] fetchTableData( ) {
64) int l_record_count=0, l_x, l_y, l_try_count;
65) String serverResponse=null;
66)
67) MegaDBF aDB = new MegaDBF();
68) aDB.open_database();
69)
70) DBF d = aDB.getDBF();
71)
72) l_record_count = d.getRecordCount();
73) System.out.println( "Record count " + l_record_count);
74)
75) // declare an array to fill based upon rows in table
76) //
77) Object tableData [][] = new Object[ l_record_count] [7];
78)
79)
80) // Fill our new array”
81) //
82) try {
83) l_x = 0;
84) d.startTop();
85) while ( l_x < l_record_count) {
86) try {
87) d.findNext();
88)
89) tableData[ l_x] [0] = out_date_format.format(
90) file_date_format.parse
( aDB.Draw_Dt.get()));
91) tableData[ l_x] [1] = new Integer( aDB.No_1.get().trim());

121) l_x = l_record_count + 1;
122) JRootPane m = (JRootPane)
126
Chapter 2 – Mega­Zillionaire Application
123) SwingUtilities.getAncestorOfClass( JRootPane.class,
refreshButton);
124) if ( m != null)
125) {
126) JOptionPane.showMessageDialog(m, j.toString(), "Browse",
127) JOptionPane.ERROR_MESSAGE);
128) }
129) else
130) System.out.println( "m was null");
131) }
132)
133) aDB.close_database();
134)
135) return tableData;
136) } // end fetchTableData method
137)
138)
139)
140) public void actionPerformed(ActionEvent event) {
141) System.out.println( "Entered action event");
142) mainPanel.setVisible( false);
143) mainPanel.remove( sp);
144)
145) //
146) // Build a new table and scroll panel
147) //

application is run there won't be a database.  Even if there was a database, I would still need the
refresh button so the user can verify that records he or she adds via the Entry panel are actually in
the database along with all of the other data.
Listing line 63 might look a bit odd if you  haven't done much with arrays in Java.   This
private method returns a newly allocated two dimensional array of objects.  While I could have
hard­coded the second dimension at the method level, I opted to leave it at the point of allocation.
We have seven fields, so we need seven columns, but until we obtain the record count from the
database, we have no idea how many rows are needed.
128


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status