Archive for July, 2007

Java Swing O Reilly summing and averaging along (Mac os x web server)

Thursday, July 26th, 2007

Java Swing O Reilly summing and averaging along the columns. It would have been more work (but not much more work) to present the data in any particular column, letting the user choose the column to be displayed. Figure 16.3. A Chart component using a TableModel Here’s the application that produces both the pie chart and the table. It includes the TableModel as an anonymous inner class. This inner class is very simple, much simpler than the models we used earlier in this chapter; it just provides an array for storing the data, methods to get and set the data, and methods to provide other information about the table. Notice that we provided an isCellEditable() method that always returns true (the default method always returns false). Because we’re allowing the user to edit the table, we must also override setValueAt(); our implementation updates the data array and calls fireTableRowsUpdated() to notify any listeners that data has changed and they need to redraw. The rest of ChartTester just sets up the display; we display the pie chart as a popup, to make things somewhat more interesting. // ChartTester.java // import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class ChartTester extends JFrame { public ChartTester() { super(”Simple JTable Test”); setSize(300, 200); addWindowListener(new BasicWindowMonitor()); TableModel tm = new AbstractTableModel() { String data[][] = { {”Ron”, “0.00″, “68.68″, “77.34″, “78.02″}, {”Ravi”, “0.00″, “70.89″, “64.17″, “75.00″}, {”Maria”, “76.52″, “71.12″, “75.68″, “74.14″}, {”James”, “70.00″, “15.72″, “26.40″, “38.32″}, {”Ellen”, “80.32″, “78.16″, “83.80″, “85.72″} }; String headers[] = { “”, “Q1″, “Q2″, “Q3″, “Q4″ }; public int getColumnCount() { return headers.length; } - 459 -
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Java Swing O Reilly PagingModel pm = new (Make my own web site)

Thursday, July 26th, 2007

Java Swing O Reilly PagingModel pm = new PagingModel(); JTable jt = new JTable(pm); // Use our own custom scrollpane. JScrollPane jsp = PagingModel.createPagingScrollPaneForTable(jt); getContentPane().add(jsp, BorderLayout.CENTER); } public static void main(String args[]) { PagingTester pt = new PagingTester(); pt.setVisible(true); } } We just create an instance of our PagingModel to hold the data and construct a JTable with that model. Then we get a paging scroll pane from the JTable, and add that scroll pane to the content pane of a JFrame. 16.3 Charting Data with a TableModel Our last example shows that the table machinery isn’t just for building tables; you can use it to build other kinds of components, like the pie chart in Figure 16.3. If you think about it, there’s no essential difference between a pie chart, a bar chart, and many other kinds of data displays; they are all different ways of rendering data that’s logically kept in a table. When that’s the case, it is easy to use a TableModel to manage the data, and build your own component for the display. With AWT, building a new component was easy: you simply created a subclass of Component. With Swing, it’s a little more complex because of the distinction between the component itself and the user interface implementation. But it’s not terribly hard, particularly if you don’t want to brave the waters of pluggable look-and-feel. In this case, there’s no good reason to make pie charts that look different on different platforms, so we’ll opt for simplicity. We’ll call our new component a TableChart; it extends JComponent. Its big responsibility is keeping the data for the component updated; to this end, it listens for TableModelEvents from the TableModel to determine when changes have been made. To do the actual drawing, TableChart relies on a “delegate.” Our delegate will be a PieChartPainter, which is responsible for rendering the data on the screen. To keep things flexible, PieChartPainter is a subclass of ChartPainter, which gives us the option of building other kinds of chart painters bar chart painters, etc. in the future. ChartPainter extends ComponentUI, which is the base class for user interface delegates. Here’s where the model-viewcontroller architecture comes into play. The table model contains the actual data; TableChart is a controller that tells a delegate what and when to paint; and PieChartPainter is the “view” that actually paints a particular kind of representation on the screen. Just to prove that the same TableModel can be used with any kind of display, we also display an old fashioned JTable using the same data which turns out to be convenient, because we can use the JTable’s built-in editing capabilities to modify the data. If you change any field (including the name), the pie chart will immediately change to reflect the new data. The TableChart class is particularly interesting because it shows the “other side” of table model event processing. In the PagingModel of the previous example, we had to generate events as the data changed. Here, you see how those events might be handled. The TableChart has to register itself as a TableModelListener and respond to events so that it can redraw itself when you edit the table. The TableChart also implements one (perhaps unsightly) short cut: it presents the data by - 458 -
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Mac os x web server - Java Swing O Reilly methods of our table

Thursday, July 26th, 2007

Java Swing O Reilly methods of our table model; and we include some logic to disable buttons when we reach the top or bottom of the table. Finally, we turn on the scroll pane’s scrollbars, and add the buttons in the upper- and lower-right corners. Remember that if the scrollbars aren’t enabled, there won’t be any place to put the buttons. Our table is currently static: it displays data, but doesn’t have any means for updating the data. How would you implement table updates? We’ll leave this as a thought experiment. There’s some simple bookkeeping that you’d have to do, but the most interesting part would be implementing setValueAt() in the PagingModel class. Like getValueAt(), it would have to translate between logical rows in the data and physical rows in the JTable. It would have to call fireTableDataChanged() to generate a table model event and cause the JTable to update the display. But, you would also need a way to set the value of the cells that are not visible. For real applications, you might consider writing your own getRealValueAt() and setRealValueAt() that do not map incoming row values. Here’s the very simple Record class; it just provides column names and generates meaningless data, one record (row) at a time: // Record.java // A simple data structure for use with the PagingModel demo. // public class Record { static String headers[] = { “Record Number”, “Batch Number”, “Reserved” }; static int counter; String[] data; public Record() { data = new String[] { “” + (counter++), “” + System.currentTimeMillis(), “Reserved” }; } public String getValueAt(int i) { return data[i]; } public static String getColumnName(int i) { return headers[i]; } public static int getColumnCount() { return headers.length; } } And here’s the application that brings up the JTable using our paging model: // PagingTester.java // import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class PagingTester extends JFrame { public PagingTester() { super(”Paged JTable Test”); setSize(300, 200); addWindowListener(new BasicWindowMonitor()); - 457 -
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

My web server - Java Swing O Reilly // If we hit

Wednesday, July 25th, 2007

Java Swing O Reilly // If we hit the bottom of the data, disable the down button if (model.getPageOffset() == (model.getPageCount() - 1)) { downButton.setEnabled(false); } upButton.setEnabled(true); } } ); // Turn on the scrollbars; otherwise we won’t get our corners jsp.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); jsp.setHorizontalScrollBarPolicy (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); // add in the corners (page up/down) jsp.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, upButton); jsp.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER, downButton); return jsp; } } The PagingModel constructor fills an array with all our data. (The Record object does something simple to generate meaningless data; in real life, it would probably use JDBC to read data from a database.) Most of the methods in the PagingModel are fairly self-explanatory. The interesting ones have something to do with the table’s rows. getRowCount() isn’t computationally complex, but is typical of what we’ll see: instead of returning the number of rows of data, it returns pageSize, which is the number of rows we want to display. getValueAt() is only slightly more complex: it translates the desired row from the physical table into the actual row within the much larger logical table, and returns the appropriate data. (We don’t need to do anything to the column, but we would if we were making a table that paged in two directions.) We’ve added some convenience methods, getPageOffset(), getPageCount(), and getRealRowCount(), to provide information about the table’s actual size, and the portion we’re looking at. The pageDown() and pageUp() methods are a bit more interesting. These get called when the user clicks on either of the paging buttons that we’ll display with the table. When the user pages, these methods increment or decrement the model’s pageOffset variable, which records the current offset into the table. This effectively means that the data in the physical table (the table we display) has changed, although nothing has changed in the logical table at all. Because the physical table has changed, we call fireTableDataChanged(), which fires a TableModelEvent that tells the JTable to reload all the data. When it’s created, the JTable will register itself as a listener for table model events. Our table doesn’t let you change the page increment, but that would be a useful feature. Therefore, we provide a setPageSize() method that you could use to alter the page size. This method is interesting because, again, changing the page size does nothing to the logical table, but it effectively adds or deletes rows from the physical JTable on the screen. Therefore, this method figures out whether we’re adding or deleting rows; which rows are added or deleted, and then calls fireTableRowsInserted() or fireTableRowsDeleted() accordingly. The last important task that our table model has to perform is to build a JScrollPane that knows how to work properly with our table. This is implemented in the createPagingScrollPaneForTable() method. This method starts by getting a JScrollPane and then modifying it to work appropriately. The modifications are really quite simple. We create a pair of buttons to control the paging (the icons for the buttons are implemented by the rather simple ArrowIcon class, which we haven’t shown); we wire the buttons to the pageUp() and pageDown() - 456 -
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Free web hosts - Java Swing O Reilly return data.length; } public

Wednesday, July 25th, 2007

Java Swing O Reilly return data.length; } public int getPageSize() { return pageSize; } public void setPageSize(int s) { if (s == pageSize) { return; } int oldPageSize = pageSize; pageSize = s; if (pageSize < oldPageSize) { fireTableRowsDeleted(pageSize, oldPageSize - 1); } else { fireTableRowsInserted(oldPageSize, pageSize - 1); } } // update the page offset and fire a data changed (all rows) public void pageDown() { if (pageOffset < getPageCount() - 1) { pageOffset++; fireTableDataChanged(); } } // update the page offset and fire a data changed (all rows) public void pageUp() { if (pageOffset > 0) { pageOffset–; fireTableDataChanged(); } } // we’ll provide our own version of a scroll pane that includes // the page up and page down buttons by default. public static JScrollPane createPagingScrollPaneForTable(JTable jt) { JScrollPane jsp = new JScrollPane(jt); // Don’t choke if this is called on a regular table . . . if (! (jt.getModel() instanceof PagingModel)) { return jsp; } // Okay, go ahead and build the real scroll pane final PagingModel model = (PagingModel)jt.getModel(); final JButton upButton = new JButton(new ArrowIcon(ArrowIcon.UP)); upButton.setEnabled(false); // starts off at 0, so can’t go up final JButton downButton = new JButton(new ArrowIcon(ArrowIcon.DOWN)); upButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { model.pageUp(); // If we hit the top of the data, disable the up buttonif (model.getPageOffset() == 0) { upButton.setEnabled(false); } downButton.setEnabled(true); } } ); downButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { model.pageDown(); - 455 -
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Christian web host - Java Swing O Reilly // PagingModel.java // A

Tuesday, July 24th, 2007

Java Swing O Reilly // PagingModel.java // A larger table model that performs “paging” of its data. This model // reports a small number of rows (like 100 or so) as a “page” of data. You // can switch pages to view all of the rows as needed using the pageDown() // and pageUp() methods. Presumably, access to the other pages of data is // dictated by other GUI elements such as up/down buttons, or maybe a text // field that allows you to enter the page number you want to display. // import javax.swing.table.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class PagingModel extends AbstractTableModel { protected int pageSize; protected int pageOffset; protected Record[] data; public PagingModel() { this(10000, 100); } public PagingModel(int numRows, int size) { data = new Record[numRows]; pageSize = size; // fill our table with random data (from the Record() constructor) for (int i=0; i < data.length; i++) { data[i] = new Record(); } } // Return values appropriate for the visible table part public int getRowCount() { return pageSize; } public int getColumnCount() { return Record.getColumnCount(); } // Only works on the visible part of the table public Object getValueAt(int row, int col) { int realRow = row + (pageOffset * pageSize); return data[realRow].getValueAt(col); } public String getColumnName(int col) { return Record.getColumnName(col); } // use this method to figure out which page you are on public int getPageOffset() { return pageOffset; } public int getPageCount() { return (int)Math.ceil((double)data.length / pageSize); } // use this method if you want to know how big the real table is . . . we // could also write "getRealValueAt()" if we needed. public int getRealRowCount() { - 454 -
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Java Swing O Reilly The other table column

Tuesday, July 24th, 2007

Java Swing O Reilly The other table column model, rowHeaderModel, overrides addColumn() to do the opposite: it inserts the first column (the row header) into the table, and ignores all the other columns. These classes make the assumption that column (the row headers) is added to the table first, and never added again. As long as the table isn’t editable, that assumption will be valid. If the table were editable, we would have to add some logic to make sure we always know what column we’re working on. The rest of the code is almost self-explanatory. We create two JTable objects, jt (for the body) and headerColumn (for the row headers). We start by telling our tables to build their columns, since we specified our own column models. Our tables use the same TableModel, and the appropriate TableColumnModel. To make sure that row selection for the two tables is always in sync, we give them both the same SelectionModel. We give headerColumn a different color and disable column and cell selection. The only thing left is to arrange the display. We create a separate JViewport to display the row headers and put the header column in it. We set the viewport’s width to match the header column’s size. Then we disable autoresize mode for both tables (jt and headerColumn); this is necessary to make the scrollbars work appropriately. (Things get confusing if a table inside a scroll pane tries to resize itself.) Finally, we create a JScrollPane from jt. To get the row headers into the scroll pane, we add the viewport, which already contains headerColumn to the JScrollPane by calling setRowHeader(). Then we just slap the JScrollPane, which now contains both tables, into our JFrame’s content pane, and we’re done. 16.2 Large Tables with Paging Working conveniently with very large tables can be a pain. Scrolling up and down is fine as long as the table is only a few hundred lines long, but when it gets larger, a tiny movement in the scrollbar can change your position by a few thousand rows. One way to solve this is by combining paging with scrolling. We’ll create a table with 10,000 rows (large enough to make scrolling through the entire table a real hassle) and add buttons to page up and down 100 rows at a time. Within any group of 100 rows, you can use the scrollbar as usual to move around. Figure 16.2 shows the result. Figure 16.2. A paging (and scrolling) table In this example, we’re using a simple trick. There are really two tables to worry about: a logical table that contains all 10,000 rows, which might represent records that were read from a database, and the physical table that’s instantiated as a JTable object and displayed on the screen. To do this trick, we implement a new table model, PagingModel, which is a subclass of AbstractTableModel. This table model keeps track of the data for the entire logical table: all 10,000 rows. However, when a JTable asks it for data to display, it pretends it only knows about the 100 rows that should be on the screen at this time. It’s actually quite simple. (And we don’t even need to worry about any column models; the default column model is adequate.) Here’s the PagingModel code used to track the 10,000 records: - 453 -
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Java Swing (Web design tools) O Reilly JTable jt = new

Monday, July 23rd, 2007

Java Swing O Reilly JTable jt = new JTable(tm, cm); // Set up the header column and get it hooked up to everything JTable headerColumn = new JTable(tm, rowHeaderModel); jt.createDefaultColumnsFromModel(); headerColumn.createDefaultColumnsFromModel(); // Make sure that selections between the main table and the header stay // in sync (by sharing the same model) jt.setSelectionModel(headerColumn.getSelectionModel()); // Make the header column look pretty headerColumn.setMaximumSize(new Dimension(40, 10000)); headerColumn.setBackground(Color.lightGray); // If you want to make the header selection invisible, uncomment this // next line: // headerColumn.setSelectionBackground(Color.lightGray); headerColumn.setColumnSelectionAllowed(false); headerColumn.setCellSelectionEnabled(false); // Put it in a viewport that we can control a bit JViewport jv = new JViewport(); jv.setView(headerColumn); jv.setPreferredSize(headerColumn.getMaximumSize()); // Without shutting off autoResizeMode, our tables won’t scroll // correctly (horizontally, anyway) jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); headerColumn.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // We have to manually attach the row headers, but after that, the scroll // pane keeps them in sync JScrollPane jsp = new JscrollPane(jt); jsp.setRowHeader(jv); getContentPane().add(jsp, BorderLayout.CENTER); } public static void main(String args[]) { SimpleTable2 st = new SimpleTable2(); st.setVisible(true); } } The various models we use our subclasses of AbstractTableModel and DefaultTableColumnModel -are anonymous inner classes. The new table model doesn’t do anything really interesting; it just keeps track of the raw data. There’s an array of column headers; the data itself is computed in the getValueAt() method. Of course, in a real example, you’d have some way of looking up real data. Our TableColumnModels are where the magic happens. The addColumn() method, which they override, is called whenever a column is added to a table. The first of our two models, cm, keeps track of the body of the table. The first time it is called, it returns without doing anything effectively ignoring the first column in the table, which is the column of row headers. (It does set the local variable first to false, indicating that it’s already processed the headers.) For subsequent columns, addColumn() behaves the way you would expect: it sets a minimum column width, then calls the method in the superclass to insert the column in the table. - 452 -
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Java Swing O Reilly That’s the approach we’ve (Com web hosting)

Monday, July 23rd, 2007

Java Swing O Reilly That’s the approach we’ve chosen. Once we have our models, it is relatively simple to create two JTables that use the same TableModel, but different TableColumnModels. Each table displays only the columns that its column model allows. When we put the tables next to each other, one will serve as the row header, and the other will display the body. Following is the code for our not-so-simple table. // SimpleTable2.java // import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class SimpleTable2 extends JFrame { public SimpleTable2() { super(”Simple JTable Test”); setSize(300, 200); addWindowListener(new BasicWindowMonitor()); TableModel tm = new AbstractTableModel() { String data[] = {”", “a”, “b”, “c”, “d”, “e”}; String headers[] = {”", “Column 1″, “Column 2″, “Column 3″, “Column 4″, “Column 5″}; public int getColumnCount() { return data.length; } public int getRowCount() { return 1000; } public String getColumnName(int col) { return headers[col]; } // Synthesize some entries using the data values & the row # public Object getValueAt(int row, int col) { return data[col] + row; } }; // Create a column model for the main table. This model ignores the first// column added, and sets a minimum width of 150 pixels for all others. TableColumnModel cm = new DefaultTableColumnModel() { boolean first = true; public void addColumn(TableColumn tc) { // Drop the first column . . . that’ll be the row header if (first) { first = false; return; } tc.setMinWidth(150); super.addColumn(tc); } }; // Create a column model that will serve as our row header table. This// model picks a maximum width and only stores the first column. TableColumnModel rowHeaderModel = new DefaultTableColumnModel() { boolean first = true; public void addColumn(TableColumn tc) { if (first) { tc.setMaxWidth(35); super.addColumn(tc); first = false; } // Drop the rest of the columns . . . this is the header column only} }; - 451 -
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Java Swing O Reilly We’re going to look (Web hosting services)

Sunday, July 22nd, 2007

Java Swing O Reilly We’re going to look at three particular examples: A scrollable table with row headers. Remember that a JTable understands column headers, but doesn’t have any concept of a row header. Also, remember that a JScrollPane understands both column and row headers. In this example, we’ll show you how to add row headers to a JTable and make them work properly within a JScrollPane. A table that has an extremely large numbers of rows. Scrolling stops working well when you have more than a few hundred rows to work with. We’ll build a table with 10,000 rows, let you page up and down to select a range of 100 rows within the table, and then scroll back and forth within that more limited range. A TableChart component that builds pie charts based on the TableModel class used by JTable. In this example, the JTable is almost superfluous, although it provides a convenient way to edit the data in the pie chart. The real point is that the TableModel is a powerful abstraction that can be put to use even when there’s no table around. 16.1 A Table with Row Headers As we promised, this is a table with headers for both rows and columns. The JTable handles the column headers itself; we need to add machinery for the rows. Figure 16.1 shows what the resulting table looks like. It shows column labels, plus two data columns from a larger table. Scrolling works the way you would expect. When you scroll vertically, the row headers scroll with the data. You can scroll horizontally to see other data columns, but the row headers always remain on the screen. Figure 16.1. A table with row and column headers The trick is that we really have two closely coordinated tables: one for the row headers (a table with only one column) and one for the data columns. There is a single TableModel, but separate TableColumnModels for the two parts of the larger table. In the figure, the gray column on the left is the row header; it’s really column of the data model. To understand what’s going on, it helps to remember how a Swing table models data. The TableModel itself keeps track of all the data for the table, i.e., the values that are used to fill in the cells. There’s no reason why we can’t have two tables that share the same table model that’s one of the advantages of the model-view-controller architecture. Likewise, there’s no reason why we can’t have data in the table model that isn’t displayed; the table model can keep track of a logical table that is much larger than the table we actually put on the screen. This will be particularly important in the next example, but it’s also important here. The table that implements the row headers uses the first column of the data model and ignores everything else; the table that displays the data ignores the first column. The TableColumnModel keeps track of the columns and is called whenever we add, delete, or move a column. One way to implement tables that use or ignore parts of our data is to build table column models that do what we want: only add a particular column (or group of columns) to the table. - 450 -
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.