Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners grid view in java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Amit Chauhan
grid view in java
2 Answer(s)      4 years and 5 months ago
Posted in : Java Beginners


Is it possible to create dynamic grid view to display data from database.
if yes. how ??

Advertisement
View Answers

July 20, 2010 at 12:46 PM


Hi Friend,

Yes, you can. Try the following:

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.*;

public class GridView extends JPanel {
RadioButtonUI ui = new RadioButtonUI();
int pageSize = 5;

Model model = new Model();
TableRowSorter<Model> sorter = new TableRowSorter<Model>(model);
Box box = Box.createHorizontalBox();
public GridView() {
super(new BorderLayout());
JTable table = new JTable(model) {
public Component prepareRenderer(TableCellRenderer tcr, int row, int column) {
Component c = super.prepareRenderer(tcr, row, column);
if(isRowSelected(row)) {
c.setForeground(getSelectionForeground());
c.setBackground(getSelectionBackground());
}else{
c.setForeground(getForeground());
c.setBackground((row%2==0)?Color.lightGray:getBackground());
}
return c;
}
};
table.setIntercellSpacing(new Dimension());
table.setShowGrid(false);
table.setRowSorter(sorter);
showPages(10, 1);

add(new JScrollPane(table));
add(box, BorderLayout.SOUTH);
setPreferredSize(new Dimension(320, 240));
}
private void showPages(final int itemsPerPage, final int currentPageIndex) {
sorter.setRowFilter(filter(itemsPerPage, currentPageIndex-1));
ArrayList<JRadioButton> l = new ArrayList<JRadioButton>();

int startPageIndex = currentPageIndex-pageSize;
if(startPageIndex<=0) startPageIndex = 1;
int maxPageIndex = (model.getRowCount()/itemsPerPage)+1;
int endPageIndex = currentPageIndex+pageSize-1;
if(endPageIndex>maxPageIndex) endPageIndex = maxPageIndex;

if(currentPageIndex>1)
l.add(createRadioButtons(itemsPerPage, currentPageIndex-1, "Prev"));
for(int i=startPageIndex;i<=endPageIndex;i++)
l.add(createLinks(itemsPerPage, currentPageIndex, i-1));
if(currentPageIndex<maxPageIndex)
l.add(createRadioButtons(itemsPerPage, currentPageIndex+1, "Next"));

box.removeAll();
ButtonGroup bg = new ButtonGroup();
box.add(Box.createHorizontalGlue());
for(JRadioButton r:l) {
box.add(r); bg.add(r);
}
box.add(Box.createHorizontalGlue());
box.revalidate();
box.repaint();
l.clear();
}
private JRadioButton createLinks(final int itemsPerPage, final int current, final int target) {
JRadioButton radio = new JRadioButton(""+(target+1)) {
protected void fireStateChanged() {
ButtonModel model = getModel();
if(!model.isEnabled()) {
setForeground(Color.GRAY);
}else if(model.isPressed() && model.isArmed()) {
setForeground(Color.GREEN);
}else if(model.isSelected()) {
setForeground(Color.RED);
}
super.fireStateChanged();
}
};
radio.setForeground(Color.BLUE);
radio.setUI(ui);
if(target+1==current) {
radio.setSelected(true);
}
radio.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showPages(itemsPerPage, target+1);
}
});
return radio;
}


July 20, 2010 at 12:46 PM


continue..

private JRadioButton createRadioButtons(final int itemsPerPage, final int target, String title) {
JRadioButton radio = new JRadioButton(title);
radio.setForeground(Color.BLUE);
radio.setUI(ui);
radio.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
showPages(itemsPerPage, target);
}
});
return radio;
}
private RowFilter<Model,Integer> filter(final int itemsPerPage, final int target) {
return new RowFilter<Model,Integer>() {
public boolean include(Entry<? extends Model, ? extends Integer> entry) {
int ei = entry.getIdentifier();
return (target*itemsPerPage<=ei && ei<target*itemsPerPage+itemsPerPage);
}
};
}
public static void main(String[] args) {
JFrame frame = new JFrame("Table");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new GridView());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class RadioButtonUI extends BasicRadioButtonUI {
public Icon getDefaultIcon() {
return null;
}
private static Dimension size = new Dimension();
private static Rectangle rec1 = new Rectangle();
private static Rectangle rec2 = new Rectangle();
private static Rectangle rec3 = new Rectangle();

public synchronized void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
Font f = c.getFont();
g.setFont(f);
FontMetrics fm = c.getFontMetrics(f);

Insets i = c.getInsets();
size = b.getSize(size);
rec1.x = i.left;
rec1.y = i.top;
rec1.width = size.width - (i.right + rec1.x);
rec1.height = size.height - (i.bottom + rec1.y);
rec2.x = rec2.y = rec2.width = rec2.height = 0;
rec3.x = rec3.y = rec3.width = rec3.height = 0;

String text = SwingUtilities.layoutCompoundLabel(
c, fm, b.getText(), null,
b.getVerticalAlignment(), b.getHorizontalAlignment(),
b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
rec1, rec2, rec3,
0);

if(c.isOpaque()) {
g.setColor(b.getBackground());
g.fillRect(0,0, size.width, size.height);
}
if(text==null) return;
g.setColor(b.getForeground());
if(!model.isSelected() && !model.isPressed() && !model.isArmed()
&& b.isRolloverEnabled() && model.isRollover()) {
g.drawLine(rec1.x, rec1.y+rec1.height,
rec1.x+rec1.width, rec1.y+rec1.height);
}
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if(v!=null) {
v.paint(g, rec3);
}else{
paintText(g, b, rec3, text);
}
}
}
class Model extends DefaultTableModel {
Model(){
JTable table = new JTable(this);
addColumn("Name");
addColumn("Address");
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root";, "root");
String query = "select * from data";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);

while(rs.next()){
String name=rs.getString("name");
String address=rs.getString("address");
addRow(new Object[] { name,address });
}
}
catch(Exception e){}
}
}

Thanks


Related Tutorials/Questions & Answers:
grid view in java - Java Beginners
grid view in java  Is it possible to create dynamic grid view to display data from database. if yes. how ??   Hi Friend, Yes, you can. Try the following: import java.awt.*; import java.sql.*; import
java + grid - Java3D
java + grid  i need to give the output of the application in the form of a grid in XY-plane,the grid should have different colors for regions of different value range
Advertisements
Java with Grid - Ajax
Java with Grid  please check this URL http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/nestedviewtemplate/defaultcs.aspx grid on above link is made in dotnet. i want to make [b]same UI[/b] as in Java. which
Grid Problem - Java Beginners
Grid Problem  Hi Deepak Plz take seriously I m telling u detail. Steps: 1:- there r one form,this form belong to grid. and two... belong to different table.but in main form,all data going in the grid(with join
count asterisk in a grid - Java Beginners
count asterisk in a grid  I need to write a program that uses a recursive method to count the number of asterisk in a square grid
tree view - Java Beginners
tree view  I want to use tree view in my example code to display data... visit the following links: http://www.roseindia.net/java/example/java/swing/TreeComponent.shtml http://www.roseindia.net/java/example/java/swing
tree view - Java Beginners
time regarding java. I have another problem, Actually I want a tree view in my application which will display the files...   Hi Friend, You are using jsp or java swing. Please clarify this. Do
Create Layout Components in a Grid in Java
Create Layout Components in a Grid in Java   ... layout components with the help of grid in Java Swing. The grid layout provides the facility to arrange some created GUI components for the frame. The grid
tree view - Java Beginners
tree view   I m using swing for the treeview in my application and also the database(Hsqldb). I want to connect the tree view with the database fields. If u want more clarification ,let me know. Thanks
hit and view java
hit and view java  Q:hit and view servlet   //hit...;/editor-fold> } view /* * To change this template, choose Tools...; /** * * @author ignite178 */ public class view extends HttpServlet
view
view  what is the use view in database
Tree Grid using JSF - Java Server Faces Questions
Tree Grid using JSF  Hi All, I am using Trinidad TLD for JSF. I have implemented simple table, but having problem with Tree grid using same. I have read all document on Trinidad site for Tree grid, but they didn't mention
Tree View with database - Java Beginners
Tree View with database   Hi, I'm working with Swing.I have to construct a SplitPane with JTree on the leftside and its details which is stored in the database on the right side of the pane. For ex: Network | |Node1
Ajax grid
Ajax grid  Hi, How I can create ajax grid in my program? Thanks
drawing rectangle in java/Creating a draw rectangle filled with black color function in Java for a grid
drawing rectangle in java/Creating a draw rectangle filled with black color function in Java for a grid  drawing rectangle in java/Creating a draw rectangle (filled with black color) function in Java for a grid
Setting Grid Line in JTable
JTable that contains predefined grid line with black color. But in this Java programming tutorial, you will learn how to set the colored grid line in JTable... Setting Grid Line in JTable     
View source code of a html page using java ..
View source code of a html page using java ..  I could find the html source code of a web page using the following program, http://download.oracle.com/javase/1.4.2/docs/api/java/net/URLConnection.html i could get the html code
HTML GRID
HTML GRID  how to retrieve data from mysql database in grid form using html and servlets? urgent....   import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class
Jsp Grid
Jsp Grid  <p>&lt;%@ page language="java" import="java.util.<em>,saar.etisalat.dto.</em>,saar.etisalat.*" pageEncoding="ISO-8859-1"%> &lt;%@ page contentType="text/html"%> &lt;%@ page import
need view for jsf - Java Server Faces Questions
need view for jsf  Hi, iam using eclipse 3.1 and jsf 1.1 in my application,while creating only we can see the view of jsf page ,it will appear at left corner of eclipse,this is i saw in somewhere now i need this one please tell
Nitobi Grid is not working in IE10/IE11
Nitobi Grid is not working in IE10/IE11  Nitobi grid is working in IE8, but not working in IE10/IE11. we are using above two javascript files to open the grid
Nitobi Grid V3
Nitobi Grid V3      ...) with Ajax-powered Nitobi Grid V3. With minimal coding, Grid delivers responsive...; in-place cell editing. Available for ASP.NET, JAVA, PHP, Classic ASP
Sigma grid -- Ajax-enabled JavaScript grid
Sigma grid -- Ajax-enabled JavaScript grid       Sigma Grid Sigma Grid is a freely open source AJAX editable data grid, written in pure javascript for displaying and inline
Java Model View Controller (MVC) Design Pattern
; margin-left: 400px; } .style4 { margin-left: 160px; } Java MVC ( Model View Controller ) Design Pattern Model View controller is a classical design... logic and view who represents data. MVC design pattern isolates
How to export grid into excel
How to export grid into excel  Hi, i created a grid panel i have to export it to the excel. please help me by some sample code. thanks in advance. cool day dude
GRID IN JSP - JSP-Servlet
GRID IN JSP  I m creating one ERP project in which i need to fill the data from the data base in a grid in jsp page. So pls any one provide the code
data grid - JSP-Servlet
data grid  how can we implement data grid (we have data grid in asp.net) in jsp. can u please send me the sample code and also the possible ways to implement data grid in jsp Thanks varun kumar  Hi friend, I
Ask make graph from i report and view to java Language Programming
Ask make graph from i report and view to java Language Programming  Dear sir, Please help me, i want to make a graph from mysql to i report and view to java. Please help me, give me example source code please. thank you
give me a grid example in php
give me a grid example in php  give me easy code example of grid in php like mysql editor
add row in grid using dojo
add row in grid using dojo  add row in grid using dojo
Grid rows delete issue
Grid rows delete issue  I have a grid having rows with Remove button to remove rows,But we also havae an facility to remove those rows on onchange event of dropdown also,so if we delete middle row and then remove all rows
 

 

 

DMCA.com