Home Answers Viewqa Java-Beginners grid view in java

 
 


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

Is it possible to create dynamic grid view to display data from database.
if yes. how ??
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 Pages:
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
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
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
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
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
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
View jsp
View jsp  <%@ page language="java" contentType="text/html; charset...; charset=ISO-8859-1"> <title>View ur Details</title> </head>...;%=request.getContextPath()%>/Controller"> <input type="hidden" name="page" value="view
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     
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
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
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
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
dojo grid from 3 different sources
dojo grid from 3 different sources  I'm new with dojo. I need to get... a grid) and show friends (name+img) of the logged in user with the title.... On the server-side I use RESTful Web Services (Java, Json). Do you have any
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
Grid Layout Container in Flex4
Grid Layout Container in Flex4: The Grid Layout Container is a MX container... in a row and each cell can have a Grid container. The default Horizontal and Vertical alignment of cell is left, Top. You can use three things for creating a Grid
pre view of image uploaded
pre view of image uploaded  I am uploading a file through input type="file" in html form. I want to display preview of that uploaded image in the same form using java script only.. and I want to insert that image into oracle
pre view of image uploaded
pre view of image uploaded  I am uploading a file through in html form. I want to display preview of that uploaded image in the same form using java script only.. and I want to insert that image into oracle database I don't want
pre view of image uploaded
pre view of image uploaded  I am uploading a file through input in html form. I want to display preview of that uploaded image in the same form using java script only.. and I want to insert that image into oracle database I
view
view  what is the use view in database
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
view data from jTextArea to jtable
view data from jTextArea to jtable  good night Please help senior java all, I want to make a brief program of reading data in the text area... this: I have a sentence like this **Java is a programming language that uses
JSP: View Session
JSP: View Session          The view consists of the Html page or the jsp components that provide the user interface. View is what we see. It is mainly
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
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
Chapter 5. Client View of an Entity
client view can also be mapped to non-Java client environments... Chapter 5. Client View of an EntityPrev Part I. Exam Objectives Next     Chapter 5. Client View
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
Model-View-Controller (MVC) Structure
Java NotesModel-View-Controller (MVC) Structure Previous - Presentation-Model Here the same calculator is organized according the the Model-View... (the Presentation in the previous example) into a View (creates the display,interacting
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
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
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
How to remove the space between a JComponent and its Grid border while using GridBagLayout?
How to remove the space between a JComponent and its Grid border while using GridBagLayout?   Hi, I had try to design a UI using GridBagLayout.I...("Java"), c); c.gridx = 1; c.ipadx = 0; c.ipady = 0; p.add(new
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
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
add row in grid using dojo
add row in grid using dojo  add row in grid using dojo
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
Chapter 2. Client View of a Session Bean
Chapter 2. Client View of a Session BeanPrev ...;Client View of a Session BeanIdentify correct and incorrect statements or examples about the client view of a session bean's local and remote home
java swing
java swing  how to connect database with in grid view in java swing   Hi Friend, Please visit the following link: Grid view in java swing Thanks
Creates a view of  byte buffer as a int buffer.
Creates a view of  byte buffer as a int buffer.  In this tutorial, we will see how to creates a view of byte buffer as a int buffer. ByteBuffer... asIntBuffer() The asIntBuffer() method creates a view of byte buffer
Creates a view of  byte buffer as a long buffer.
Creates a view of  byte buffer as a long buffer.  In this tutorial, we will see how to create a view of byte buffer as a long buffer... asLongBuffer() The asLongBuffer() method creates a view of byte buffer
Creates a view of byte buffer as a char buffer.
Creates a view of byte buffer as a char buffer.  In this tutorial, we will see how to creates a view of this byte buffer as a char buffer... asCharBuffer() The asCharBuffer() method creates a view of byte buffer
Creates a view of this byte buffer as a double buffer.
Creates a view of this byte buffer as a double buffer.  In this tutorial, we will see how to creates a view of byte buffer as a double buffer... Doublebuffer asDoubleBuffer() The asDoubleBuffer() method creates a view
Creates a view of  byte buffer as a float buffer.
Creates a view of  byte buffer as a float buffer.  In this tutorial, we will see how to creates a view of byte buffer as a float buffer... FloatBuffer asFloatBuffer() The asFloatBuffer() method creates a view of byte
editable grid in jsp
editable grid in jsp  i want to create editable grid in jsp for invoice so that i can insert multiple product at a same time.how to create that in jsap, plz reply   We are providing you the code where we have specified

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.