Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java - input text string from database into paintComponent drawString?

Options
  • 28-03-2012 1:00am
    #1
    Registered Users Posts: 2,967 ✭✭✭


    I really hope someone can help me with this, as it's really ruining my week!

    I've an assignment to get finished this week, Java GUI for a hotel management system, and I'm really stuck on this final piece.

    I've an index.java class which starts my app with a large JFrame containing a login.java frame, accepting user names and passwords stored on an M$ Access db. No problem with that, connection and all other queries/inserts/db operations are fine.

    My GUI frame uses a BorderLayout, with a "header.java" panel to the North.
    This header class contains a "public void paintComponent(Graphics g)" method which creates a linear gradient across the text for the Hotels name.
    That works great.

    The main GUI frame contains a JMenu called Hotel, containing JMenuItems for Dublin, Galway, Sligo and New Location (although I haven't the new location bit coded). When one of these items is pressed the actionEvent stores the text in a field called "name" in a table called Location. This works fine, selecting new JMenuItems does change the value in the db field.

    HOWEVER, after much wasted effort I cannot get this value back out into the paintComponent! TBH, I'm not 100% sure this can be done, as all I get back is "null".

    Here's my "header" code:
    import javax.swing.*;
    import java.awt.*;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    
    @SuppressWarnings("serial")
    public class header extends JPanel
    {
    	Statement st;	// Database statement item
    	ResultSet set;
    	String loc;
    	
    public header()
    	{		
    	   setBackground(Color.GRAY);	// Background fill colour.
    	}
    
    public String local()		
    	{
    	   try
    		{	
    		set=st.executeQuery("Select name from location");
    
    		while(set.next())
    			{
    				loc = set.getString("name");
    			}
    	}
    catch (Exception e)
    	{
    		System.out.println("error header location");
    	}
    	return loc;
    
    	}
    		
    public void paintComponent(Graphics g)
    	{
    	super.paintComponent(g);
    	Graphics2D g2d = (Graphics2D) g;
    	g2d.setFont(new Font("Arial", Font.BOLD, 40));
    
    				
    g2d.setPaint(new GradientPaint(240, 50, Color.RED, 600, 50,Color.BLACK));
    				
    g2d.drawString(("Posh Mansions " + loc), 240, 50);
    	}
    		
    
    public Dimension getPreferredSize()
    	{
    		return new Dimension(570, 70);
    	}
    
    }
    

    I want to be able to display "Posh Mansions Sligo / Galway or Dublin";
    but what I get outputted is "Posh Mansions null" ?

    I've been looking at this so long - I'm bewildered and need to go to bed!

    Any help / suggestions would be greatly appreciated - or else it's going to be converted to a simple JLabel!!!!
    :mad:


Comments

  • Registered Users Posts: 5,246 ✭✭✭conor.hogan.2


    May be unwanted advice but if you are forced to use access could you not develop the gui in access too?

    Or is there some extra functionality that you use Java for?


  • Registered Users Posts: 2,967 ✭✭✭mrmac


    I hear you, but it's actually a Java GUI assignment - the use of an Access db is purely my decision, as a way of adding extra functionality, compared to using ArrayLists.


  • Registered Users Posts: 2,021 ✭✭✭Colonel Panic


    When does the method local() get called to fill in the string you want to draw?

    As an aside, you're mixing logic and UI quite badly there! When you get it working, it might be worth looking at moving the database call out of there into some sort of UI controller class.


  • Registered Users Posts: 2,967 ✭✭✭mrmac


    Sorry, when I pasted in the code, it got formatted 4 screens wide!
    Seems my editing deleted my local() call.
    It was called inside the paintComponent - however this doesn't work either.
    UI controller class? I like the sound of that, but haven't heard of it before.
    This is my first semester using Java awt/swing, and while I really like it, most topics are only briefly mentioned at college.

    Thanks for the pointers.


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    while(set.next())
    {
        loc = set.getString("name"); }
    

    should it not be:
    while(set.hasNext())
    {
        loc = set.getString("name"); }
    

    The reason your getting null is because loc is not actually getting any value assigned to it.


  • Advertisement
Advertisement