SasseMan 49 Junior Poster

What I know there is no super easy way to write your hashtable to a database, since you want to store a java object. The most common tool/framework to do stuff like this is hibernate which lets you define java objects to database mapping and then you can simply retrieve and store object in the database without having to handle sql to much.

You basically make a hibernate mapping file, an xml file which stores information on how an object is stored in the database and its relations to other objects etc. Then you have the corresponding java Object Prod. You can then use hibernate like the following...

Get a session
start a database transaction
//do work
commit transaction

where work can be for example.

Query q = session.createQuery("from Prod");
List<Prod> prods = q.list();

or create a new Prod and persist it...

Prod p = new Prod( input... );
session.save(p);

This way you don't have to handle sql inserts and getting out info from a db table and storing it to an object, hibernate does that for you.

If you use netbeans for example for your project there are built in tools that helps you create everything you need for hibernate to work. There are wizards that can access your DB, create suitable mapping and java Object files, and then you just use it. But you'll need to read a little on the basics of hibernate.

SasseMan 49 Junior Poster

Parsing the text with space and dot delimiters isn't enought, java is more complex than that. You should maybe check out recursive descent. There might be easier ways to do it but I would define the java grammar and write a recursive descent parser for it. Check out the link and google it, I believe it will be useful.

http://en.wikipedia.org/wiki/Recursive_descent_parser

SasseMan 49 Junior Poster

the program I'm writing is for embedded systems with touchscreens so you really want as little scrolling as possible, there are some scrollpanels but with a custom implementation where you have big scrollbuttons and no scrollbar, and you want the content to be really predictable, thats the history of my problem really. It is more important for the middle panel to have a large size rather then the panel to the left of it showing for example the entire labels that are in it, and most important you don't want the panels sizes to change while they get filled with content. So my solution was to set minimum and preferred sizes for the two panels that do not have to be large or even show its entire contents and then set their weights to 0, but you still want everything to take up as much space as possible at the same time so things are easy to click on with you finger etc.

SasseMan 49 Junior Poster

Yes of course your totally right. But try placing a giant jlabel in redpanel and it will take up more space, just as you said. I think the problem is solved, Thanks you for your help, hope to meet again!

cheers!

SasseMan 49 Junior Poster

hehe "fit the bill", you make me sound jerky. Well yes I did and the problem is the same, the panels will still take up the minimum amount of space that they need, maybe I could at initialization of all components set their minimum sizes to 0 or something small, but thats a pain in the ass. The layout will only try to divide space even amongst grids. I guess it's a limitation of gridbaglayout.

SasseMan 49 Junior Poster

I don't see the problem, the code works fine for me.

SasseMan 49 Junior Poster

I aswell as a professional pro can verify that the answer you are looking for is made very avaliable in the first post that was posted by "BestJewSinceJC". And I did not find his or anyones comments patronizing in any way. You would normaly hear infinitely more patronizing stuff coming out of programmers mouths. This was all love and candies.

SasseMan 49 Junior Poster

use code tags so your code is more readable [*code*][*/code*]

without the stars

And could you tell a little more about the problem, i'm not sure if I get what youre trying to do.

SasseMan 49 Junior Poster

Oh i see that i forgot column 8, not in my code thought. But having gridbaglayout with forced gridsizes wouldn't be the same things as a gridlayout. In a gridlayout you can only place a component in one grid. I was thinking a gridlayout where you can choose to make a component span several grids, but still forcing the grids to be of equal size no matter what the component thinks of it's size, that would be some kind of a combination of gridlayout and gridbaglayout.

I set panel 1s weight to 1 because i thought that all the grids would then have to share an equal amount of space in x-direction. Panel 2 takes up 8 grids in x-direction and the weight is also 1 so i assumed that all those grids would have to be as big as the first grid, but it apparently doesn't work that way.

SasseMan 49 Junior Poster

yes I know that the weight are percentages. But as you can see panel 2 has a gridwidthx of 8, and I guess i thought that panel2 would take up 8 grids and the rest would take up 1 grid each. But that didnt work.

For now I just set weightx for panel 1 and 3 to zero and then I set the minimum and preffered sizes of those panels to something that works, so panels 1 and 3 will always take up a certain amount of pixels while panel 2 will resize.

What I would like thought is a gridbaglayout that forces the gridsizes on its content just like gridlayout. The question is why it isn't there, GridBagLayout.forceGridSize(boolean b)?

SasseMan 49 Junior Poster

Hi! I'm having some problems with gridbaglayout trying to get functionality that maybe isn't even there.

The problem is the following

I have 3 Jpanels with differend contents. I want panel 1 to be displayed to the left of the screen filling the y-axis, panel 2 in the middle filling y and panel 3 to the right.

I Want panel 1 to take 1/10 of space in x-direction, panel 2 8/10 and panel 3 1/10 to. So i made these three constraints.

GridBagConstraints c1 = new GridBagConstraints();
        c1.fill = c1.BOTH;
        c1.gridx = 0;
        c1.gridy = 0;
        c1.weightx = 1;
        c1.weighty = 1;
        c1.gridwidth = 1;
        this.add( panel1, c1 );

        GridBagConstraints c2 = new GridBagConstraints();
        c2.fill = c2.BOTH;
        c2.gridx = 1;
        c2.gridy = 0;
        c2.weightx = 1;
        c2.weighty = 1;
        c2.gridwidth = 8;
        this.add( panel2, c2 );

        GridBagConstraints c3 = new GridBagConstraints();
        c3.fill = c3.BOTH;
        c3.gridx = 10;
        c3.gridy = 0;
        c3.weightx = 1;
        c3.weighty = 1;
        c3.gridwidth = 1;
        this.add(panel3, c3 );

Now the problem is that the panels doesn't take up the space i want. Panel1 gets way to big, and then shrink when panel2 is filled with content.

So the question is if there is a way to make gridbaglayout force all grids to be of equal size like gridlayout or should I use another layoutManager. In that case, Which one?

SasseMan 49 Junior Poster

Okay it seems like a components can only have one parent, so thats that, solved it in another way.

Thank you

youre welcome

SasseMan 49 Junior Poster

Hi, I have really weird layout problem.

I have a class VerticalGridPanel, which uses GridBagLayout to place components top to bottom, left to right, nothing special.

Then I have a List of components, which i want to add a part of to a VerticalGridPanel. I then want to add another part of the same list to another VerticalGridPanel, the VerticalGridPanels stores many of the same components from the list.

Now the problem is that if I add Components 0-10 from the list to a VerticalGridPanel and then add Component 5-15 to another VerticalGridPanel, then components 5-10 dissapeares from the first VerticalGridPanel. I of course want the panels to show the duplicates as I would expect.

I also noted that when i call getComponentCount on the first panel directly after populating it I get the right amount of components. But when I add components to the second panel the first panel loses exactly as many components as there were duplicates in the second panel.

This is to me very strange behaviour, and I cant seem find out what the problem is. Are you not supposed to add the same components to different panels?

The code is pretty simple and i dont think it would do much to post it, but just write if you feel it's needed.

SasseMan 49 Junior Poster

Hi, Im writing a java app that uses hibernate for DB stuff. The thing is that it takes some time to start hibernate, and I would like to have a progress bar to show the loading procedure, and maybe print what hibernate is outputting

public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Here is where the loading takes place, more precisely the line...

sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

Does anyone have an idea of how to make a progress bar for this in java?

SasseMan 49 Junior Poster

GMP has a c++ wrapper, you could use that if you really want to code in c++ style.

Salem commented: Exactly! It's like they never bothered to read more than 5 words... +19
SasseMan 49 Junior Poster

Yes i thought that to, but the behaviour is totally different in Windows. Well well, the problem is solved. Thanks for you help!

SasseMan 49 Junior Poster

You need to store the output from >> somewhere before passing it to the setter methods, your getter methods should be const and line 153 makes no sense

CSCI208Class[N]=TempStudent;

, should probably be indexed

CSCI208Class[N]=TempStudent[N];

I took the liberty of correcting your code, just corrected the compile errors, I have no idea if it will run succesfully. Here it is...

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//************************************
const int NAMESIZE=11;
const int FILENAMESIZE=51;
const int ARRAYSIZE=20;
int N=0;
typedef char Name_t[NAMESIZE];
typedef int Grade_t;
typedef char Filename_t[FILENAMESIZE];
typedef fstream Datafile_t;
typedef fstream Outfile_t;
//*************************************
class cStudent
{
        private:
                Name_t fname;
                Name_t lname;
                Grade_t t1;
                Grade_t t2;
                Grade_t t3;
                Grade_t t4;
                Grade_t assigngrade;
                Grade_t examgrade;
        public:
                cStudent(){}//default constructor
                cStudent(Name_t NewName, Grade_t NewGrade);
                
                cStudent & operator=( const cStudent student ) {
                	student.get_fname( fname );
                	student.get_lname( lname );
                	student.get_t1(t1);
                	student.get_t2(t2);
                	student.get_t3(t3);
                	student.get_t4(t4);
                	student.get_assigngrade( assigngrade );
                	student.get_examgrade( examgrade );
                }
                
                void Read(Datafile_t &Datafile);
                void Print(Outfile_t &Outfile);
                //set
                void set_fname(Name_t first)
                {       strcpy(fname,first);}
                void set_lname(Name_t last)
                {       strcpy(lname,last);}
                void set_t1(Grade_t x)
                {       t1=x;}
                void set_t2(Grade_t x)
                {       t2=x;}
                void set_t3(Grade_t x)
                {       t3=x;}
                void set_t4(Grade_t x)
                {       t4=x;}
                void set_assigngrade(Grade_t x)
                {       assigngrade=x;}
                void set_examgrade(Grade_t x)
                {       examgrade=x;}
                //end set
                //get
                void get_fname(Name_t &fname) const
                {       strcpy(fname,this->fname);}
                void get_lname(Name_t &lname) const
                {       strcpy(lname,this->lname);}
                int get_t1(Grade_t &t1) const
                {return t1;}
                int get_t2(Grade_t &t2) const
                {return t2;}
                int get_t3(Grade_t &t3) const
                {return t3;}
                int get_t4(Grade_t &t4) const
                {return t4;}
                int get_assigngrade(Grade_t &assigngrade) const
                {return assigngrade;}
                int get_examgrade(Grade_t &examgrade) const
                {return assigngrade;} …
SasseMan 49 Junior Poster

Yea i thought that to, but it doesn't work in windows for some reason. super.paintComponent must be doing something more than calling paintComponents in windows.

SasseMan 49 Junior Poster

my paint method looks like this now

public void paint( Graphics g ) {
        autoResizeColWidth( billTable, billTableModel );
        super.paintComponent(g);
}

this works, so does this.paintComponents(g), instead of super.paintComponents(g).

SasseMan 49 Junior Poster

It just resizes table columns to its contents, looks like this...

public JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setModel(model);

        int margin = 5;

        for (int i = 0; i < table.getColumnCount(); i++) {
            int                     vColIndex = i;
            DefaultTableColumnModel colModel  = (DefaultTableColumnModel) table.getColumnModel();
            TableColumn             col       = colModel.getColumn(vColIndex);
            int                     width     = 0;

            // Get width of column header
            TableCellRenderer renderer = col.getHeaderRenderer();

            if (renderer == null) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }

            Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);

            width = comp.getPreferredSize().width;
            
            // Get maximum width of column data
            for (int r = 0; r < table.getRowCount(); r++) {
                renderer = table.getCellRenderer(r, vColIndex);
                comp     = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                        r, vColIndex);
                width = Math.max( width, comp.getPreferredSize().width );
            }

            // Add margin
            width += 2 * margin;

            // Set the width
            col.setPreferredWidth(width);

        }

        DefaultTableColumnModel colModel  = (DefaultTableColumnModel) table.getColumnModel();
        int w0 = colModel.getColumn(0).getPreferredWidth();
        int w2 = colModel.getColumn(2).getPreferredWidth();
        colModel.getColumn(1).setPreferredWidth( scroll.getViewport().getExtentSize().width -w0 -w2 -4 );

        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(
            SwingConstants.LEFT);

        // table.setAutoCreateRowSorter(true);
        table.getTableHeader().setReorderingAllowed(false);

        return table;
    }

}
SasseMan 49 Junior Poster

Oh I didn't call supers paintcomponents. But the idea was to override paint so I could call autoResizeColWidth.

Should I maybe only call super.paintComponents() instead and autoResizeColWidth? In reverse order that is.

SasseMan 49 Junior Poster

there are a few things that are pretty wrong here.

From line 270-298 you write TempStudent::something.

TempStudent is an array so you should use [] to access some element. I seems like you would want to write TempStudent[N]::something

And Im not sure if :: will work either. You have setters for those variables so why dont you ise them instead? It is private access to those members so you should use setters.

you could also make all members public and write Datafile>>TempStudent[index].member

Secondly you haven't defined an operator= for cStudent. Should look something like this...

cStudent & operator=( const cStudent & );

and ofcourse define it to.

SasseMan 49 Junior Poster

Ok! Problem solved, It works if i call paintChildren istead of paintComponents, I guess i should have tried for myself just a bit longer.

Do you have an explanation to why painComponents didn't work?

SasseMan 49 Junior Poster

I have isolated the problem.

In one class i override the paint method which looks like this...

public void paint( Graphics g ) {
        autoResizeColWidth( billTable, billTableModel );
        this.paintComponents(g);
    }

When i comment out pain it works fine, but i want to be call autoResizeColWidth every time paint is called. But problem seems to be the paintComponents method. Should I paint child components some other way?

SasseMan 49 Junior Poster

Hi!

I'm writing a java app with a swing gui. The problem is the following. I am developing on a mac, where it runs fine. but when I run it on Windows Posready09 (Windows XP), which is the platform the app is intended for, or Windows7, half of the window doesnt load. It just stays gray, until I start to klick on stuff which makes components appear. Also when I drag the window the graphics get all messed up.

It worked fine on Windows until recently. I have made some minor adjustments and additions since then, but nothing I can put my finger on which would cause this problem.

Does anyone know what this could be caused by? Some windows java gui secrets I'm not aware of?

SasseMan 49 Junior Poster

Hi, I have a problem with two classes who both has instances to each other.

Here are the header files...

#ifndef ENVIRONMENT_H
#define ENVIRONMENT_H

#include <vector>
#include "item.h"
#include "actor.h"

namespace game{
		
	class Environment{
		
	public:
		Environment(){}
		Environment(std::string name_, std::string description_);
		
		enum directions {
			north = 0,
			north_east = 1,
			east = 2,
			south_east = 3,
			south = 4,
			south_west = 5,
			west = 6,
			north_west = 7
		};
		
		void add_item(Item & it); 
		void remove_item(Item & it);
		
		void add_exit(const int position, const Environment & env);
		void remove_exit(const int position);
		
		void add_actor(Actor & act);
		void remove_actor(Actor & act);
		
	protected:		
		std::string name, description;		
		std::vector<Item> items;
		bool existing_exits[8];
		Environment * exits;
		std::vector<Actor> actors;
	};
}

#endif
#ifndef ACTOR_H
#define ACTOR_H

#include <string>
#include "environment.h"

namespace game{
	
	class Actor{
		
		#define max_level 20
	
	protected:
		std::string name, description, type;
		
		int vitality, strength, level, dexterity;
		bool evil;
		Environment current_location;
		
	public:
		const int get_vitality() const{ return vitality; }
		const int get_strength() const{ return strength; }
		const int get_dexterity() const{ return dexterity; }
		const int get_level() const{ return level; }
		const std::string get_name() const{ return name; }
		const std::string get_description() const{ return description; }
		const std::string get_type() const{ return type; }
		const bool is_evil() const{ return evil; }
		
		const bool operator==(const Actor & ref) const;
		
		//const Environment & get_location() const{ return current_location; }
		
		void increase_level(int lev);
		//virtual const bool attack(Actor & other) const = 0;
		//virtual void take_damage(const int damage) = 0;
		
		const float chance_to_hit(const Actor other) const;
	};
	
}

#endif

I get the following errors when …

SasseMan 49 Junior Poster

It sound to me like you are describing bucket sort, or radix sort where you use the entire integer to place in buckets instead of parts of it. It is fast but not very space efficient. Runs practically in O(n) (linear) time.

SasseMan 49 Junior Poster

Hello! I am a student from Stockholm, Sweden studying Computer Science at KTH (Translation, RIT - Royal Institute of technology), and I have about a year left to get my masters degree. My concentration is on Autonomous Systems where it mostly comes down to computer vision (Object detection, computer geometry etc).

I'm very much looking forward to see what this community has to offer and I myself will try to be as helpfull as I can.

Cheers!

SasseMan 49 Junior Poster

Oh i solved it!

I just added operator= as a pure virtual function in Date and that did the trick!

Let me know if anyone has a better solution.

SasseMan 49 Junior Poster

Hi! I am having some problems with using the operator= on an dereferences object. I have the code...

Julian j1(2319,1,7);
Date * dp = new Gregorian(2319,1,23);	
cout << (*dp) << " " << typeid(*dp).name() << endl;	
*dp = j1;	
cout << (*dp) << " " << typeid(*dp).name() << endl;	
j1 = *dp;	
cout << (j1) << " " << typeid(j1).name() << endl;

which gives me the output

2319-01-23 N4lab29GregorianE
2319-01-07 N4lab29GregorianE
operator= Julian
2318-12-22 N4lab26JulianE

The problem is that *dp = j1 does not call the operator= in Gregorian. Instead it seems like a synthesized function is being called.

Here is the operator= definition for Gregorian.

Gregorian & Gregorian::operator= ( Date const & d ) {		
	cout << "operator= Gregorian" << endl;
	Gregorian temp(mod_year, mod_month, mod_day);
	temp += d.mod_julian_day();
	current_year = temp.year();
	current_month = temp.month();
	current_day = temp.day();
	return *this;
}

This is not being called only when I dereference the object and use the assignment operator.

To clarify, Gregorian and Julian inherits from Date which is abstract.

Can it have something to do with that dp is a Date pointer and when assigning to dp the synthesized assignment operator of Date is used instead of Gregorian?