0

I am trying to implement CPU scheduling algorithms ... i am using java swing (not a runtime design)

This is how my main form looks: I've designed it by dragging components from swing containers/controls/menus and dropping them on a JFrame.

I've a JPanel on my main-panel of FCFS tab. I want to draw a 'Gantt Chart' on that panel (as it's shown in picture). I've read so many tutorials and implemented many examples on drawing lines but i don't know how to @Override painComponent() method in my code, because all the variables and methods are generated automatically (am using NetBeans IDE), and am unable to locate my JPanel (on which i want to draw lines).

enter image description here

I want to draw a "Gantt Chart" like this one. It will be generated automatically when user enters CPU burst for a number of processes and presses "Calculate" button. The current chart will be generated if user enters '10 ms' burst for each process.

enter image description here

In this picture i've tried to explain the sequence of components, i've added on my JFrame, along with their variable names.

enter image description here

I'm searching for the solution from last 15 days ... but am stuck in this problem ... please please help me to solve this problem.

Please please please help me...

Muhammad Shuja
  • 642
  • 5
  • 18

1 Answers1

1

The key to this is first understanding how Swing uses the MVC pattern, examined here. Just as a JTable listens to its TableModel, a JFreeChart Gantt plot listens to its own CategoryDataset, illustrated here and here.

Override setValueAt() in your TableModel and update your CategoryDataset accordingly. Both the JTable and Gantt plot will update themselves in response. If required, you can alter the GanttRenderer as shown here.

While debatable, I'd advocate hand coding over a GUI builder at this stage in development? If you go with the builder, limit the scope as outlined here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thank you so much for help ... actually my problem is that i've to draw gantt chart using `paint()` method for JFrame .. or `paintComponent()` method for other components ... – Muhammad Shuja May 09 '15 at 17:55
  • i have not studied MVC yet .. and never used JFreeChart before ... so am not aware of them .. but i seriously need help ... please help me to find solution – Muhammad Shuja May 09 '15 at 17:57
  • as you can see in first picture .. i've created two tables ... what i did was: i created them on a JPanel using Netbeans GUI ... then in events of my JTextField and Calculate-Button i used `DefaultTableModel` to fill these tables ... i want to do the same for Gantt chart ... i've a JPanel on which i want to draw my chart ... the chart will be generated when user presses the Calculate button .. so i've to write my code in actionPerformed of Calculate button ... but i don't know what to write there .. i don't understand how can i override `paintComponent()` method and use it there – Muhammad Shuja May 09 '15 at 18:03
  • i've tried making an inner-class which draws on a JPanel .. but i don't understand how to use its object to draw on my specified JPanel .. please help me – Muhammad Shuja May 09 '15 at 18:05
  • I'd go with MVC in this context. If you don't use [tag:jfreechart], you might be able to get away with letting your `TableModel` update your Gantt view component directly. "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). Too avoid being closed as too broad, please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that focuses any problem you encounter with your revised approach. – trashgod May 09 '15 at 21:59