Results 1 to 6 of 6
  1. #1
    Lanuk is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Swing, Cant figure out how to add background image, label problem

    Hey guys, im kind of new to java and was wondering how to do a few things. A. I wanted to figure out how to set the background of one of my panels as a picture I have in my src folder. More importantly, I wanted to fix lines 98-107. That is supposed to get the text put in the text box attached to my panelD, and add it to a label, and then add that label to the panel. Currently, nothing happens when I enter text in the textbox. Sorry for the really long code, id really appreciate it if anyone could help. Thanks.

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.border.Border;
    import javax.swing.border.TitledBorder;
    
    
    public class LibraryThing extends JFrame implements MouseListener{
    
    	static ImageIcon imageIcon = createImageIcon("bc.jpg");
       // Image image = imageIcon.getImage();
    	
    	//public GridPanel(){
    		//setLayout(new GridLayout (2,5));
    	//}
    	
    	//creating frames, panels, button, and labels
    
    	private static JFrame main = new JFrame("Library");
    	
    	private static JPanel primary = new JPanel();
    	private static JPanel panelm = new JPanel();
    	private static JPanel panelA = new JPanel();
    	private static JPanel panelB = new JPanel();
    	private static JPanel panelC = new JPanel();
    	private static JPanel panelD = new JPanel();
    	
    	
    //	private static JButton bCheckIn = new JButton("Check In Book");
    //	private static JButton bCheckOut = new JButton("Check Out Book");
    	
    	private static JLabel lblSearch = new JLabel("Search for a Book");
    	private static JTextField txtSearch = new JTextField(10);
    	
    	private static JLabel lblBookName = new JLabel("Book Name: ");
    //	private static JTextField txtBookName = new JTextField(10);
    	private static JTextField txtBookNameC = new JTextField(10);
    	
    	private static JLabel lblName = new JLabel("Enter Name: ");
    	private static JLabel lblAddress = new JLabel("Enter Address: ");
    	private static JLabel lblCardNum = new JLabel("Enter Card No.: ");
    	private static JTextField txtName = new JTextField(10);
    	private static JTextField txtAddress = new JTextField(10);
    	private static JTextField txtCardNum = new JTextField(10);
    	
    	private static JLabel lblIntrob = new JLabel("");
    	private static JLabel lblIntro = new JLabel("Welcome to the library.",JLabel.CENTER);
    	private static JLabel lblIntro2 = new JLabel("Please use the dropdown menus",JLabel.CENTER);
    	private static JLabel lblIntroa = new JLabel("to navigate the website.",JLabel.CENTER);
    	private static JLabel lblIntro3 = new JLabel("Thanks!",JLabel.CENTER);
    	
    	private static JMenuBar mB = new JMenuBar();
    	private static JMenu pat = new JMenu("Patron");
    	private static JMenu l = new JMenu("Books");
    	//private static JMenu c = new JMenu("Check in/Check out");
    	private static JMenuItem register,logout,search,list,inout;
    	
    	private static JTextField txtCheckIn = new JTextField(10);
    	private static JTextField txtCheckOut = new JTextField(10);
    	private static JLabel lblCheckIn = new JLabel("Check In: ");
    	private static JLabel lblCheckOut = new JLabel("Check Out: ");
    	private static JLabel label = new JLabel();	
    	private static JLabel lblimage = new JLabel("",imageIcon,JLabel.CENTER);
    	
    	//use TextArea for multiple lines of output. Can be used with toString method.
    	private static JTextArea output = new JTextArea("",5,25);
    	
    	public static void main (String[] args){
    		setUp();
    	}
    	private static ImageIcon createImageIcon(String string) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	private class Listener implements ActionListener{
    		Account acct;
    		
    		public Listener(Account c){
    			acct = c;
    		}
    		
    		public void actionPerformed(ActionEvent e){
    			if(e.getSource()==txtName){
    				label.setText(txtName.getText()+"");
    				panelD.add(label);
    			}
    			panelD.repaint();
    			validate ();
    		}
    
    	}
    	
    	public static void setUp(){
    	
    		LibraryThing one = new LibraryThing();
    		
    		Font f = new Font("Monospaced", Font.BOLD, 16);
    		main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		main.setPreferredSize(new Dimension(400,400));
    		
    		register = new JMenuItem("Register");
    		logout = new JMenuItem("Logout");
    		search = new JMenuItem("Book Search");
    		list = new JMenuItem("Book List");
    		inout = new JMenuItem("Check In/Out");
    		
    		panelD.setLayout(new GridLayout (6,1));
    		panelA.setLayout(new GridLayout (6,1));
    		panelB.setLayout(new GridLayout (4,1));
    		panelC.setLayout(new GridLayout (2,1));
    		
    		Border tb = BorderFactory.createTitledBorder("Register");
    		((TitledBorder) tb).setTitleJustification (TitledBorder.LEFT);
    		panelD.setBorder(tb);
    		
    		Border tb2 = BorderFactory.createTitledBorder("Check In/Out");
    		((TitledBorder) tb2).setTitleJustification (TitledBorder.LEFT);
    		panelB.setBorder(tb2);
    		
    		Border tb3 = BorderFactory.createTitledBorder("Book Search");
    		((TitledBorder) tb3).setTitleJustification (TitledBorder.LEFT);
    		panelC.setBorder(tb3);
    		
    		panelm.setLayout(new GridLayout (6,1));
    		
    		pat.add(register);
    		pat.add(logout);
    		register.addMouseListener(one);
    		logout.addMouseListener(one);
    		
    		l.add(search);
    		l.add(list);
    		search.addMouseListener(one);
    		list.addMouseListener(one);
    		
    		l.add(inout);
    		inout.addMouseListener(one);
    		
    		mB.add(pat);
    		mB.add(l);
    		mB.add(l);
    		
    		main.setJMenuBar(mB);
    		
    		panelA.setFont(f);
    		main.setPreferredSize(new Dimension(300,200));
    		
    			lblimage.setIcon(imageIcon);
    		
    			lblIntro.setForeground(Color.BLACK);
    			lblIntro.setFont(f);
    			
    			lblIntro2.setForeground(Color.BLACK);
    			lblIntro2.setFont(f);
    			
    			lblIntro3.setForeground(Color.BLACK);
    			lblIntro3.setFont(f);
    			
    			lblIntroa.setForeground(Color.BLACK);
    			lblIntroa.setFont(f);
    			
    			lblSearch.setForeground(Color.BLACK);
    			lblSearch.setFont(f);
    			
    			lblName.setForeground(Color.BLACK);
    			lblName.setFont(f);
    			
    			lblAddress.setForeground(Color.BLACK);
    			lblAddress.setFont(f);
    			
    			lblCardNum.setForeground(Color.BLACK);
    			lblCardNum.setFont(f);
    			
    			lblBookName.setForeground(Color.BLACK);
    			lblBookName.setFont(f);
    			
    			lblCheckIn.setForeground(Color.black);
    			lblCheckIn.setFont(f);
    			
    			lblCheckOut.setForeground(Color.black);
    			lblCheckOut.setFont(f);
    			
    			txtSearch.setForeground(Color.black);
    			txtSearch.setFont(f);
    			
    			txtCheckIn.setForeground(Color.black);
    			txtCheckIn.setFont(f);
    			
    			txtCheckOut.setForeground(Color.black);
    			txtCheckOut.setFont(f);
    			
    			txtBookNameC.setForeground(Color.black);
    			txtBookNameC.setFont(f);
    			
    			output.setFont(f);
    			output.setBackground(new Color(0,255,0));
    			output.setForeground(Color.white);
    			output.setFont(f);
    			
    			panelB.add(lblIntrob);
    			panelB.add(lblCheckIn);
    			panelB.add(txtCheckIn);
    			panelB.add(lblCheckOut);
    			panelB.add(txtCheckOut);
    			
    			panelC.add(lblBookName);
    			panelC.add(txtBookNameC);
    
    			panelD.add(lblIntrob);
    			panelD.add(lblName);
    			panelD.add(txtName);
    			panelD.add(lblAddress);
    			panelD.add(txtAddress);
    			panelD.add(lblCardNum);
    			panelD.add(txtCardNum);
    			
    			panelm.add(lblIntrob);
    			panelm.add(lblIntro);
    			panelm.add(lblIntro2);
    			panelm.add(lblIntroa);
    			panelm.add(lblIntro3);
    			
    			primary.add(panelm);
    			primary.add(panelA);
    			primary.add(panelB);
    			primary.add(panelC);
    			primary.add(panelD);
    
    			main.getContentPane().add(primary);
    			
    			panelA.setVisible(false);
    			panelB.setVisible(false);
    			panelC.setVisible(false);
    			panelD.setVisible(false);
    			panelm.setVisible(true);
    		
    			main.pack();
    			main.setVisible(true);
    			
    	}//setUp
    	
    	@Override
    	public void mouseClicked(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		
    	}
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mousePressed(MouseEvent e) {
    		
    	}
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		if(e.getSource() == logout){
    			panelA.setVisible(false);
    			panelB.setVisible(false);
    			panelC.setVisible(false);
    			panelm.setVisible(true);
    			panelD.setVisible(false);
    		}	
    		if(e.getSource() == register){
    			panelA.setVisible(false);
    			panelB.setVisible(false);
    			panelC.setVisible(false);
    			panelm.setVisible(false);
    			panelD.setVisible(true);
    		}	
    		if(e.getSource() == inout){
    			panelA.setVisible(false);
    			panelB.setVisible(true);
    			panelC.setVisible(false);
    			panelm.setVisible(false);
    			panelD.setVisible(false);
    		}	
    		if(e.getSource() == search){
    			panelA.setVisible(false);
    			panelB.setVisible(false);
    			panelC.setVisible(true);
    			panelm.setVisible(false);
    			panelD.setVisible(false);
    		}	
    		if(e.getSource() == list){
    			panelA.setVisible(true);
    			panelB.setVisible(false);
    			panelC.setVisible(false);
    			panelm.setVisible(false);
    			panelD.setVisible(false);
    		}
    	}
    }

  2. #2
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Swing, Cant figure out how to add background image, label problem

    I think you've forgot to use the addActionListener method on txtName. Thats why nothing is happening.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Swing, Cant figure out how to add background image, label problem

    Also, don't use MouseListeners for menu items, use ActionListeners. Your MouseListeners will still be active, even if the menu item has been disabled which is inappropriate behavior. If you use an ActionListener, the listener has been built to work with menus and buttons, and is smart enough to know not to respond if the menu or button is inactive.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Swing, Cant figure out how to add background image, label problem

    Moved here from 'New to Java'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Lanuk is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: Swing, Cant figure out how to add background image, label problem

    Ohh, thanks... just one problem... I completely forgot how to do that XD

    I tried this:

    Java Code:
    public void actionPerformed(ActionEvent e){
    			if(e.getSource()==txtName){
    				txtName.addActionListener();
    				label.setText(txtName.getText()+"");
    				panelD.add(label);
    			}
    			panelD.repaint();
    			validate ();
    		}
    And it gives me a red line under addActionListener. Any idea on what I should change?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Having a problem with a scroll game background image
    By footballdude2k3 in forum Java 2D
    Replies: 3
    Last Post: 09-16-2011, 05:57 PM
  2. Swing : Background image
    By Java Unknown in forum New To Java
    Replies: 1
    Last Post: 02-24-2011, 09:52 AM
  3. Problem with zooming background image
    By LianaN in forum AWT / Swing
    Replies: 0
    Last Post: 01-28-2011, 09:50 PM
  4. Replies: 6
    Last Post: 07-21-2010, 06:15 AM
  5. Timer in Swing app - refreshing label
    By qoqosz in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 02:17 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •