Image problem in Scroll Pane

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2006
Posts: 3
Reputation: maczu.pikczu is an unknown quantity at this point 
Solved Threads: 0
maczu.pikczu maczu.pikczu is offline Offline
Newbie Poster

Image problem in Scroll Pane

 
0
  #1
Jan 15th, 2006
Hi I have this little app in java that looks like that :
http://img399.imageshack.us/img399/2046/wardrobe6cd.jpg

Its a JFrame a ScrollPane inside and a JTable in it. I'd like to have an image displayed uder the table but I have no clue how to do this (I'm a begginer whit java)
the code looks like this
  1. public class WardrobeFrame extends JFrame implements ChangeListener{
  2.  
  3. private WardrobeTableModel _wardrobeTableModel;
  4. private JTable _wardrobeTable;
  5.  
  6. public WardrobeFrame(Wardrobe wardrobe) {
  7. super("My Wardrobe");
  8. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9.  
  10. _wardrobeTableModel = new WardrobeTableModel(wardrobe);
  11. _wardrobeTable = new JTable(_wardrobeTableModel);
  12.  
  13. Clock.getClock().addChangeListener(this);
  14.  
  15. initGUI();
  16. pack();
  17. setLocationRelativeTo(null);
  18. }
  19.  
  20. private void initGUI() {
  21. getContentPane().setLayout(new BorderLayout(5,5));
  22.  
  23. // setting main GUI components
  24. _wardrobeTable.setRowHeight(45);
  25. _wardrobeTable.getColumnModel().getColumn(0).setCellRenderer(
  26. new ClothesTypeTableRenderer());
  27. _wardrobeTable.getColumnModel().getColumn(6).setCellRenderer(
  28. new TrueFalseTableRenderer());
  29.  
  30. JScrollPane listScroller = new JScrollPane(_wardrobeTable);
  31. getContentPane().add(listScroller,BorderLayout.CENTER);
  32.  
  33. JPanel actionPanel = new JPanel();
  34. actionPanel.setLayout(new GridLayout(9, 1));
  35. getContentPane().add(actionPanel,BorderLayout.SOUTH);
  36.  
  37. // creating action buttons
  38. JButton createBtn = new JButton("Create new item");
  39. actionPanel.add(createBtn);
  40.  
  41. JButton putOnBtn = new JButton("Put on");
  42. actionPanel.add(putOnBtn);
  43.  
  44. JButton takeOffBtn = new JButton("Take off");
  45. actionPanel.add(takeOffBtn);
  46.  
  47. JButton sendToLaundryBtn = new JButton("Send to laundry");
  48. actionPanel.add(sendToLaundryBtn);
  49.  
  50. JButton removeBtn = new JButton("Remove selected item");
  51. actionPanel.add(removeBtn);
  52.  
  53. JButton removeAllBtn = new JButton("Remove all clothes");
  54. actionPanel.add(removeAllBtn);
  55.  
  56. JButton saveBtn = new JButton("Save wardrobe content");
  57. actionPanel.add(saveBtn);
  58. saveBtn.setForeground(Color.BLUE);
  59.  
  60. JButton loadBtn = new JButton("Load wardrobe content");
  61. actionPanel.add(loadBtn);
  62. loadBtn.setForeground(Color.BLUE);
  63.  
  64. JButton closeBtn = new JButton("Close wardrobe");
  65. closeBtn.setFont(new Font(closeBtn.getFont().getFamily(), Font.BOLD, 16));
  66. closeBtn.setForeground(Color.RED);
  67. actionPanel.add(closeBtn);
  68.  
  69. // setting listeners
  70. createBtn.addActionListener(new ActionListener(){
  71. public void actionPerformed(ActionEvent e) {
  72. actionCreateAndPut();
  73. }
  74. });
  75.  
  76. putOnBtn.addActionListener(new ActionListener(){
  77. public void actionPerformed(ActionEvent e) {
  78. actionPutOnSelected();
  79. }
  80. });
  81.  
  82. takeOffBtn.addActionListener(new ActionListener(){
  83. public void actionPerformed(ActionEvent e) {
  84. actionTakeOffSelected();
  85. }
  86. });
  87.  
  88. sendToLaundryBtn.addActionListener(new ActionListener(){
  89. public void actionPerformed(ActionEvent e) {
  90. actionSendSelectedToLaundry();
  91. }
  92. });
  93.  
  94. removeBtn.addActionListener(new ActionListener(){
  95. public void actionPerformed(ActionEvent e) {
  96. actionRemoveSelected();
  97. }
  98. });
  99.  
  100. removeAllBtn.addActionListener(new ActionListener(){
  101. public void actionPerformed(ActionEvent e) {
  102. actionRemoveAll();
  103. }
  104. });
  105.  
  106. saveBtn.addActionListener(new ActionListener(){
  107. public void actionPerformed(ActionEvent e) {
  108. actionSaveToFile();
  109. }
  110. });
  111.  
  112. loadBtn.addActionListener(new ActionListener(){
  113. public void actionPerformed(ActionEvent e) {
  114. actionLoadFromFile();
  115. }
  116. });
  117.  
  118. closeBtn.addActionListener(new ActionListener(){
  119. public void actionPerformed(ActionEvent e) {
  120. closeApp();
  121. }
  122. });
  123. }
  124.  
  125. //setting actions
  126. private void actionCreateAndPut() {
  127. CreateDialog createDialog = new CreateDialog(this);
  128. createDialog.setVisible(true);
  129.  
  130. if (!createDialog.isCanceled()) {
  131. IWearable item = createDialog.getItem();
  132. _wardrobeTableModel.putItem(item);
  133. }
  134. }
  135. /**
  136. * pobiera warunki z tableModel odnosnie metody putOn jesli wszystko sie zgadza odswierza tabele
  137. */
  138. private void actionPutOnSelected() {
  139. int itemIndex = _wardrobeTable.getSelectedRow();
  140. if (itemIndex > -1 && itemIndex < _wardrobeTableModel.getRowCount()) {
  141. int result = _wardrobeTableModel.putOn(itemIndex);
  142. if (result == 1) {
  143. JOptionPane.showMessageDialog(this,
  144. "The item is already in use.","Error", JOptionPane.ERROR_MESSAGE);
  145. }else if (result == 2){
  146. JOptionPane.showMessageDialog(this,
  147. "The item has not returned from loundry.Pleas wait.",
  148. "Error", JOptionPane.ERROR_MESSAGE);
  149. }else if (result == 3){
  150. stateChanged (null); // zeby obrazek true/false startowal odrazu
  151. }
  152. }
  153. }
  154.  
  155. private void actionTakeOffSelected() {
  156. int itemIndex = _wardrobeTable.getSelectedRow();
  157. if (itemIndex > -1 && itemIndex < _wardrobeTableModel.getRowCount()) {
  158. boolean result = _wardrobeTableModel.takeOffItem(itemIndex);
  159. if (result == false)
  160. JOptionPane.showMessageDialog(this,
  161. "The item is not in use.","Error", JOptionPane.ERROR_MESSAGE);
  162. }
  163. }
  164.  
  165. public void actionSendSelectedToLaundry() {
  166. int itemIndex = _wardrobeTable.getSelectedRow();
  167. if (itemIndex > -1 && itemIndex < _wardrobeTableModel.getRowCount()) {
  168. int result = _wardrobeTableModel.sendItemToLaundryConditions(itemIndex);
  169. if (result == 1) {
  170. JOptionPane.showMessageDialog(this,
  171. "You need to take this item off first!", "Error",
  172. JOptionPane.ERROR_MESSAGE);
  173. }else if (result == 2){
  174. JOptionPane.showMessageDialog(this,
  175. "This item is already in laundry!", "Error",
  176. JOptionPane.ERROR_MESSAGE);
  177. }else if (result == 3){
  178. int n = JOptionPane.showConfirmDialog(this,
  179. "This item seems clean! Would you like to send it to the laundry?","Question",
  180. JOptionPane.YES_NO_OPTION);
  181. if (n == JOptionPane.YES_OPTION) {
  182. _wardrobeTableModel.sendItemToLaundry(itemIndex);
  183. JOptionPane.showMessageDialog(this,
  184. "Your clothes have been sent to the laundry!", "Succes",
  185. JOptionPane.INFORMATION_MESSAGE);
  186. } else if (n == JOptionPane.NO_OPTION) {
  187. }
  188. }
  189. else if (result == 4){
  190. _wardrobeTableModel.sendItemToLaundry(itemIndex);
  191. JOptionPane.showMessageDialog(this,
  192. "Your clothes have been sent to the laundry!", "Succes",
  193. JOptionPane.INFORMATION_MESSAGE);
  194. }
  195. }
  196. }
  197.  
  198. private void actionRemoveSelected() {
  199. int itemIndex = _wardrobeTable.getSelectedRow();
  200. if (itemIndex > -1 && itemIndex < _wardrobeTableModel.getRowCount()) {
  201. boolean result = _wardrobeTableModel.removeItem(itemIndex);
  202. if (result == false) {
  203. JOptionPane.showMessageDialog(this,
  204. "You need to take this item off first!", "Error",
  205. JOptionPane.ERROR_MESSAGE);
  206. }
  207. }
  208. }
  209.  
  210. private void actionRemoveAll() {
  211. int n = JOptionPane.showConfirmDialog(this,
  212. "This will remove ALL of the items. Continue?","Question",
  213. JOptionPane.YES_NO_OPTION);
  214. if (n == JOptionPane.YES_OPTION) {
  215. _wardrobeTableModel.removeAll();
  216. } else if (n == JOptionPane.NO_OPTION) {
  217. }
  218. }
  219.  
  220. private void actionSaveToFile() {
  221. try {
  222. final JFileChooser fc = new JFileChooser();
  223. int returnVal = fc.showOpenDialog(this);
  224. if (returnVal == JFileChooser.APPROVE_OPTION){
  225. File file = fc.getSelectedFile();
  226. FileOutputStream fout = new FileOutputStream(file);
  227. ObjectOutputStream oout = new ObjectOutputStream(fout);
  228. oout.writeObject(_wardrobeTableModel.getWardrobe());
  229. oout.close();
  230. JOptionPane.showMessageDialog(this, "Wardrobe saved");
  231. }else {
  232. }
  233. } catch (Exception e) {
  234. JOptionPane.showMessageDialog(this,
  235. "Cannot save wardrobe: "+e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
  236. e.printStackTrace();
  237. }
  238. }
  239.  
  240. private void actionLoadFromFile(){
  241. try {
  242. final JFileChooser fc = new JFileChooser();
  243. int returnVal = fc.showOpenDialog(this);
  244. fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
  245. if (returnVal == JFileChooser.APPROVE_OPTION){
  246. File file = fc.getSelectedFile();
  247. FileInputStream fin = new FileInputStream(file);
  248. ObjectInputStream oin = new ObjectInputStream(fin);
  249. _wardrobeTableModel.setWardrobe( (Wardrobe) oin.readObject());
  250. oin.close();
  251. JOptionPane.showMessageDialog(this, "Wardrobe loaded");
  252. }else {
  253. }
  254.  
  255. }catch (EOFException eof) {
  256. JOptionPane.showMessageDialog(this, "Cannot load wardrobe: Unexpected end of file",
  257. "Error", JOptionPane.ERROR_MESSAGE);
  258. eof.printStackTrace();
  259. } catch (Exception e) {
  260. JOptionPane.showMessageDialog(this, "Cannot load wardrobe: "+e.getMessage(),
  261. "Error", JOptionPane.ERROR_MESSAGE);
  262. e.printStackTrace();
  263. }
  264. }
  265. private void closeApp() {
  266. int n = JOptionPane.showConfirmDialog(this,
  267. "Do you really want to quit?","Question",JOptionPane.YES_NO_OPTION);
  268. if (n == JOptionPane.YES_OPTION) {
  269. System.exit(0);
  270. } else if (n == JOptionPane.NO_OPTION) {
  271. }
  272. }
  273.  
  274. public void stateChanged (ChangeEvent arg0){
  275. repaint();
  276. }
  277. }

the question is simple how do I load an image as a backgorund

I'd appriciate any help
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Image problem in Scroll Pane

 
0
  #2
Jan 15th, 2006
Personally, I would create a seperate class that extends JPanel and add it to the other class:


  1.  
  2. import javax.swing;
  3.  
  4. public class ImagePanel extends JPanel
  5. {
  6. public ImagePanel
  7. {
  8. }
  9.  
  10. public void paintComponent(Graphics g)
  11. {
  12. }
  13.  
  14. }


Then add it to your normal class:

  1.  
  2. public class WardrobeFrame blah blah blah
  3. {
  4. ImagePanel ip;
  5.  
  6. public WardrobeFrame()
  7. {
  8. ip = new ImagePanel();
  9. add(ip, BorderLayout.CENTER);
  10. }
  11. }


Something like that
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Image problem in Scroll Pane

 
0
  #3
Jan 15th, 2006
You may also want to use some nested JPanels in your program.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 3
Reputation: maczu.pikczu is an unknown quantity at this point 
Solved Threads: 0
maczu.pikczu maczu.pikczu is offline Offline
Newbie Poster

Re: Image problem in Scroll Pane

 
0
  #4
Jan 15th, 2006
ok so I've made an ImagePanel class and put this code below to my WardrobeFrame now it looks like this:
  1. public WardrobeFrame(Wardrobe wardrobe) {
  2. super("My Wardrobe");
  3.  
  4. ip = new ImagePanel();
  5. add(ip, BorderLayout.CENTER);
  6.  
  7. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  8.  
  9. _wardrobeTableModel = new WardrobeTableModel(wardrobe);
  10. _wardrobeTable = new JTable(_wardrobeTableModel);
  11.  
  12. Clock.getClock().addChangeListener(this);
  13.  
  14. initGUI();
  15. pack();
  16. setLocationRelativeTo(null);
  17. }

and now its time for a stupid quesstion... how do I load an image?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Image problem in Scroll Pane

 
0
  #5
Jan 15th, 2006
I expected that question

It's something like this:

INSIDE YOUR PAINTCOMPONENT METHOD:


g.drawImage(image, x, y, this);

or


g.drawImage(image, x, y, null);


I can't remember the last part, but I know it's one of the two.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 786
Reputation: Phaelax is on a distinguished road 
Solved Threads: 39
Phaelax Phaelax is offline Offline
Master Poster

Re: Image problem in Scroll Pane

 
0
  #6
Jan 15th, 2006
Did my reply to your email help out any?
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 3
Reputation: maczu.pikczu is an unknown quantity at this point 
Solved Threads: 0
maczu.pikczu maczu.pikczu is offline Offline
Newbie Poster

Re: Image problem in Scroll Pane

 
0
  #7
Jan 17th, 2006
yeah thx u guys for the help everything is now up and running
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC