grid view in java

grid view in java

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 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
ModuleNotFoundError: No module named 'buildbot-grid-view'
ModuleNotFoundError: No module named 'buildbot-grid-view'  Hi, My... named 'buildbot-grid-view' How to remove the ModuleNotFoundError: No module named 'buildbot-grid-view' error? Thanks   Hi, In your
Advertisements
ModuleNotFoundError: No module named 'buildbot-grid-view'
ModuleNotFoundError: No module named 'buildbot-grid-view'  Hi, My... named 'buildbot-grid-view' How to remove the ModuleNotFoundError: No module named 'buildbot-grid-view' error? Thanks   Hi, In your
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
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
view
view  what is the use view in database
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
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 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
Maven dependency for it.unibo.alchemist - alchemist-grid version 25.2.0 is released. Learn to use alchemist-grid version 25.2.0 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 25.2.0 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 07 Dec 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 25.1.1 is released. Learn to use alchemist-grid version 25.1.1 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 25.1.1 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 05 Dec 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 25.1.0 is released. Learn to use alchemist-grid version 25.1.0 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 25.1.0 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 04 Dec 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 25.0.0 is released. Learn to use alchemist-grid version 25.0.0 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 25.0.0 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 03 Dec 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.16 is released. Learn to use alchemist-grid version 24.1.16 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.16 in Java... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 03 Dec 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.15 is released. Learn to use alchemist-grid version 24.1.15 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.15 in Java... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 12 Nov 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.14 is released. Learn to use alchemist-grid version 24.1.14 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.14 in Java... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 03 Nov 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.13 is released. Learn to use alchemist-grid version 24.1.13 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.13 in Java... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 02 Nov 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.12 is released. Learn to use alchemist-grid version 24.1.12 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.12 in Java... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 02 Nov 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.11 is released. Learn to use alchemist-grid version 24.1.11 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.11 in Java... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 14 Oct 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.10 is released. Learn to use alchemist-grid version 24.1.10 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.10 in Java... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 04 Oct 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.9 is released. Learn to use alchemist-grid version 24.1.9 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.9 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 02 Sep 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.8 is released. Learn to use alchemist-grid version 24.1.8 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.8 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 02 Sep 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 25.4.0 is released. Learn to use alchemist-grid version 25.4.0 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 25.4.0 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 24 Dec 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 25.3.0 is released. Learn to use alchemist-grid version 25.3.0 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 25.3.0 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 22 Dec 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 25.2.1 is released. Learn to use alchemist-grid version 25.2.1 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 25.2.1 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 21 Dec 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.7 is released. Learn to use alchemist-grid version 24.1.7 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.7 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 08 Aug 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.6 is released. Learn to use alchemist-grid version 24.1.6 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.6 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 31 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.5 is released. Learn to use alchemist-grid version 24.1.5 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.5 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 30 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.4 is released. Learn to use alchemist-grid version 24.1.4 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.4 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 29 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 21.0.2 is released. Learn to use alchemist-grid version 21.0.2 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 21.0.2 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 14 Jun 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 21.0.1 is released. Learn to use alchemist-grid version 21.0.1 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 21.0.1 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 11 Jun 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 21.0.0 is released. Learn to use alchemist-grid version 21.0.0 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 21.0.0 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 03 Jun 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.1.0 is released. Learn to use alchemist-grid version 24.1.0 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.1.0 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 14 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.0.8 is released. Learn to use alchemist-grid version 24.0.8 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.0.8 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 13 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.0.7 is released. Learn to use alchemist-grid version 24.0.7 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.0.7 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 08 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.0.6 is released. Learn to use alchemist-grid version 24.0.6 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.0.6 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 08 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.0.5 is released. Learn to use alchemist-grid version 24.0.5 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.0.5 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 07 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.0.4 is released. Learn to use alchemist-grid version 24.0.4 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.0.4 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 07 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.0.3 is released. Learn to use alchemist-grid version 24.0.3 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.0.3 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 05 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.0.2 is released. Learn to use alchemist-grid version 24.0.2 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.0.2 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 05 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.0.1 is released. Learn to use alchemist-grid version 24.0.1 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.0.1 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 05 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 24.0.0 is released. Learn to use alchemist-grid version 24.0.0 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 24.0.0 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 04 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 23.0.0 is released. Learn to use alchemist-grid version 23.0.0 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 23.0.0 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 03 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 22.0.0 is released. Learn to use alchemist-grid version 22.0.0 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 22.0.0 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 02 Jul 2022
Maven dependency for it.unibo.alchemist - alchemist-grid version 21.0.8 is released. Learn to use alchemist-grid version 21.0.8 in Maven based Java projects
to use  it.unibo.alchemist - alchemist-grid version 21.0.8 in Java projects... of alchemist-grid released The developers of   it.unibo.alchemist - alchemist-grid project have released the latest version of this library on 29 Jun 2022

Ads