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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java GUI help

  • 17-03-2004 2:36pm
    #1
    Closed Accounts Posts: 1,152 ✭✭✭


    hey all im trying to align my java GUI but im having a problem with one or two components,

    ill post a snippet of the code i have written:
    
    public Login()
    	{
    		//assigning GUI components
    
    		super("Login: Please enter card number and PIN");
    
    		pinLbl = new JLabel("PIN:");
    		cardNumLbl = new JLabel("Card Number:");
    
    		pinTxt = new JPasswordField(4);
    		cardNumTxt = new JTextField(15);
    
    		loginBtn = new JButton("Login");
    		loginBtn.addActionListener(this);
    
    		
    		exitBtn = new JButton("Exit");
    		exitBtn.addActionListener(this);
    
    
    
    		//create the container and set operations
    
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    
                    
    		//add image to used as logo
    
    		ImageIcon logo =  new ImageIcon("logocopy.jpg","Welcome to INB Services");
    		JLabel pic = new JLabel(logo);
    		
    
    		c = getContentPane();
    				
    		layout = new GridLayout(1,2);
    		buttonPanel = new JPanel();
    		buttonPanel.setLayout(layout);
    
    		//create ButtonPanel
    		//add components to the button panel
    
    		buttonPanel.add(loginBtn);
    		buttonPanel.add(exitBtn);
    
    		contentPane = new JPanel();
    		contentPane.setLayout(new FlowLayout());
    
    		contentPane.add(pic);
    		contentPane.add(cardNumLbl);
    		contentPane.add(cardNumTxt);
    		contentPane.add(pinLbl);
    		contentPane.add(pinTxt);
    
    		
    
    		//add the components to the GUI  container
    
    		c.add(contentPane);
    		c.add(buttonPanel,BorderLayout.SOUTH);
    
    
    		//show and customize the Interface
    
    		setSize(500,260);
    		show();
    		c.setBackground(Color.white);
    
    
    
    
    	}// method
    
    

    however the labels and txtfields wont appear under the pic that i want to use as a logo

    any help would be much apprectiated

    thanks


Comments

  • Registered Users, Registered Users 2 Posts: 659 ✭✭✭Chunks


    From what I can see is....

    your labels anfd textfields are appearing, but, the size of the jpg actually covers them up, making them appear not to be visible. The picture in the jpg might be small but the fringes of the jpg will also be included.


    If im right then the way to fix this problem is to cut what you want from the jpg using photoshop and when you create a tile to put the newly cut part into make it the size that you want it to appear on the GUI(i.e; amount of pixels in width and height). save it as a gif and change the logo.jpg to logo.gif. Hopefully it should work. Im working on a GUI at the mo also, so if my explanation isnt clear enough let me know and i'll try and help further


  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    thanks chunks

    one more question

    im trying to set up an actionlistener so that when i clikc a button another container will appear.

    whats the method for this? i cant seem to find it on the api
    public void actionPerformed(ActionEvent e)
    	{
    		if (e.getSource() == loginBtn )
    		{
    			JOptionPane.showMessageDialog(null,"Welcome to INB ATM Service");
    
    //code to display the next screen
    			
    
    			
    
    
    		}
    
    

    ??


  • Registered Users, Registered Users 2 Posts: 659 ✭✭✭Chunks


    um im not too sure what you mean by a container will appear. Do you mean a pop up box will appear? (cos if so i'm struggling with this problem too) Or do you want a panel to be cleared and a load of new textfields button etc be placed in it? If thats the case then all you have to do is...

    // when a button is pressed
    {
    Panelname.removeall();

    // all your new textfields, buttons etc are added to the panel here

    Panelname.revalidate();
    Panelname.repaint();
    }

    whenever a certain button is pressed this wipes the panel, then adds all the new stuff.

    Remeber to initialise all your action listeners ina seperate method also cos it will lead to headwrecking consequences later on.Call this method in your main method i.e:

    public static implement_action_listeners()
    {
    button.addActionListener(this);

    button.addActionListener(new classname); // cos you might a new class for different action listeners
    }

    public static void main (String [] args)
    {
    // initilaise your GUI here
    implement_action_listeners();
    }



    Hope this is of help to you.
    Any more questions dont hesitate to ask.


  • Closed Accounts Posts: 302 ✭✭Auburn


    You could try using GridBagLayout for positioning your components where you want them. 'Tis a bit tricky, though. As for the other thing:
    
    button.addActionListener(new java.awt.event.ActionListener()
    {
         public void actionPerformed(java.awt.event.ActionEvent e)
         {
                           openframe();
         }
    });
    
    ........
    
    public static void openframe()
    {
         JFrame f = new JFrame();
         f.add(whatever);
         f.pack();
         f.setvisible(true);
    }
    


  • Registered Users, Registered Users 2 Posts: 659 ✭✭✭Chunks


    you have to remember thought that the code given by auburn is java awt not java swing so if you were to copy it you would have to alter it slightly


  • Advertisement
  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    i wasnt too clear on what i meant by a container.
    i mean that i have two GUIs written say a Login screen and a Menu screen. i want to click a button Login and display the menu screen.

    i have tried using the CardLayoutManager but to no avail. can i just call the method where i create the menu gui and hide the login screen?

    thanks


  • Closed Accounts Posts: 302 ✭✭Auburn


    I'm not too sure what you mean. If you mean that you want to display the menu screen when the person has logged in correctly, then maybe put your menu screen GUI stuff in it's own class. And just do something like
    if((cardNumTxt.getText() == "whatever") && (pinTxt.getText() == "whatever"))
    {
         Login.dispose();  // this gets rid of the login screen
         menu mymenu = new menu();  // calling the menu gui class
         mymenu.setVisible(true);
         .........
    } 
    
    

    Hope that helps


  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    thanks Auburn thats exactly what i was looking for!


  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    however now im getting an error message:

    topUp.java:5: topUp should be declared abstract; it does not define itemStateCha
    nged(java.awt.event.ItemEvent) in topUp
    public class topUp extends JFrame implements ActionListener, ItemListener

    i cant see where i am going wrong here is the code:
    import javax.swing.*;
    import java.awt.*;		
    import java.awt.event.*;		
    
    public class topUp extends JFrame implements ActionListener, ItemListener
    {
    	
    	private JLabel topUpLbl, providerLbl, amountNumLbl,numLbl;
    	private JTextField numTxt;
    	private JButton confirmBtn,menuBtn,resetBtn;
    	private Container c;
    	private GridLayout layout, layout2;	
    	private FlowLayout flow;
    	private JPanel buttonPanel;
    	private JPanel content;
    	private String amount[] = {"€ 10","€ 20","€ 30","€ 40","€ 50"};
    	private String providers[] = {"Meteor","O2","Vodafone"};
    	private JComboBox amountCmb;
    	private JComboBox providerCmb;
    
    	
    
    
    
    	public topUp()
    	{
    		//assigning GUI components
    
    		super("Welcome to INB ATM System");
    
    		topUpLbl = new JLabel("Mobile Top-Up");
    
    		providerLbl = new JLabel("Select Provider:");
    		amountNumLbl = new JLabel("Select Amount:");
    
    		numTxt = new JTextField(10);
    		numLbl = new JLabel("Enter mobile number");
    
    		amountCmb = new JComboBox(amount);
    		amountCmb.ItemListener(this);
    
    		providerCmb = new JComboBox(providers);
    		providerCmb.addItemListener(this);
    
    
    		confirmBtn = new JButton("Confirm");
    		confirmBtn.addActionListener(this);
    
    		
    		menuBtn = new JButton("Main Menu");
    		menuBtn.addActionListener(this);
    		
    		resetBtn = new JButton("Clear fields");
    		resetBtn.addActionListener(this);
    
    
    
    
    		//create the container and set operations
    		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    
                    
    		c = getContentPane();
    				
    		
    		buttonPanel = new JPanel();
    		layout = new GridLayout(1,3);
    		buttonPanel.setLayout(layout);
    		
    	
    
    		//create ButtonPanel
    		//add components to the button panel
    
    		buttonPanel.add(confirmBtn);
    		buttonPanel.add(resetBtn);
    		buttonPanel.add(menuBtn);
    		
    
    		//create the content pane and add components 
    
    		content = new JPanel();
    
    		content.setLayout(new FlowLayout());
    
    		content.setBackground(Color.white);
    
    		content.add(providerLbl);
    
    		content.add(providerCmb);
    
    		content.add(amountNumLbl);
    
    		content.add(amountCmb);
    
    		content.add(numLbl);
    
    		content.add(numTxt);
    
    		
    
    		//add the components to the GUI  container
    
    		c.add(content, BorderLayout.CENTER);
    		c.add(buttonPanel, BorderLayout.SOUTH);
    		
    
    		//show and customize the Interface
    
    		setSize(600,400);
    		show();
    		
    
    
    	}// method
    
    
    
    
    	public static void main(String [] args)
    	{
    		topUp app = new topUp();
    
    	} //main
    
    
    	public void actionPerformed(ActionEvent e)
    	{
    		if (e.getSource() == confirmBtn )
    		{
    			JOptionPane.showMessageDialog(null,"Welcome to INB ATM Service");
    		
    
    
    
    		}
    
    
    		if (e.getSource() == menuBtn)
    		{
    	
    			Menu appMenu = new Menu();
    		}	
    
    		
    		if (e.getSource() == resetBtn)
    		{
    			numTxt.setText("");
    			
    
    		}
    
    	}
    
    
    }  // class
    
    


  • Closed Accounts Posts: 302 ✭✭Auburn


    When you implement an interface, e.g. ActionListener / ItemListemer, you have one of two choices, 1. declare your class abstract (which means it cannot be instantiated) or 2. override all the methods defined in that interface.

    By the looks of it, you dont want your class to be abstract, so insert the following
    method in your class :
    public void itemStateChanged(ItemEvent e) { }
    

    I haven't run your code, but that should sort it out hopefully ;)


  • Advertisement
Advertisement