-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: how to display data in gui
PostPosted: Tue Feb 27, 2007 6:04 am 
Newbie

Joined: Mon Feb 19, 2007 6:31 am
Posts: 12
Hi

can someone please show me how to display data retrieved from database in a GUI.

I'm using hibernate to get the data from a PostgreSQL table.

List messages = newSession.createQuery("from NAMES order by name asc").list();
System.out.println( messages.size() +" people found in database" );
System.out.println("");

for ( Iterator iter = messages.iterator();iter.hasNext(); )
{
NAMES loadedMsg = (NAMES) iter.next();
System.out.println( loadedMsg.getId() + " " + loadedMsg.getText() );
}

but how do i display the names in a list box?

Please help.
Thanks
Avesh


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 7:05 am 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
Hi there aveshn.

It seams you dont have much experience with GUI Developing with Java Swing. So i advise to take a look on a Java Swing Tutorial, for instance one of the Sun. here you have a URL:

http://java.sun.com/docs/books/tutorial/uiswing/

any way i leave you here a piece of code to create a JList that have to be added on a JPanel.

public class JListExample extends JFrame {
private JList list;
private JPanel listPanel;
private JScrollPane scroll;
private DefaultListModel listModel;

public JListExample() {
super("GUJ - JList");
listModel = new DefaultListModel();
listPanel = new JPanel();

// adding items to the JList
listModel.addElement("Item 1");
listModel.addElement("Item 2");
listModel.addElement("Item 3");

//configuring the JList with listModel
list = new JList(listModel);
list.setVisibleRowCount(3);

scroll = new JScrollPane(lista);

listPanel.add(scroll);
getContentPane().add(listPanel, BorderLayout.CENTER);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// show the window
pack();
setVisible(true);
}

public static void main(String args[]){
JListExample jListExample = new JListExample();
}
}

Hope this tip was useful. Anyway this is not a GUI forum. You would find more helpfull to use a GUI forum.

See ya later. Dont forget to rate

--ms


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 7:15 am 
Newbie

Joined: Mon Feb 19, 2007 6:31 am
Posts: 12
hi

thanks. i know how to build a gui. what i want to know is how to display the results in the gui from the object LoadedMsg

//here the NAMES class gets the data from messages which is mapped to the database.

for ( Iterator iter = messages.iterator();iter.hasNext(); )
{
NAMES loadedMsg = (NAMES) iter.next();
System.out.println( loadedMsg.getId() + " " + loadedMsg.getText() );
}

instead of using
System.out.println( loadedMsg.getId() + " " + loadedMsg.getText() );
i want is displayed in a list on the gui


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 7:27 am 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
On my reply i showed you how to build a JList and how to add elements to it. you just have to adjust your code so insted of doing:

"listModel.addElement("Item 1");"

doing something like:

"listModel.addElement(loadedMsg.getId() + " " + loadedMsg.getText());

hope this tip was usefull;

--ms


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 8:29 am 
Newbie

Joined: Mon Feb 19, 2007 6:31 am
Posts: 12
hi

ok i tried the JList. but it only displays the first element from the database.
how can i display all?

this my code:

package hello;

import java.util.*;
import org.hibernate.*;

import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class HelloWorld extends JFrame
{
private static final long serialVersionUID = -3507123492363499680L;
private JList list;
private JPanel listPanel;
private JScrollPane scroll;
private DefaultListModel listModel;

public HelloWorld()
{

}

public void launchFrame(String name)
{
listModel = new DefaultListModel();
listPanel = new JPanel();
// adding items to the JList
//System.out.println(name);
listModel.addElement(name);

// configuring the JList with listModel
list = new JList(listModel);
list.setVisibleRowCount(10);
scroll = new JScrollPane(list);
listPanel.add(scroll);
getContentPane().add(listPanel, BorderLayout.CENTER);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// show the window
pack();
setVisible(true);
}
public static void main(String[] args)
{
HelloWorld gui = new HelloWorld();
//First unit of work
System.out.println("Enter the number of users you wish to input");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String number = null;
try
{
number = in.readLine();
}
catch (IOException e) {
e.printStackTrace();
}

for (int k = 0;k<= Integer.parseInt(number.trim())-1;k++)
{
Session firstSession =HibernateUtil.currentSession().getSessionFactory().openSession();
Transaction firstTransaction = firstSession.beginTransaction();
System.out.println("Enter users name");
BufferedReader iname = new BufferedReader(new InputStreamReader(System.in));
String inname = null;
try {
inname = iname.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
NAMES name = new NAMES(inname);
Long msgid = (Long) firstSession.save(name);
firstTransaction.commit();
firstSession.close();
}

Session newSession =HibernateUtil.currentSession().getSessionFactory().openSession();
Transaction newTransaction = newSession.beginTransaction();
List messages = newSession.createQuery("from NAMES order by name asc").list();
int size = messages.size();
System.out.println( messages.size() + " people found in database" );
System.out.println("");

for ( Iterator iter = messages.iterator();iter.hasNext(); )
{
NAMES loadedMsg = (NAMES) iter.next();
gui.launchFrame(loadedMsg.getText());
//System.out.println( loadedMsg.getId() + " " + loadedMsg.getText() );
}

newTransaction.commit();
newSession.close();
HibernateUtil.closeSession();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 8:47 am 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
You just can init the JFrame once:

Or you don't pass at all a element to the JFram intit: launchFrame, or you pass a list and iterate that list. Once this is a simple example is easier the 2nd choise, but i dont advise you to use. I is preferable to initiate the frame and its components and then with a method call populate it

do something like this:


public static void main(String[] args){
......
gui.launchFrame(messages);
.....
}

public void launchFrame(List messages){
.......
for (Iterator iter = messages.iterator();iter.hasNext(); ){
NAMES loadedMsg = (NAMES) iter.next();
listModel.addElement(loadedMsg.getText());
}
......
}

i thinks this should work.
--ms


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 9:04 am 
Newbie

Joined: Mon Feb 19, 2007 6:31 am
Posts: 12
ah thanks for the help. it works now.

Cheers
Avesh

ps. i'm going to rate you now


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.