0

I have this code in which a Gantt Chart will be shown on another JFrame, other than the main UI frame. Is there any way such that the drawn Graphics here will be shown on a JPanel on the main UI frame?

chart= new JFrame();
final ArrayList<Task> tasks = taskList;
final int ET = processET;
chart.setSize(300, 300);
chart.setContentPane(new JPanel(){
    public void paint(Graphics g){
      for(int j=0;j &lt tasks.size();j++)
        {
            int ST = 0;
            if(j!= tasks.size()-1) ST = tasks.get(j + 1).getStartTime();
            else ST = ET;
            int Product = ST * 20;    
            g.drawRect(50,50,Product,30);
            g.drawString("p"+tasks.get(j).getPID(),(55+(tasks.get(j).getStartTime()*20)),70);
            g.drawString(""+tasks.get(j).getStartTime(),    (50+(tasks.get(j).getStartTime()*20)),100);
        }
        g.drawString(""+ET,50+(ET*20),100);
        }
    });
    chart.pack();
    chart.setLocationRelativeTo(null);
    chart.setVisible(true);

I tried this code so that it would put it on a JPanel, but obviously it did not work, hence this inquiry. Please help and thanks :)

 panel_2 = new JPanel(){ "same thing" };   
 panel.add(panel_2);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
hiakoto
  • 35
  • 1
  • 5
  • You need to add the `panel` to the frame `'chart'` and call `chart.pack();`. Then it should work. – My-Name-Is Jan 05 '14 at 10:30
  • Extract the painting from the paint method. Create a custom component, extending from JPanel, place the paint code within its paintComponent method. Add this component to what ever you want... – MadProgrammer Jan 05 '14 at 10:32
  • I am assuming that you would like to pass an image from one frame and paint it in another frame. We can achieve this using a BufferedImage object which can be passed – user3041058 Jan 05 '14 at 10:40
  • For better help sooner, post an [SSCCE](http://sscce.org/). Note that the custom painting is already done inside a `JPanel`. One approach to showing it in the original frame is to use a `CardLayout`. There are other strategies as well. If you describe more about the app. it will be easier to determine which is best. E.G. Is there a space in the original frame where you want the graph to appear? Will the user be making just one chart during the run of the app.? If not, do they need an easy way to get back to earlier charts? – Andrew Thompson Jan 05 '14 at 10:40
  • BTW - `new JPanel(){ public void paint(Graphics g){` should be something like: `new JPanel(){ @Override public void paintCompnent(Graphics g){ super.paintCompnent(Graphics g);`. 1) The `@Override` notation is handy yo make sure the method exists. 2) Custom painting in any `JComponent` should override `paintComponent(Graphics)` instead of `paint(Graphics)` & 3) Always immediately call the `super` method to make sure the other parts of the panel are painted correctly. – Andrew Thompson Jan 05 '14 at 10:46
  • I'm actually making a scheduling program as a project. I wanted this gantt chart JPanel to be included in the main UI frame, so I made a space for it, blank at first. So when the user generates the scheduling type, the gantt chart(graphics) would show on this JPanel. What I have done so far is show it on another frame. I tried the second code above to show it NOT on another frame, but on the same main UI frame (as in disregard the chart JFrame). – hiakoto Jan 05 '14 at 10:57
  • Tip: Add @MadProgrammer (or whoever - the `@` is important) to *notify* them of a new comment. But see how you go with my suggestion, and get back to us with an SSCCE if you cannot manage it. – Andrew Thompson Jan 05 '14 at 11:01

1 Answers1

2

Is there any way such that the drawn Graphics here will be shown on a JPanel on the main UI frame?

There are a number of ways. See this answer for details. I'm thinking either a CardLayout or JTabbedPane might be the easiest, given that code is already rendered in a JPanel (that is currently added to a frame for display).

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433