Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » WindowBuilder » Viewing custom components?(Is there a way to see in design mode a custom JPanel used for drawing?)
icon5.gif  Viewing custom components? [message #1425008] Tue, 16 September 2014 18:48 Go to next message
Antoine Dubuc is currently offline Antoine DubucFriend
Messages: 1
Registered: September 2014
Junior Member
Hello Everyone,

I am using WindowsBuilder and ApplicationWindows for each of my forms.

I have to write an application that draws a preview of what will be printed.

I have a custom JPanel I use to draw on and it does not show up in Design view.

Is there a way to change that?

Thank you,

Antoine



ps: The code below shows the proof of concept (yes, drawing is possible in Java) and works in Eclipse/JavaEE

package views;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.*;
import java.awt.Toolkit;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.BorderLayout;



public class appMainWindow
{
	class PdfLocation
	{
		public double xPos;
		public double yPos; 
	}
	
	class DrawCanvas extends JPanel
	{
	      @Override
	      public void paintComponent(Graphics g) 
	      {
	         super.paintComponent(g);
	         for (PdfLocation p : PdfLocations)
	         {
	        	 g.drawRect((int)p.xPos, (int)p.yPos, 35, 20);
	        	 repaint();
	         }
	      }
	}
	
	public void AddRectangle()
	{
		PdfImagesCount++;
		lblPdfCount.setText(Integer.toString(PdfImagesCount));
		
		PdfLocation rect = new PdfLocation();
		
		if (PdfLocations.isEmpty() == false)
		{
			PdfLocation spot = PdfLocations.get(PdfLocations.size() - 1);
			rect.xPos = spot.xPos + 45;
			rect.yPos = 10;
		}	
		else
		{
			rect.xPos = 10;
			rect.yPos = 10;			
		}
		PdfLocations.add(rect);				
	}

	private JFrame frame;
	public ArrayList<PdfLocation> PdfLocations = new  ArrayList<PdfLocation>();
	public int PdfImagesCount = 0;

	
	public static final int CANVAS_HEIGHT = 700;
	public static final int CANVAS_WIDTH = 1000;
	
	private DrawCanvas canvas;
	private JLabel lblPdfCount;
	private JButton btnOk;
	
	public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { appMainWindow window = new appMainWindow(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }

	public appMainWindow() 
	{ 
		
		// Set up a custom drawing JPanel
	    canvas = new DrawCanvas();
	    canvas.setBackground(Color.WHITE);
	    canvas.setBounds(150, 25, CANVAS_WIDTH, CANVAS_HEIGHT);
		initialize(); 
	}

	private void initialize() 
	{
		frame = new JFrame();
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(appMainWindow.class.getResource("/icons/appIcon2.png")));
		frame.setBounds(100, 100, 1200, 728);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(true);
		frame.setTitle("Shaareable Apps Presents ~ Pdf Imposition Monster");
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnMenu = new JMenu("Menu");
		menuBar.add(mnMenu);
		
		JMenuItem mntmTest = new JMenuItem("test");
		mntmTest.setIcon(new ImageIcon(appMainWindow.class.getResource("/icons/Blue-Monster-icon.png")));
		mnMenu.add(mntmTest);
		frame.getContentPane().setLayout(null);
		
		lblPdfCount = new JLabel("");
		lblPdfCount.setBounds(92, 325, 61, 16);
		frame.getContentPane().add(lblPdfCount);
		
		
		
		btnOk = new JButton("ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				AddRectangle();
				frame.repaint();
			}
		});
		btnOk.setBounds(22, 172, 75, 29);
		frame.getContentPane().add(btnOk);
		
		frame.getContentPane().add(canvas);
	}
}
Re: Viewing custom components? [message #1425202 is a reply to message #1425008] Wed, 17 September 2014 01:45 Go to previous message
Eric Clayberg is currently offline Eric ClaybergFriend
Messages: 979
Registered: July 2009
Location: Boston, MA
Senior Member
WindowBuilder is only going to display the static representation of any window/panel you are editing. None of your custom drawing code is executed in that context (intentionally).
Previous Topic:WindowBuilder crashes.
Next Topic:Eclipse freezing after inserting components in windows builder design mode
Goto Forum:
  


Current Time: Fri Apr 19 07:14:36 GMT 2024

Powered by FUDForum. Page generated in 0.02013 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top