BorderFactory TitledBorder

problem creating a TitledBorder on a gui panel with several panels. Th

Page 1 of 1

7 Replies - 1271 Views - Last Post: 22 October 2009 - 08:56 PM Rate Topic: -----

#1 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

BorderFactory TitledBorder

Post icon  Posted 21 October 2009 - 08:25 PM

Everything else is working properly, the directions were to add a TitledBorder with the value "Customer Order" to the panel instance. I am doing what the book says only the border is not showing up. There are 6 panels on this content pane, how do i get the border to go around all of it?

Can someone give me a visual, I learn more visually.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;
public class Ch07Ex03 extends JFrame
{
	DecimalFormat currency = new DecimalFormat("##0.00");
	private JPanel panel;
	private JLabel messageLabel;
	private JLabel results;
	private JLabel totalCostLabel;
	private JTextField textField;
	private JButton submitButton;
	private JButton resetButton;
	private final int WINDOW_WIDTH = 600;
	private final int WINDOW_HEIGHT = 500;
	
	/**
	Constructor
	*/
	
	public Ch07Ex03()
	{
		//Set the window title.
		setTitle("Order");
		
		//Set the size of the window.
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		
		
		//Specify what happens when the close button is clicked.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Add a GridLayout manager to the content pane.
		setLayout(new GridLayout(3, 2));
		
		
		
		//Build the panel and add it to the frame.
		buildPanel();
		panel.setBorder(BorderFactory.createTitledBorder("Customer Order"));	
		
		//set the panel background to light gray.
		getContentPane().setBackground(Color.lightGray);
		
		//set the messageLabel and textField to Blue.
		messageLabel.setForeground(Color.BLUE);
		textField.setBackground(Color.BLUE);
		
		//set the totalCost label and results label to red.
		totalCostLabel.setForeground(Color.RED);
		results.setForeground(Color.RED);
		
			

		
		//Create 6 panels
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		JPanel panel3 = new JPanel();
		JPanel panel4 = new JPanel();
		JPanel panel5 = new JPanel();
		JPanel panel6 = new JPanel();
	
		
		
		//Add the components to the panels.

		panel1.add(messageLabel);
		panel2.add(textField);
		panel3.add(submitButton);
		panel4.add(resetButton);
		panel5.add(totalCostLabel);
		panel6.add(results);
		
		//Add the panels to the content pane.
		add(panel1);
		add(panel2);
		add(panel3);
		add(panel4);
		add(panel5);
		add(panel6);
	
		
		
		//Display the window.
		setVisible(true);
	}
	/**
	The buildPanel method adds a label, text field, and a button to the panel.
	*/
	
	private void buildPanel()
	{
		
		//Create a label to display instructions.
		messageLabel = new JLabel("Enter the number of items");
		
		//Creates a text field 10 characters wide.
		textField = new JTextField(10);
		
		//Create a button with the caption "Submit".
		submitButton = new JButton("Submit");
		
		
		//Create a button with the caption "Reset."
		resetButton = new JButton("Clear");
		
		//Creates a label to display the results.
		results = new JLabel("Result");
		
		//Creates a label for Total Cost.
		totalCostLabel = new JLabel("Total Cost");
		
		//Add an action listener to the button.
		submitButton.addActionListener(new ButtonListener());
		
		//Add an action listener to the button.
		resetButton.addActionListener(new ButtonListener());
		
		
		//Create a JPanel object and let the panel field reference it.
		panel = new JPanel();
		
		//Add the label, text field, and button components to the panel.
		panel.add(messageLabel);
		panel.add(textField);
		panel.add(results);
		panel.add(submitButton);
		panel.add(resetButton);
		
		
		
	}
	/**
	SubmitButtonListener is an action listener class for the submit button
	*/
	
	private class ButtonListener implements ActionListener
	{
		/**
		the actionPerformed method executes when the user clicks on the submit button.
		@param e the event object.
		*/
	
		
		public void actionPerformed(ActionEvent e)
		{
			String input; //To hold the users input
			int numOrdered; //The number items ordered
			
			//Get the text entered by the user into the text field.
			input = textField.getText();
			
			//Convert the input to items.
			numOrdered = Integer.parseInt(input);
			double total = numOrdered * 50.00;
			
			if(e.getSource() == submitButton)
			{
			//Display the result.
			results.setText(currency.format(total));
			}
			else if(e.getSource() == resetButton)
			{
				textField.setText("");
			}
			
		}
	}

		public static void main(String[] args)
		 {
 		new Ch07Ex03();
		}

}


This post has been edited by tricket_7: 22 October 2009 - 11:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: BorderFactory TitledBorder

#2 pbl  Icon User is offline

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

Reputation: 8021
  • View blog
  • Posts: 31,132
  • Joined: 06-March 08

Re: BorderFactory TitledBorder

Posted 21 October 2009 - 09:02 PM

Two things:

first: your Panel (to which you add the Border) is never added to your frame so no wonder it's Border does not show up
second: a JComponent cannot be at two places at the same time so you add in your buildPanel() method messageLabel, textField, ... to your panel... but later on you add them to panel1, panel2, panel3, ....
by doing that, you remove them from where they were (panel) and put them into the other panel

you see them because you add panel1, panel2, panel3.... to your frame
but the panel with a Border is never added and would contain nothing
Was This Post Helpful? 0
  • +
  • -

#3 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: BorderFactory TitledBorder

Posted 21 October 2009 - 09:55 PM

View Postpbl, on 21 Oct, 2009 - 08:02 PM, said:

Two things:

first: your Panel (to which you add the Border) is never added to your frame so no wonder it's Border does not show up
second: a JComponent cannot be at two places at the same time so you add in your buildPanel() method messageLabel, textField, ... to your panel... but later on you add them to panel1, panel2, panel3, ....
by doing that, you remove them from where they were (panel) and put them into the other panel

you see them because you add panel1, panel2, panel3.... to your frame
but the panel with a Border is never added and would contain nothing

I am confused then, if I take away the add(panel1) etc. then nothing shows up but then the border shows up. I am not sure I am understanding how to fix this....
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8021
  • View blog
  • Posts: 31,132
  • Joined: 06-March 08

Re: BorderFactory TitledBorder

Posted 22 October 2009 - 03:52 PM

View Posttricket_7, on 21 Oct, 2009 - 08:55 PM, said:

I am confused then, if I take away the add(panel1) etc. then nothing shows up but then the border shows up. I am not sure I am understanding how to fix this....

You should not only remove the add(panel1), ....
you should completly remove them (thei definition and the lines that add() something to them
Was This Post Helpful? 0
  • +
  • -

#5 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: BorderFactory TitledBorder

Posted 22 October 2009 - 06:03 PM

I see, but when I remove that the items are not longer in the order they needed to be in.
I have the GridLayout manager this should be in two columns and 3 rows, the first row should show the messageLabel and text box in blue
2nd row should show the submit and clear button
3rd row should show Total Cost and REsults the text in Red.
So how do i get them back in the order they were to be in?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8021
  • View blog
  • Posts: 31,132
  • Joined: 06-March 08

Re: BorderFactory TitledBorder

Posted 22 October 2009 - 07:52 PM

Just change the order you add them :D

GridLayout are fill row by row so the first add() will put the component in 0,0 the second in 0,1 the thirs in 1, 0.....
Was This Post Helpful? 0
  • +
  • -

#7 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: BorderFactory TitledBorder

Posted 22 October 2009 - 08:08 PM

View Postpbl, on 22 Oct, 2009 - 06:52 PM, said:

Just change the order you add them :D

GridLayout are fill row by row so the first add() will put the component in 0,0 the second in 0,1 the thirs in 1, 0.....

I made these changes to my code, but maybe I have the GridLayout wrong, the order is correct for the add(). but the panel displays everything in one row, I attached the updated version of my code.
I really appreciate your help.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;
public class Ch07Ex03 extends JFrame
{
	DecimalFormat currency = new DecimalFormat("##0.00");
	private JPanel panel;
	private JLabel messageLabel, results, totalCostLabel;
	private JTextField textField;
	private JButton submitButton, resetButton;
	private final int WINDOW_WIDTH = 600;
	private final int WINDOW_HEIGHT = 500;
	
	/**
	Constructor
	*/
	
	public Ch07Ex03()
	{
		//Set the window title.
		setTitle("Order");
		
		//Set the size of the window.
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		
		
		//Specify what happens when the close button is clicked.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Add a GridLayout manager to the content pane.
		setLayout(new GridLayout(3, 2));
		
		
		
		//Build the panel and add it to the frame.
		buildPanel();
		panel.setBorder(BorderFactory.createTitledBorder("Customer Order"));	
		
		//Add the panel to the frame's content pane.
		add(panel);
		
		//set the panel background to light gray.
		getContentPane().setBackground(Color.lightGray);
		
		//set the messageLabel and textField to Blue.
		messageLabel.setForeground(Color.BLUE);
		textField.setBackground(Color.BLUE);
		
		//set the totalCost label and results label to red.
		totalCostLabel.setForeground(Color.RED);
		results.setForeground(Color.RED);
		
		//Display the window.
		setVisible(true);
	}
	/**
	The buildPanel method adds a label, text field, and a button to the panel.
	*/
	
	private void buildPanel()
	{
		
		//Create a label to display instructions.
		messageLabel = new JLabel("Enter the number of items");
		
		//Creates a text field 10 characters wide.
		textField = new JTextField(10);
		
		//Create a button with the caption "Submit".
		submitButton = new JButton("Submit");
		
		
		//Create a button with the caption "Reset."
		resetButton = new JButton("Clear");
		
		//Creates a label to display the results.
		results = new JLabel("Result");
		
		//Creates a label for Total Cost.
		totalCostLabel = new JLabel("Total Cost");
		
		//Add an action listener to the button.
		submitButton.addActionListener(new ButtonListener());
		
		//Add an action listener to the button.
		resetButton.addActionListener(new ButtonListener());
		
		
		//Create a JPanel object and let the panel field reference it.
		panel = new JPanel();
		
		//Add the label, text field, and button components to the panel.
		panel.add(messageLabel);
		panel.add(textField);
		panel.add(submitButton);
		panel.add(resetButton);
		panel.add(totalCostLabel);
		panel.add(results);
	}
	/**
	SubmitButtonListener is an action listener class for the submit button
	*/
	
	private class ButtonListener implements ActionListener
	{
		/**
		the actionPerformed method executes when the user clicks on the submit button.
		@param e the event object.
		*/
	
		
		public void actionPerformed(ActionEvent e)
		{
			String input; //To hold the users input
			int numOrdered; //The number items ordered
			
			//Get the text entered by the user into the text field.
			input = textField.getText();
			
			//Convert the input to items.
			numOrdered = Integer.parseInt(input);
			double total = numOrdered * 50.00;
			
			if(e.getSource() == submitButton)
			{
			//Display the result.
			results.setText(currency.format(total));
			}
			else if(e.getSource() == resetButton)
			{
				textField.setText("");
			}
			
		}
	}

		public static void main(String[] args)
		 {
 		new Ch07Ex03();
		}

}


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8021
  • View blog
  • Posts: 31,132
  • Joined: 06-March 08

Re: BorderFactory TitledBorder

Posted 22 October 2009 - 08:56 PM

It is the JPanel() that should have the GridLayout not the frame into which you put the JPanel

//Add a GridLayout manager to the content pane.
// setLayout(new GridLayout(3, 2));

...

panel = new JPanel(new GridLayout(3,2));
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1