2 Replies - 117 Views - Last Post: 13 January 2012 - 05:37 AM Rate Topic: -----

#1 Cpp_noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 06-July 09

Adding componets to frame issue

Posted 12 January 2012 - 06:51 PM

im trying to add a new JTable to my frame that alrady contains a Toolbar , but when i do the Toolbar vanises and i cant seem to reposition the JTable

heres my code

public abstract class mySystem
{ 



	public static void CreateGUI() 
	{
		JFrame frame = new JFrame();

	        frame.add(new ToolBar()); 
	        frame.add( new ListView());

	       
	        frame.pack();
	        frame.setLocationRelativeTo(null);
	        //frame.setResizable(false);
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        frame.setVisible(true);
	}	
	
}



Toolbar Class

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class ToolBar extends JPanel implements ActionListener 
{

	private static final long serialVersionUID = 233436L;
	static final private String xSAVE = "Save";
	static final private String xOPEN = "Open";
	static final private String xFCTR = "Flight Control";
	static final private String xARR = "Flight Arrivals";
	static final private String xDEP = "Flight Departures";
	static final private String xABOUT = "About";
	public ToolBar() 
	{
	    super(new BorderLayout());
	
	    JToolBar toolBar = new JToolBar();
	    toolBar.setFloatable(false);
	    toolBar.setRollover(true);
	  
        setPreferredSize(new Dimension(500, 40));
	    addButtons(toolBar);
	    add(toolBar, BorderLayout.PAGE_START);
	}

	
	protected void addButtons(JToolBar toolBar) 
	{
	    JButton button = null;
	
	    button = makeNavigationButton("Save", xSAVE,"Save data to disk","Save");
	    toolBar.add(button);
	    button = makeNavigationButton("Open", xOPEN,"Load data from disk","Open");
	    toolBar.add(button);
	    
	    toolBar.addSeparator();
	    
	    button = makeNavigationButton("FControl", xFCTR,"Flight Control Manager","Flight Control");
	    toolBar.add(button);
	    button = makeNavigationButton("FArrivals", xARR,"Flight Arrivals Manager","Flight Arrivals");
	    toolBar.add(button);
	    button = makeNavigationButton("FDepartures", xDEP,"Flight Departures Manager","Flight Departures");
	    toolBar.add(button);
	    
	    toolBar.addSeparator();
	    button = makeNavigationButton("About", xABOUT,"Application Information","About");
	    toolBar.add(button);
	    
	}
	
	protected JButton makeNavigationButton(String imageName,String actionCommand,String toolTipText,String altText) {
	
	    String imgLocation = "icons/" + imageName + ".gif";
	    URL imageURL = mySystem.class.getResource(imgLocation);
	    
	
	    JButton button = new JButton();
	    button.setActionCommand(actionCommand);
	    button.setToolTipText(toolTipText);
	    button.addActionListener(this);
	
	    if (imageURL != null) 
	    {                     
	        button.setIcon(new ImageIcon(imageURL, altText));
	    } 
	    else 
	    {                                    
	        button.setText(altText);
	        System.err.println("Resource not found: "
	                           + imgLocation);
	    }
	
	    return button;
	}
	
	public void actionPerformed(ActionEvent e)
	{
	    String cmd = e.getActionCommand();
	    @SuppressWarnings("unused")
		String description = null;
	
	    if (xSAVE.equals(cmd)) 
	    { 
	        description = "taken you to the previous <something>.";
	    } 
	    else if (xOPEN.equals(cmd))
	    { 
	        description = "taken you up one level to <something>.";
	    } 
	    else if (true) 
	    {
	        description = "taken you to the next <something>.";
	    }
	}


}



TableView Class
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

public class ListView extends JPanel 
{ 
	public ListView() {
		super(new BorderLayout());
		 
	    Vector<String> rowOne = new Vector<String>();
	    rowOne.addElement("Row1-Column1");
	    rowOne.addElement("Row1-Column2");
	    rowOne.addElement("Row1-Column3");
	    
	    Vector<String> rowTwo = new Vector<String>();
	    rowTwo.addElement("Row2-Column1");
	    rowTwo.addElement("Row2-Column2");
	    rowTwo.addElement("Row2-Column3");
	    
	    Vector<Vector> rowData = new Vector<Vector>();
	    rowData.addElement(rowOne);
	    rowData.addElement(rowTwo);
	    
	    Vector<String> columnNames = new Vector<String>();
	    columnNames.addElement("Column One");
	    columnNames.addElement("Column Two");
	    columnNames.addElement("Column Three");
	    JTable table = new JTable(rowData, columnNames);

	    JScrollPane scrollPane = new JScrollPane(table);
	    scrollPane.getViewport().setViewPosition(new Point(0,0));
	    
	    add(scrollPane, BorderLayout.AFTER_LAST_LINE);
        

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Adding componets to frame issue

#2 pbl  Icon User is offline

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

Reputation: 8027
  • View blog
  • Posts: 31,161
  • Joined: 06-March 08

Re: Adding componets to frame issue

Posted 12 January 2012 - 07:20 PM

By default a JFrame has a BorderLayout
A BorderLayout has 5 regions NORTH, SOUTH, EAST, WEST, CENTER
All of these regions can only hold ONE JComponent. If you add a second JComponent to the same region it simply replace the one that was there before

frame.add(comp) is equivalent to frame.add(comp, BorderLayout.CENTER);
so you add your ToolBar to the CENTER region and replace it by your ListView the following statement
Was This Post Helpful? 1
  • +
  • -

#3 Cpp_noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 06-July 09

Re: Adding componets to frame issue

Posted 13 January 2012 - 05:37 AM

thanks for the help!!! solved now :)))

This post has been edited by Cpp_noob: 13 January 2012 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1