13 Replies - 470 Views - Last Post: 11 October 2012 - 07:30 PM Rate Topic: -----

#1 LadyBee  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 09-October 12

use of cardLayout and other layouts

Posted 09 October 2012 - 11:55 AM

Hi, I do now about layouts and I try to use them in my code but it doesn't want to display
properly the data in JFrame. I use cardLayout b/c I want to switch between cards when I press one
of the bottoms. Could you please look at my code and to tell me if you see what I have to work on.
Thank you very much for any comments.

 

        CardLayout ca;

	public void addControls( Container c ) 
	{
		ca = new CardLayout();
		
		initConvertionPanel( c );
		initMeasurementPanel( c );
	}
	
	
	public void initConvertionPanel(Container c )
	{
		JButton conversionButton1, conversionButton2, exitButton;
		JLabel label;
		JPanel conversionButtonPanel;
		
		conversionTypePanel = new JPanel();
		conversionButtonPanel = new JPanel();
		conversionTypePanel.setLayout( ca );
		conversionButtonPanel.setLayout( new GridLayout(3,2) );
		conversionButton1 = new JButton("Convert English to Metric");
		conversionButton2 = new JButton("Convert Metric to English");
		exitButton = new JButton("Exit"); 
		label = new JLabel("Conversion Type");
		conversionButton1.addActionListener( new ConversionclickListener() );
		conversionButton2.addActionListener( new ConversionclickListener() );
		exitButton.addActionListener( new ConversionclickListener() );
		conversionButtonPanel.add(conversionButton1);
		conversionButtonPanel.add(conversionButton2); 
		conversionButtonPanel.add(exitButton);
		conversionTypePanel.add(label, BorderLayout.NORTH);
		conversionTypePanel.add(conversionButtonPanel, "Convert English to Metric" );
		conversionTypePanel.add(conversionButtonPanel, "Convert Metric to English" );
		conversionTypePanel.add(conversionButtonPanel, "Exit" );
		conversionTypePanel.add(conversionButtonPanel, BorderLayout.CENTER);
		conversionTypePanel.setBackground( Color.MAGENTA );
		c.add(conversionTypePanel);		
	}




Is This A Good Question/Topic? 0
  • +

Replies To: use of cardLayout and other layouts

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: use of cardLayout and other layouts

Posted 09 October 2012 - 12:45 PM

Thats not all your code is it? Do you add the container to your JFrame? Do you setVisible(true) on your panel?
Was This Post Helpful? 1
  • +
  • -

#3 LadyBee  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: use of cardLayout and other layouts

Posted 09 October 2012 - 12:54 PM

Hi, yes it isn't all my code. I added now more code which I think is needed to better see what I did.
I added JFrame and set up Panel to be visible. Thank you for help.



import java.awt.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

public class Driver extends JFrame
{
	UILayer uiLayer;
	BusinessLayer businessLayer;
	DataLayer dataLayer;

	
	public void init()
	{
		uiLayer = new UILayer();
		businessLayer = new BusinessLayer();
		dataLayer = new DataLayer();	
		businessLayer.setDataLayer(dataLayer);
		businessLayer.setUILayer(uiLayer);
		uiLayer.setDataLayer(dataLayer);
		uiLayer.setBusinessLayer(businessLayer);
	}
	
	public void start()
	{
		uiLayer.addControls(getContentPane());
		
	}
	
	public Driver( String title ) throws IOException 
	{
		super(title);
		init();
		pack();
		setVisible(true);	
		start();
	}
	
	public static void main ( String [ ] args )
	{
		Driver driver;
		try
		{
			driver = new Driver( "Converter" ); 
			Console.run(driver, 400, 400);
		}
		
		catch ( IOException e )
		{
			e.printStackTrace();
		}
		
	}
}

import java.awt.CardLayout;
import javax.swing.*;

public class Console 
{
	public static void run( JFrame frame, int width, int high )
	{
		
		frame.setLocationRelativeTo( null );
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
		
		if ( width > 0 && high > 0 )
		{
			frame.setSize(width, high);
			frame.setVisible(true);
		}
		else
		{
			frame.setVisible(false);
		}
	}
}




import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class UILayer 
{
	DataLayer dlayer;
	BusinessLayer blayer;
	private JPanel conversionTypePanel, measurementPanel;
	Container c1;
	CardLayout ca;
	
	public void setDataLayer( DataLayer layer )
	{
		dlayer = layer;
	}
	
	public void setBusinessLayer( BusinessLayer layer)
	{
		blayer = layer;
	}
	
	public DataLayer getDataLayer()
	{
		return dlayer;
	}
	
	public BusinessLayer getBusinessLayer()
	{
		return blayer;
	}
		
	public void addControls( Container c ) 
	{
		ca = new CardLayout();
		
		initConvertionPanel( c );
		initMeasurementPanel( c );
	}
	
	
	public void initConvertionPanel(Container c )
	{
		JButton conversionButton1, conversionButton2, exitButton;
		JLabel label;
		JPanel conversionButtonPanel;
		
		conversionTypePanel = new JPanel();
		conversionButtonPanel = new JPanel();
		conversionTypePanel.setLayout( ca );
		conversionButtonPanel.setLayout( new GridLayout(3,2) );
		conversionButton1 = new JButton("Convert English to Metric");
		conversionButton2 = new JButton("Convert Metric to English");
		exitButton = new JButton("Exit"); 
		label = new JLabel("Conversion Type");
		conversionButton1.addActionListener( new ConversionclickListener() );
		conversionButton2.addActionListener( new ConversionclickListener() );
		exitButton.addActionListener( new ConversionclickListener() );
		conversionButtonPanel.add(conversionButton1);//"Convert English to Metric");
		conversionButtonPanel.add(conversionButton2); //"Convert Metric to Englis");
		conversionButtonPanel.add(exitButton);
		/*conversionTypePanel.add(label, BorderLayout.NORTH);
		conversionTypePanel.add(conversionButtonPanel, BorderLayout.CENTER);
		conversionTypePanel.setBackground( Color.MAGENTA );*/
		
		conversionTypePanel.add(label, BorderLayout.NORTH);
		conversionTypePanel.add(conversionButtonPanel, "Convert English to Metric" );
		conversionTypePanel.add(conversionButtonPanel, "Convert Metric to English" );
		conversionTypePanel.add(conversionButtonPanel, "Exit" );
		conversionTypePanel.add(conversionButtonPanel, BorderLayout.CENTER);
		conversionTypePanel.setBackground( Color.MAGENTA );
		c.add(conversionTypePanel);	
			
	}
	
	public void initMeasurementPanel(Container c)
	{
		measurementPanel = new JPanel();
		JPanel buttonsPanel = new JPanel(); 
		JLabel label = new JLabel("MeasurementType");
		JButton distance = new JButton("Distance");
		JButton weight = new JButton("Weight");
		JButton volume = new JButton("Volume");
		JButton exit = new JButton("Exit");
		
		measurementPanel.setLayout(new GridLayout(2,1));
		measurementPanel.setBackground( Color.MAGENTA );
		buttonsPanel.setLayout(new GridLayout(4,2));
		//measurementPanel.setVisible(false);
		//measurementPanel.setEnabled(false);
		distance.addActionListener( new MeasurementClickListener() );
		weight.addActionListener( new MeasurementClickListener() );
		volume.addActionListener( new MeasurementClickListener() );
		exit.addActionListener( new MeasurementClickListener() );
		buttonsPanel.add(distance);
		buttonsPanel.add(weight);
		buttonsPanel.add(volume);
		buttonsPanel.add(exit);
		measurementPanel.add(label);
		measurementPanel.add(buttonsPanel);	
		c.add(measurementPanel);
	}
	
	protected class ConversionclickListener implements ActionListener
	{
		ConversionclickListener() { }
		
		@Override
		public void actionPerformed( ActionEvent event ) 
		{	
			if ( event.getActionCommand().equals("Exit") )	
			{ 
				int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Message",
					    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				
				if( response == JOptionPane.YES_OPTION )
				{
					JOptionPane.showMessageDialog( null, "Thank you for using our services." +
				    " Have a nice day", "Message", JOptionPane.INFORMATION_MESSAGE );
					System.exit(0);
				}	
			}
			else 
			{
				if ( event.getActionCommand().equals("Convert English to Metric") )
				{
					blayer.setConversionType(0);
					ca.show(measurementPanel, "Convert English to Metric");
				}
				else if (( event.getActionCommand().equals("Convert Metric to English")))
				{
					blayer.setConversionType(1);
					ca.show(measurementPanel, null);
				}
				//showMeasurementPanel( );
			}
		}
	}
	
	public void showMeasurementPanel()
	{
		//ca.show(measurementPanel, "Convert English to Metric");
		//ca.show(measurementPanel, "Convert Metric to English");
		//c1.remove(conversionTypePanel);
		//c1.add(measurementPanel);
	
	}	
	
	protected class MeasurementClickListener implements ActionListener
	{
		MeasurementClickListener() { }
		
		@Override
		public void actionPerformed( ActionEvent event ) 
		{	
			if ( event.getActionCommand().equals("Exit") )	
			{ 
				int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Message",
					    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				
				if( response == JOptionPane.YES_OPTION )
				{
					JOptionPane.showMessageDialog( null, "Thank you for using our services." +
				    " Have a nice day", "Message", JOptionPane.INFORMATION_MESSAGE );
					System.exit(0);
				}	
			}
			else 
			{
				if ( event.getActionCommand().equals("Distance") )
				{
					
				}
				else if (event.getActionCommand().equals("Weight"))
				{
					
				}
				else
				{
					
				}
				//showMeasurementPanel( );
			}
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: use of cardLayout and other layouts

Posted 09 October 2012 - 01:26 PM

A JComponent can only be at one place at the same moment

Here you add 4 times conversionButtonPanel to conversionTypePanel
only the last one will be effective, so the one in BorderLayout.CENTER the other ones will just vanish

conversionTypePanel.add(conversionButtonPanel, "Convert English to Metric" );
conversionTypePanel.add(conversionButtonPanel, "Convert Metric to English" );
conversionTypePanel.add(conversionButtonPanel, "Exit" );
conversionTypePanel.add(conversionButtonPanel, BorderLayout.CENTER);
Was This Post Helpful? 1
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: use of cardLayout and other layouts

Posted 09 October 2012 - 02:03 PM

So add what you need to add to conversionButtonPanel, and then add this one panel to conversionTypePanel. On a brighter note, is nice to see some nicely written gui code for a change.
Was This Post Helpful? 0
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: use of cardLayout and other layouts

Posted 09 October 2012 - 03:26 PM

View Postnick2price, on 09 October 2012 - 09:03 PM, said:

On a brighter note, is nice to see some nicely written gui code for a change.


You consider that nice gui code??? Wow! Not only is it insanely over-complicated, it isn't even Thread safe. The variable names are too generic, class names conflict with existing Java classes, the if/else bomb in actionPerformed isn't a very object oriented approach, and the code is all over the place!
Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: use of cardLayout and other layouts

Posted 09 October 2012 - 04:03 PM

Quote

Not only is it insanely over-complicated,

How? Its a good early attempt at trying to seperate layers. And anyone who even attepts to use a design pattern, especially a beginner, should be commended.

Quote

it isn't even Thread safe.

They dont even use threads.

Quote

The variable names are too generic, class names conflict with existing Java classes,

Thats true, but this is a case of conventions, not bad coding.

Quote

the if/else bomb in actionPerformed isn't a very object oriented approach,

Just because its an OO language, doesnt mean you have to do overkill. No need to make this part OO, especially when first learning the language.

Quote

and the code is all over the place!

Once again, how? They are attempting to seperate layers, and this should be commended.

If you want to put down someones code, go into a thread where they use ide generated code. Someone who makes an effort to do the correct things should be commended, not insulted.
Was This Post Helpful? 0
  • +
  • -

#8 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: use of cardLayout and other layouts

Posted 09 October 2012 - 05:47 PM

View Postnick2price, on 09 October 2012 - 11:03 PM, said:

They dont even use threads.


Oh, God.

Every Swing app has three threads, The main, the event dispatch, and the toolkit thread. You do not create or modify Swing components on any thread, but the event dispatch thread.

I am not insulting the TC, but your comment about it being nice code was way off.

As for the other nonsense you wrote, I won't even get into it. I hope you're not paid to write, test, or debug Java code...

This post has been edited by farrell2k: 09 October 2012 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: use of cardLayout and other layouts

Posted 09 October 2012 - 06:06 PM

View Postnick2price, on 09 October 2012 - 07:03 PM, said:

Quote

it isn't even Thread safe.

They dont even use threads.

Nick I guess you should avoid to comment on GUI in the future :)

View Postnick2price, on 09 October 2012 - 05:03 PM, said:

On a brighter note, is nice to see some nicely written gui code for a change.

Not really sure what you mean
Not really structured... but the OP is a beginnr... I hope
Let's focus on the OP problem without code quality comments
Was This Post Helpful? 0
  • +
  • -

#10 LadyBee  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: use of cardLayout and other layouts

Posted 09 October 2012 - 07:22 PM

I think I will not comment what that other guy was writing. :)
I want to say:
1) I have just finish course - java for beginners
2) don't find the word threads
3) the reason of using different layers is my teacher design to give us some idea where to write code

Well I solved my problem. Thank you everyone who tried to help me.
Was This Post Helpful? 0
  • +
  • -

#11 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: use of cardLayout and other layouts

Posted 10 October 2012 - 02:16 AM

Just to clear things up quickly. My mistake about the thread part, havnt used Swing in a while. The whole point of what I posted was to encourage the original post to continue writing their own gui code, and not to rely on ide generated code. I am not saying its brilliant code, I am saying its a good attempt (for a beginner) to seperate layers. Forget about what your capable of, this is someone who is beginning java, not someone with 10 years experience. To many beginners these days rely on the ide to produce their apps, and its something we see a lot on the forum. I respect any beginner who attempts to write everything themselves. So if you want to be useful, give some constructive critism, not just critism.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: use of cardLayout and other layouts

Posted 10 October 2012 - 03:39 AM

So LadyBee how does your updated code looks like ?
Was This Post Helpful? 0
  • +
  • -

#13 LadyBee  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: use of cardLayout and other layouts

Posted 11 October 2012 - 07:16 PM

Hi,
The main problem with my code were containers.
I didn't know how to use them correctly because I created two container.
I didn't put panels into one container and it was causing problems.
I highlighted changes in the code.

[code]


import java.awt.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

public class Driver extends JFrame
{
UILayer uiLayer;
BusinessLayer businessLayer;
DataLayer dataLayer;

public void init()
{
uiLayer = new UILayer(getContentPane()); // pass container here
businessLayer = new BusinessLayer();
dataLayer = new DataLayer();
businessLayer.setDataLayer(dataLayer);
businessLayer.setUILayer(uiLayer);
uiLayer.setDataLayer(dataLayer);
uiLayer.setBusinessLayer(businessLayer);
}

public void start()
{
uiLayer.addControls(); // before passed container here
}

public Driver( String title ) throws IOException
{
super(title);
init();
pack();
setVisible(true);
start();
}

public static void main ( String [ ] args )
{
Driver driver;
try
{
driver = new Driver( "Converter" );
Console.run(driver, 400, 400);
}

catch ( IOException e )
{
e.printStackTrace();
}
}
}


import java.awt.CardLayout;
import javax.swing.*;

public class Console
{
public static void run( JFrame frame, int width, int high )
{
frame.setLocationRelativeTo( null );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

if ( width > 0 && high > 0 )
{
frame.setSize(width, high);
frame.setVisible(true);
}
else
{
frame.setVisible(false);
}
}
}



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class UILayer
{
private DataLayer dlayer;
private BusinessLayer blayer;
private JPanel conversionTypePanel, measurementPanel, ;
private Container c1;
private JTextField amount, result;

public UILayer( Container c ) // passed container
{
c1 = c;
}


public void setDataLayer( DataLayer layer )
{
dlayer = layer;
}

public void setBusinessLayer( BusinessLayer layer )
{
blayer = layer;
}

public DataLayer getDataLayer()
{
return dlayer;
}

public BusinessLayer getBusinessLayer()
{
return blayer;
}

public void addControls( ) // don't pass contenter
{
c1.setLayout(new CardLayout()); // used CardLayout for container c1
initConvertionPanel( );
initMeasurementPanel( );
}


public void initConvertionPanel()
{
JButton conversionButton1, conversionButton2, exitButton;
JLabel label;
JPanel conversionButtonPanel;

conversionTypePanel = new JPanel();
conversionButtonPanel = new JPanel();
conversionTypePanel.setLayout( new BorderLayout() );
conversionButtonPanel.setLayout( new GridLayout(3,2) );
conversionButton1 = new JButton("Convert English to Metric");
conversionButton2 = new JButton("Convert Metric to English");
exitButton = new JButton("Exit");
label = new JLabel("Conversion Type");
conversionButton1.addActionListener( new ConversionclickListener() );
conversionButton2.addActionListener( new ConversionclickListener() );
exitButton.addActionListener( new ConversionclickListener() );
conversionButtonPanel.add(conversionButton1);
conversionButtonPanel.add(conversionButton2);
conversionButtonPanel.add(exitButton);
conversionTypePanel.add(label, BorderLayout.NORTH);
conversionTypePanel.add(conversionButtonPanel, BorderLayout.CENTER);
conversionTypePanel.setBackground( Color.MAGENTA );
c1.add(conversionTypePanel, "Conversion Panel");

}

public void initMeasurementPanel()
{
measurementPanel = new JPanel();
JPanel buttonsPanel = new JPanel();
JLabel label = new JLabel("MeasurementType");
measurementPanel.setLayout(new BorderLayout());
measurementPanel.setBackground( Color.MAGENTA );
buttonsPanel.setLayout(new GridLayout(4,1));
JButton b;
for(Measurement m : dlayer.getMeasurementsList())
{
b = new JButton(m.getType());
b.addActionListener( new MeasurementClickListener() );
buttonsPanel.add(B);
}

JButton exit = new JButton("Exit");
exit.addActionListener( new MeasurementClickListener() );
buttonsPanel.add(exit);
measurementPanel.add(label, BorderLayout.NORTH);
measurementPanel.add(buttonsPanel, BorderLayout.CENTER);
c1.add(measurementPanel, "Measurement Panel");
}

public void initConversionFromToPanel( Measurement m )
{
conversionFromToPanel = new JPanel();
JPanel convertFromPanel = new JPanel();
JPanel convertToPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel calculationPanel = new JPanel();
conversionFromToPanel.setLayout(new BorderLayout());
conversionFromToPanel.setBackground( Color.MAGENTA );
convertFromPanel.setLayout(new GridLayout(4,1));
convertToPanel.setLayout(new GridLayout(4,1));
calculationPanel.setLayout(new GridLayout(5,1));
centerPanel.setLayout(new GridLayout(1,2));
JLabel label1 = new JLabel("Convert from: ");
JLabel label2 = new JLabel("Convert to: ");
JLabel label3 = new JLabel("Enter amount: ");
JLabel label4 = new JLabel("Result: ");
JRadioButton b;
ButtonGroup bgroup1 = new ButtonGroup();
ButtonGroup bgroup2 = new ButtonGroup();
convertFromPanel.add(label1);
convertToPanel.add(label2);

if(blayer.getConversionType() == 1 )
{

for( Unit u : m.getUnitsList(0))
{
b = new JRadioButton(u.type);
b.addActionListener( new RadioButtonFromClickListener());
convertFromPanel.add(B);
bgroup1.add(B);
}

for( Unit u : m.getUnitsList(1))
{
b = new JRadioButton(u.type);
b.addActionListener( new RadioButtonToClickListener());
convertToPanel.add(B);
bgroup2.add(B);
}
}
else
{
for( Unit u : m.getUnitsList(1))
{
b = new JRadioButton(u.type);
b.addActionListener( new RadioButtonFromClickListener());
convertFromPanel.add(B);
bgroup1.add(B);
}

for( Unit u : m.getUnitsList(0))
{
b = new JRadioButton(u.type);
b.addActionListener( new RadioButtonToClickListener());
convertToPanel.add(B);
bgroup2.add(B);
}
}

JButton calculate = new JButton("Calculate");
amount = new JTextField();
result = new JTextField();
result.setEnabled(false);
convertFromPanel.setBackground(Color.WHITE);
convertToPanel.setBackground(Color.YELLOW);
calculationPanel.setBackground(Color.BLUE);
calculate.addActionListener( new CalculateClickListener() );
calculationPanel.add(label3);
calculationPanel.add(amount);
calculationPanel.add(calculate);
calculationPanel.add(label4);
calculationPanel.add(result);
centerPanel.add(convertFromPanel);
centerPanel.add(convertToPanel);
conversionFromToPanel.add(centerPanel, BorderLayout.CENTER);
conversionFromToPanel.add(calculationPanel, BorderLayout.SOUTH);
c1.add(conversionFromToPanel, "Conversion From To Panel");
}

protected class ConversionclickListener implements ActionListener
{
ConversionclickListener() { }

@Override
public void actionPerformed( ActionEvent event )
{
if ( event.getActionCommand().equals("Exit") )
{
int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Message",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

if( response == JOptionPane.YES_OPTION )
{
JOptionPane.showMessageDialog( null, "Thank you for using our services." +
" Have a nice day", "Message", JOptionPane.INFORMATION_MESSAGE );
System.exit(0);
}
}
else
{
if ( event.getActionCommand().equals("Convert English to Metric") )
{
blayer.setConversionType(0);
}
else
{
blayer.setConversionType(1);
}
showMeasurementPanel( );
}
}
}

public void showMeasurementPanel()
{
CardLayout card1 = (CardLayout)c1.getLayout();
card1.show(c1, "Measurement Panel");
}

protected class MeasurementClickListener implements ActionListener
{
MeasurementClickListener() { }

@Override
public void actionPerformed( ActionEvent event )
{
if ( event.getActionCommand().equals("Exit") )
{
int response = JOptionPane.showConfirmDialog( null, "Are you sure you want to exit?", "Message",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE );

if( response == JOptionPane.YES_OPTION )
{
JOptionPane.showMessageDialog( null, "Thank you for using our services." +
" Have a nice day", "Message", JOptionPane.INFORMATION_MESSAGE );
System.exit(0);
}
}
else
{
blayer.setMeasurementType(event.getActionCommand());
Measurement m = dlayer.getMeasurement( event.getActionCommand() );
initConversionFromToPanel(m);
showConversionFromToPanel( );
}
}
}

public void showConversionFromToPanel( )
{
CardLayout card1 = (CardLayout)c1.getLayout();
card1.show( c1, "Conversion From To Panel" );
}

protected class RadioButtonFromClickListener implements ActionListener
{
RadioButtonFromClickListener() { }

@Override
public void actionPerformed( ActionEvent event )
{
blayer.setFromUnit(event.getActionCommand());
}
}

protected class RadioButtonToClickListener implements ActionListener
{
RadioButtonToClickListener() { }

@Override
public void actionPerformed( ActionEvent event )
{
blayer.setToUnit(event.getActionCommand());
}
}

protected class CalculateClickListener implements ActionListener
{
CalculateClickListener() { }

@Override
public void actionPerformed( ActionEvent event )
{
if ( event.getActionCommand().equals("Calculate") )
{
if(!blayer.areUnitsSelected())
JOptionPane.showMessageDialog( null, "Please select units for conversion ",
"Message", JOptionPane.ERROR_MESSAGE );
else
{
//(amount.getText())
try
{
result.setText(Double.toString(blayer.convertData(Double.parseDouble(amount.getText()))));
}
catch(NumberFormatException e )
{
JOptionPane.showMessageDialog( null, "Please enter valid amount of units to convert!",
"Message", JOptionPane.ERROR_MESSAGE );
}
}
}
}
}
}

[\code]
Was This Post Helpful? 0
  • +
  • -

#14 LadyBee  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: use of cardLayout and other layouts

Posted 11 October 2012 - 07:30 PM

I didn't finish commenting previous post because I
press something and it posted everything I wrote.
I highlighted changes in the code. Also, I added a lot of code in ActionListeners.




import java.awt.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

public class Driver extends JFrame
{
	UILayer uiLayer;
	BusinessLayer businessLayer;
	DataLayer dataLayer;
	
	public void init()
	{
	[b]	uiLayer = new UILayer(getContentPane())[/b]; // pass container here
		businessLayer = new BusinessLayer();
		dataLayer = new DataLayer();	
		businessLayer.setDataLayer(dataLayer);
		businessLayer.setUILayer(uiLayer);
		uiLayer.setDataLayer(dataLayer);
		uiLayer.setBusinessLayer(businessLayer);
	}
	
	public void start()
	{
		[b]uiLayer.addControls();[/b] // before passed container here
	}
	
	public Driver( String title ) throws IOException 
	{
		super(title);
		init();
		pack();
		setVisible(true);	
		start();
	}
	
	public static void main ( String [ ] args )
	{
		Driver driver;
		try
		{
			driver = new Driver( "Converter" ); 
			Console.run(driver, 400, 400);
		}
		
		catch ( IOException e )
		{
			e.printStackTrace();
		}
	}
}


import java.awt.CardLayout;
import javax.swing.*;

public class Console 
{
	public static void run( JFrame frame, int width, int high )
	{
		frame.setLocationRelativeTo( null );
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
		
		if ( width > 0 && high > 0 )
		{
			frame.setSize(width, high);
			frame.setVisible(true);
		}
		else
		{
			frame.setVisible(false);
		}
	}
}



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class UILayer 
{
	private DataLayer dlayer;
	private BusinessLayer blayer;
	private JPanel conversionTypePanel, measurementPanel, ;
	private Container c1;
	private JTextField amount, result;
   
	[b]public UILayer( Container c ) // passed container
	{
		c1 = c;
	}[/b]
	
	public void setDataLayer( DataLayer layer )
	{
		dlayer = layer;
	}
	
	public void setBusinessLayer( BusinessLayer layer )
	{
		blayer = layer;
	}
	
	public DataLayer getDataLayer()
	{
		return dlayer;
	}
	
	public BusinessLayer getBusinessLayer()
	{
		return blayer;
	}
		
	[b]public void addControls( ) // don't pass contenter
	{
		c1.setLayout(new CardLayout()); // used CardLayout for container c1
		initConvertionPanel( );
		initMeasurementPanel( );
	}[/b]
	
	public void initConvertionPanel()
	{
		JButton conversionButton1, conversionButton2, exitButton;
		JLabel label;
		JPanel conversionButtonPanel;
		
		conversionTypePanel = new JPanel();
		conversionButtonPanel = new JPanel();
		[b]conversionTypePanel.setLayout( new BorderLayout() );[/b] // used BorderLayout
		conversionButtonPanel.setLayout( new GridLayout(3,2) );
		conversionButton1 = new JButton("Convert English to Metric");
		conversionButton2 = new JButton("Convert Metric to English");
		exitButton = new JButton("Exit"); 
		label = new JLabel("Conversion Type");
		conversionButton1.addActionListener( new ConversionclickListener() );
		conversionButton2.addActionListener( new ConversionclickListener() );
		exitButton.addActionListener( new ConversionclickListener() );
		conversionButtonPanel.add(conversionButton1);
		conversionButtonPanel.add(conversionButton2);
		conversionButtonPanel.add(exitButton);
		conversionTypePanel.add(label, BorderLayout.NORTH);
		conversionTypePanel.add(conversionButtonPanel, BorderLayout.CENTER);
		conversionTypePanel.setBackground( Color.MAGENTA );
		[b]c1.add(conversionTypePanel, "Conversion Panel");[/b]	
                //added conversionTypePanel to container c1 and added name for this panel
	}                                                                
	
	public void initMeasurementPanel()
	{
		measurementPanel = new JPanel();
		JPanel buttonsPanel = new JPanel(); 
		JLabel label = new JLabel("MeasurementType");
		measurementPanel.setLayout(new BorderLayout());
		measurementPanel.setBackground( Color.MAGENTA );
		buttonsPanel.setLayout(new GridLayout(4,1));
		JButton b;
		for(Measurement m : dlayer.getMeasurementsList())
		{
			b = new JButton(m.getType());
			b.addActionListener( new MeasurementClickListener() );
			buttonsPanel.add(B)/>;
		}
		
		JButton exit = new JButton("Exit");
		exit.addActionListener( new MeasurementClickListener() );
		buttonsPanel.add(exit);
		measurementPanel.add(label, BorderLayout.NORTH);
		measurementPanel.add(buttonsPanel, BorderLayout.CENTER);	
		[b]c1.add(measurementPanel, "Measurement Panel");[/b]
                //added measurementnTypePanel to container c1 and added name for this panel
	}
	
	protected class ConversionclickListener implements ActionListener
	{
		ConversionclickListener() { }
		
		@Override
		public void actionPerformed( ActionEvent event ) 
		{	
			if ( event.getActionCommand().equals("Exit") )	
			{ 
				int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Message",
					    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				
				if( response == JOptionPane.YES_OPTION )
				{
					JOptionPane.showMessageDialog( null, "Thank you for using our services." +
				    " Have a nice day", "Message", JOptionPane.INFORMATION_MESSAGE );
					System.exit(0);
				}	
			}
			else 
			{
				if ( event.getActionCommand().equals("Convert English to Metric") )
				{
					blayer.setConversionType(0);
				}
				else
				{
					blayer.setConversionType(1);
				}
				showMeasurementPanel( );
			}
		}
	}
	
	public void showMeasurementPanel()
	{
		CardLayout card1 = (CardLayout)c1.getLayout();	
		card1.show(c1, "Measurement Panel");
	}	
	
	protected class MeasurementClickListener implements ActionListener
	{
		MeasurementClickListener() { }
		
		@Override
		public void actionPerformed( ActionEvent event ) 
		{	
			if ( event.getActionCommand().equals("Exit") )	
			{ 
				int response = JOptionPane.showConfirmDialog( null, "Are you sure you want to exit?", "Message",
					    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE );
				
				if( response == JOptionPane.YES_OPTION )
				{
					JOptionPane.showMessageDialog( null, "Thank you for using our services." +
				    " Have a nice day", "Message", JOptionPane.INFORMATION_MESSAGE );
					System.exit(0);
				}	
			}
			else 
			{
					blayer.setMeasurementType(event.getActionCommand());
					Measurement m =  dlayer.getMeasurement( event.getActionCommand() );					
					initConversionFromToPanel(m);
				    showConversionFromToPanel( );	    
			}
		}
	}
	
	
	}
}




Edited by macosxnerd101: Please: :code:.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1