Hi,

I'm working on a dissertation based around graph visualization. I need to make a GUI for my program but I am having trouble getting the JPanels to display what is required. My code so far:

//Class to create the GUI
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

public class GUI extends JFrame
{
	private GridBagLayout layout;
	private GridBagConstraints constraints;
	private JButton upload;
	private JButton input;
	private JButton edit;
	private JButton clear;
	private JButton save;
	private JButton sunflower;
	private JButton type;
	private JButton mini;
	private JButton saveMini;
	private JButton saveAll;
	private JPanel graph;
	private JPanel settings;
	private JPanel miniGraph;
	
	//Set up the GUI
	public GUI()
	{
		super( "TITLE OF PROGRAM" ); // Set title
		layout = new GridBagLayout(); // Instantiate layout
		setLayout( layout ); // Set frame layout
		constraints = new GridBagConstraints(); //Instantiate constraints
		
		// Create buttons
		upload = new JButton( "Upload Graph Data" );
		input = new JButton( "Input Graph Data" );
		edit = new JButton( "Edit Graph Data" );
		clear = new JButton( "Clear Graph" );
		save = new JButton( "Save Graph image" );
		sunflower = new JButton( "Sunflower Settings" );
		type = new JButton( "Mini-Graph Type" );
		mini = new JButton( "Mini-Graph settings" );
		saveMini = new JButton( "Save Mini-Graph image" );
		saveAll = new JButton( "Save Project" );
		
		// Create Panels
		graph = new JPanel( layout );
		settings = new JPanel( layout );
		miniGraph = new JPanel( layout );
		
		JLabel graphLabel = new JLabel( "Graph" );
		graph.add( graphLabel );
		JLabel miniGraphLabel = new JLabel( "Mini-Graph" );
		miniGraph.add( miniGraphLabel );
		final JLabel settingsLabel = new JLabel("Settings" );
		
		//Set weights and anchor
		constraints.weightx = 1;
		constraints.weighty = 1;
		constraints.anchor = GridBagConstraints.NORTHWEST;
		
		//Add buttons and allow for horizontal resize
		//addComponent() declared at lines !!!!!!!!!!!!!!!!!!!!!!
		constraints.fill = GridBagConstraints.HORIZONTAL;
		addComponent( upload, 0, 0, 1, 1 );
		addComponent( input, 1, 0, 1, 1 );
		addComponent( edit, 2, 0, 1, 1 );
		addComponent( clear, 3, 0, 1, 1 );
		addComponent( save, 4, 0, 1, 1 );
		//Change weighty to place second row of buttons under first row
		constraints.weighty = 100;
		addComponent( sunflower, 0, 1, 1, 1 );
		addComponent( type, 1, 1, 1, 1 );
		addComponent( mini, 2, 1, 1, 1 );
		addComponent( saveMini, 3, 1, 1, 1 );
		addComponent( saveAll, 4, 1, 1, 1 );
		
		//Add listeners and events for all buttons
		upload.addActionListener(
			new ActionListener() //Inner class
			{
				public void actionPerformed( ActionEvent event )
				{
					settings.removeAll();
					settings.add( settingsLabel );
					settings.repaint();
				}
			}
		);
		
		//Add panels and allow resize in both directions
		constraints.fill = GridBagConstraints.BOTH;
		addComponent( graph, 0, 2, 3, 2 );
		addComponent( settings, 3, 2, 2, 1 );
		addComponent( miniGraph, 3, 3, 2, 1 );
	} // End GUI constructor
	
	//addComponent method
	private void addComponent( Component component, int column, int row, int width, int height )
	{
		constraints.gridx = column; //Set gridx
		constraints.gridy = row; //Set gridy
		constraints.gridwidth = width; //Set gridwidth
		constraints.gridheight = height; //Set gridheight
		layout.setConstraints( component, constraints ); //Set constraints
		add( component ); //Add component to frame
	} // End addComponent method
} // End GUI class

The labels have nothing to do with the program I'm just using them to test that my panels are working. The GUI opens fine ( the layout needs a bit of tweaking but I'm ignoring that for now ) but when I click the "upload" button nothing happens. I have tried all I can think of along with a lot of online searches and book reading but the only help anyone gives is to use setVisible on what needs to be displayed. I would prefer to avoid using setVisible as each button clicked has it's own set of components that need to be displayed in that panel.

Also, when adding these components to the panel is it possible to use a separate class file for handling that code rather than keeping all of the code in this file.

Recommended Answers

All 5 Replies

put this two in GUI constructor...

this.setVisible(true);
 
 this.setSize(500, 500);

then reply ...

I have that in my run method. Just a generic main method so I didn't think to include it:

//Main Class for running the application

import javax.swing.JFrame;

public class run
{
	public static void main( String args[] )
	{
		GUI gui = new GUI();
		gui.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		gui.setSize( 800, 600 ); //Set initial size
		gui.setVisible( true ); //Display
	} // End main method
} // End class

before repaint add this....

settings.updateUI();

Thanks. That works fine :)

hi,
i read ur code and although i didn't think that you are using the layout correctly as you use the addComponent method with panels and (buttons)
but i'll give u a hint that u can use in the actionPerformed method:
if you want to make the panel "refresh" it self to update the visibility of components on it you can use the following methods:
panel.validate();
panel.doLayout();
They would make the desired action of removing elements from the panel.
i hope it works for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.