HI there,
you can put a picture in your GUI by Using a JLabel
JLabel bg = new JLabel(new ImageIcon("test.jpg"));
then by using XYLayout you can specify the size
myFrame.getContentPane.setLayout(new XYLayout());
myFrame.getContentPane.add(bg,new XYConstrain(0,0,800,600));
the previous code shows the Picture (test.jpg) as a JLabel and it's put at the background (imagin that your frame size is(800,600))
then the XYConstrain helps you specify the location and the size
(int,int,int,int)
these are for :
1 - y location
2 - x location
3 - width (size)
4- height (size)
note : the picture should be put in the same class file.
good luck
I just want a picture anywhere on the gui really. Maybe one of the bottome corners?
I put this and it did not work.
public void buildGUI()
{
JButton btnFirst = new JButton ("First");
JButton btnPrevious = new JButton ("Previous");
JButton btnNext = new JButton ("Next");
JButton btnLast = new JButton ("Last");
JLabel bg = new JLabel(new ImageIcon ("Images/test.gif"));
JPanel p1 = new JPanel(new BorderLayout());
JPanel p = new JPanel(new GridLayout(8,2));
myFrame.getContentPane.setLayout(new XYLayout());
myFrame.getContentPane.add(bg,new XYContrain(0,0,100,200));
Here are my errors:
D:\School\IT307\Week8\Testing.java:33: cannot find symbol
symbol : class XYLayout
location: class Testing
myFrame.getContentPane.setLayout(new XYLayout());
^
D:\School\IT307\Week8\Testing.java:33: package myFrame does not exist
myFrame.getContentPane.setLayout(new XYLayout());
^
D:\School\IT307\Week8\Testing.java:34: cannot find symbol
symbol : class XYContrain
location: class Testing
myFrame.getContentPane.add(bg,new XYContrain(0,0,100,200));
^
D:\School\IT307\Week8\Testing.java:34: package myFrame does not exist
myFrame.getContentPane.add(bg,new XYContrain(0,0,100,200));
^
4 errors
Tool completed with exit code 1
THANKS!
> I put this and it did not work.
Well, thats what you get for copying code rather than taking the time to read the tutorial an understand what is going on.
XYLayout is not a standard JDK class. It comes with a GUI builder, which is why the code shouldn't be used. Its not portable.
I do have a program that I have written and I am trying to modify/add this picture. I did not post my code because someone has been taking my code and using it on their homework! So i was trying to avoid putting my entire program on my post.
Is there anyone out there willing to help me? Java, I know is easy for most of you, and that is completely great as I think its very complicated.
Any help would be appreciated. In the meantime I will keep on trying.
Thank you!
How about this.....can anyone please tell me how to put some buttons on the top of the gui instead of all of them in one row? Also I have code for an image but it changed the java cup, instead of inserting it somewhere in the gui window.
any help would be much appreciated!!!
public void buildGUI()
{
JButton btnLoadfile = new JButton("Load File");
JButton btnAdd = new JButton("Add");
JButton btnModify = new JButton("Modify");
JButton btnDelete = new JButton("Delete");
JButton btnSearch = new JButton("Search");
JButton btnSave = new JButton("Save");
JButton btnFirst = new JButton ("First");
JButton btnPrevious = new JButton ("Previous");
JButton btnNext = new JButton ("Next");
JButton btnLast = new JButton ("Last");
JPanel p1 = new JPanel(new BorderLayout());
JPanel p = new JPanel(new GridLayout(6,2));
p.add(new JLabel("DVD Title: "));
p.add(tfTitle);
p.add(new JLabel("Genre: "));
p.add(tfGenre);
p.add(new JLabel("Product Number: "));
p.add(tfProductNumber);
p.add(new JLabel("Price per Unit: "));
p.add(tfPrice);
p.add(new JLabel("Quantity on Hand: "));
p.add(tfQuantity);
p.add(new JLabel("Inventory Value: "));
p.add(tfInventoryValue);
p1.add(p,BorderLayout.NORTH);
JPanel p2 = new JPanel();
p2.add(btnLoadfile);
p2.add(btnAdd);
p2.add(btnModify);
p2.add(btnDelete);
p2.add(btnSearch);
p2.add(btnSearch);
p2.add(btnSave);
p2.add(btnFirst);
p2.add(btnPrevious);
p2.add(btnNext);
p2.add(btnLast);
p1.add(p2,BorderLayout.CENTER);
JFrame f = new JFrame();
f.setIconImage(new ImageIcon("test.gif").getImage());
f.getContentPane().add(p1,BorderLayout.NORTH);
JScrollPane sp = new JScrollPane(list);
f.getContentPane().add(sp,BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
dvds.add(new DVD("StarWars","Action","1245",10.00,5,55.00));
dvds.add(new DVD("OfficeSpace","Comedy","9821",20.00,5,105.00));
dvds.add(new DVD("Lost","Drama","9382",25.00,2,52.50));
dvds.add(new DVD("Friends","Comedy","9824",18.00,5,94.50));
dvds.add(new DVD("Superman","Action","3652",20.00,10,210.00));
dvds.add(new DVD("Batman","Action","9583",17.00,7,124.95));
dvds.add(new DVD("Cinderella","Family","2734",19.00,8,159.60));
setList();
f.setVisible(true);
f.setIconImage(new ImageIcon("Images/test.gif").getImage());
> I did not post my code because someone has been taking my code and using it on their homework!
Why would someone copy your code? Its not working, that why you are asking questions on the forum.
> I know is easy for most of you, and that is completely great as I think
You where given a link to a working example. How much more do you expect us to do?
> its very complicated.
Its two lines of code:
JLabel label = new JLabel( new IconImage("someFileHere.jpg") );
frame.getContentPane().add(label, BorderLayout.NORTH);
> Also I have code for an image but it changed the java cup
> f.setIconImage(new ImageIcon("Images/test.gif").getImage());
Well, thats what you told it to do!
> instead of inserting it somewhere in the gui window.
I don't see any code where you try to add an icon to a label and then add the icon the the content pane.
Why are you using a different file name. If one works and the other doesn't wouldn't that be a clue?
The code you posted still isn't executable.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
Someone used my code before I added this new stuff. They took it word for word.
I apologize if you are getting frustrated with me. Please know I am working very hard on this. Learning Java in just a few weeks to me is impossible so I wil take all the help i can get. I know it must seem very easy to you because you know it very well but I am just learning, maybe you can remember back to when you were learning?
Anyways i tried this code but it did not work. I didn't get any errors when compiling but nothing showed.:
JLabel label = new JLabel ( new ImageIcon("test.gif"));
f.getContentPane().add(label, BorderLayout.NORTH);
Here is my code but please keep in mind that alot of it has comments because I have not actually worked on the code for it yet. Just getting the buttons set up is where I am at.
I really wish that i could look at the examples and just have it be easy to code from it. I tried and I tried and sometimes I can get it but sometimes I just can't.
Thank you,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JFrame.*;
import java.io.*;
class Testing
{
java.util.List<DVD> dvds = new java.util.ArrayList<DVD>();
JTextField tfTitle = new JTextField(15);
JTextField tfGenre = new JTextField(15);
JTextField tfProductNumber = new JTextField();
JTextField tfPrice = new JTextField();
JTextField tfQuantity = new JTextField();
JTextField tfInventoryValue = new JTextField();
DefaultListModel dlm = new DefaultListModel();
JList list = new JList(dlm);
public void buildGUI()
{
JButton btnLoadfile = new JButton("Load File");
JButton btnAdd = new JButton("Add");
JButton btnModify = new JButton("Modify");
JButton btnDelete = new JButton("Delete");
JButton btnSearch = new JButton("Search");
JButton btnSave = new JButton("Save");
JButton btnFirst = new JButton ("First");
JButton btnPrevious = new JButton ("Previous");
JButton btnNext = new JButton ("Next");
JButton btnLast = new JButton ("Last");
JPanel p1 = new JPanel(new BorderLayout());
JPanel p = new JPanel(new GridLayout(6,2));
p.add(new JLabel("DVD Title: "));
p.add(tfTitle);
p.add(new JLabel("Genre: "));
p.add(tfGenre);
p.add(new JLabel("Product Number: "));
p.add(tfProductNumber);
p.add(new JLabel("Price per Unit: "));
p.add(tfPrice);
p.add(new JLabel("Quantity on Hand: "));
p.add(tfQuantity);
p.add(new JLabel("Inventory Value: "));
p.add(tfInventoryValue);
p1.add(p,BorderLayout.NORTH);
JPanel p2 = new JPanel();
p2.add(btnLoadfile);
p2.add(btnAdd);
p2.add(btnModify);
p2.add(btnDelete);
p2.add(btnSearch);
p2.add(btnSearch);
p2.add(btnSave);
p2.add(btnFirst);
p2.add(btnPrevious);
p2.add(btnNext);
p2.add(btnLast);
p1.add(p2,BorderLayout.CENTER);
JFrame f = new JFrame();
//f.setIconImage(new ImageIcon("test.gif").getImage());
JLabel label = new JLabel ( new ImageIcon("test.gif"));
f.getContentPane().add(label, BorderLayout.NORTH);
f.getContentPane().add(p1,BorderLayout.NORTH);
JScrollPane sp = new JScrollPane(list);
f.getContentPane().add(sp,BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
dvds.add(new DVD("StarWars","Action","1245",10.00,5,55.00));
dvds.add(new DVD("OfficeSpace","Comedy","9821",20.00,5,105.00));
dvds.add(new DVD("Lost","Drama","9382",25.00,2,52.50));
dvds.add(new DVD("Friends","Comedy","9824",18.00,5,94.50));
dvds.add(new DVD("Superman","Action","3652",20.00,10,210.00));
dvds.add(new DVD("Batman","Action","9583",17.00,7,124.95));
dvds.add(new DVD("Cinderella","Family","2734",19.00,8,159.60));
setList();
f.setVisible(true);
f.setIconImage(new ImageIcon("Images/test.gif").getImage());
btnAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
dvds.add(new DVD(tfTitle.getText(),tfGenre.getText(),tfProductNumber.getText(),
Double.parseDouble(tfPrice.getText()),
Integer.parseInt(tfQuantity.getText()),
Double.parseDouble(tfInventoryValue.getText())));
setList();
clearTextFields();
}
});
btnDelete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int index = list.getSelectedIndex();
dvds.remove(index);
setList();
clearTextFields();
}
});
btnModify.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int index = list.getSelectedIndex();
dvds.remove(index);
dvds.add(new DVD(tfTitle.getText(),tfGenre.getText(),tfProductNumber.getText(),
Double.parseDouble(tfPrice.getText()),
Integer.parseInt(tfQuantity.getText()),
Double.parseDouble(tfInventoryValue.getText())));
setList();
clearTextFields();
}
});
// btnSave.addActionListener(new ActionListener(){
//public void actionPerformed(ActionEvent ae){
//
//try
//{
//File file = new File("TestSave.txt");
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
//out.println("a line");
//out.println("another line");
//out.close();//<very important
//}
//catch(IOException ioe){ioe.printStackTrace();}
//}
// }
//dvds.add(new DVD(tfTitle.getText(),tfGenre.getText(),tfProductNumber.getText(),
//Double.parseDouble(tfPrice.getText()),
//Integer.parseInt(tfQuantity.getText()),
//Double.parseDouble(tfInventoryValue.getText())));
//setList();
//clearTextFields();
// }
//});
//btnSearch.addActionListener(new ActionListener(){
//public void actionPerformed(ActionEvent ae){
//String Search =
//JOptionPane.showInputDialog("Enter name of Movie");
//int x =0;
//for(x = 0; x < dvds.size(); x++)
//{
// DVD dvd = (DVD)dvds.get(x);
// if(dvd.title.equals(searchTitle))// or
// if(dvd.title.equalsIgnoreCase(searchTitle))
// {
//list.setSelectedIndex(x);
//break;
// }
//}
//if(x == dvds.size()) System.out "not found";
//}
//});
btnPrevious.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
int index = list.getSelectedIndex()-1;
if (index <0) index = dvds.size()-1;
list.setSelectedIndex(index);
}
});
btnNext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
//int index = list.getSelectedIndex()+1;
int index = (list.getSelectedIndex()+1) % dvds.size();
list.setSelectedIndex(index);
}
});
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent lse){
if(lse.getValueIsAdjusting() == false)
{
int index = list.getSelectedIndex();//<
if(index > -1)//<
{
DVD dvd = (DVD)dvds.get(index);//<
tfTitle.setText(dvd.title);
tfGenre.setText(dvd.genre);
tfProductNumber.setText(dvd.productNumber);
tfPrice.setText(""+dvd.price);
tfQuantity.setText(""+dvd.quantity);
tfInventoryValue.setText(""+dvd.inventoryValue);
}
}
}
});
}
public void setList()
{
dlm.clear();
for(int x = 0, y = dvds.size(); x < y; x++)
{
dlm.addElement((DVD)dvds.get(x));
}
}
public void clearTextFields()
{
tfTitle.setText("");
tfGenre.setText("");
tfProductNumber.setText("");
tfPrice.setText("");
tfQuantity.setText("");
tfInventoryValue.setText("");
tfTitle.requestFocusInWindow();
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
class DVD
{
String title;
String genre;
String productNumber;
double price;
int quantity;
double inventoryValue;
public DVD(String t,String g, String pn, double p, int q, double i)
{
title = t; genre = g; productNumber = pn; price = p; quantity = q; inventoryValue = i;
}
public String toString(){return title;}
}
Did you read the link on creating an SSCCE?
> Anyways i tried this code but it did not work. I didn't get any errors when compiling but nothing showed.:
> JLabel label = new JLabel ( new ImageIcon("test.gif"));
>f.getContentPane().add(label, BorderLayout.NORTH);
So why did you post 200 lines of code? Did you read the link on creating an SSCCE?
Post your 10 line demo program that tested the function of displaying an image in a label. I don't care about the rest of your program. So your total program is:
JFrame f = new JFrame();
JLabel label = new JLabel ( new ImageIcon("test.gif"));
f.getContentPane().add(label, BorderLayout.NORTH);
f.pack();
f.setVisible( true );
If it doesn't work then its because the program can't find your image and I can't help you with that.
> How do I put text next to the java cup?
Read the JFrame API. Thats how you learn about the methods available to you. Look at the setXXX methods
Or, have you actually bothered to execute any of the example code found in the tutorials. If you have you would notice that they all have text next to the java cup. So then you search the source code for the text and see what method they use.
Learn some simple problem solving techniques.
I have done alot of reading and researching and looking and trying example code. It's just not as easy for me as it is for you to just get it. I apologize that I am frustrating you but I am far from a java expert, this is all extremely foreign to me so It's very hard for me to grasp.
From the examples this is the code that I saw to accomplish what I am trying to do but it doesn't work:
JFrame frame = new JFrame ("DVD Inventory");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> I have done alot of reading and researching and
> looking and trying example code. It's just not as
> easy for me as it is for you to just get it. I
> apologize that I am frustrating you but I am far from
> a java expert, this is all extremely foreign to me so
> It's very hard for me to grasp.
If it is to hard for you then change careers. Just please stop whining about how hard it was for you. When I learned it I never used my ignorance as an excuse.
> JFrame frame = new JFrame ("DVD Inventory");
>
> rame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
It does work. Please, please, please read the linke posted about an SSCCE program!
> but I am far from a java expert
What does being a Java expert have to do with knowing how to ask a question and provide the information required to solve the problem?
You've already been given a link on writing an SSCCE. Yet you post a question with 2 random lines of code. Where is the SSCCE? How many time do you need to be asked for the SSCCE? The point of creating the SSCCE is to simplify the problem. Majority of time once you simplify the problem you will find your mistake.
> but it doesn't work:
That does not explain the problem or describe what you are attempting to do.
I am not a java programmer. It is a required class to get an IT degree. My speciality is not Java, I use unix everyday but not Java.
I'm a full time working mother of 4. And I'm not exaggerating when I say it's very hard to attend school but I'm toughing it out.
I really am trying my best.
Last time I was told that I posted two liines of code, i posted the rest of my code and then was told why did I post all my code.....
Please if you don't want to help me, I really do understand. But then don't post to my question please. I'm sure there are others who remember or know what it's like to experience what I am and are willing to help me. All your negativity is just discouraging me and I need all the encouragement I can get.
And I did eventually figure it out.
thank you.
Message was edited by:
llsmr777
> Last time I was told that I posted two liines of code, i posted the rest of my code and then was told why did I post all my code.....
This is your problem. You don't listen to the advice given. You where asked to post the code that demonstrated the problem. This is not 2 lines (because 2 lines is not an executable program) and it is not your entire program. It is the code that is directly related to the problem.
If you problem is displaying an image on a label. then you post the code that we can compile and execute and show the lable. We don't care about the 20 other text fields on the form. They are not related to the problem of showing an image on a label. This way instead of trying to debug a program that is 300 lines long. you have a program that is 10-15 lines of code. Which do you think is easier to debug?
> All your negativity is just discouraging me and I need all the encouragement I can get.
Quit wasting time posting comments like this and take the time to read the suggestions you've been given in the past. All I have been doing is giving you advice on problem solving, but you seem to be ignoring it.
Because Java is so foreign to me I did not know I could just send you bits of my code enough for you to compile. My mistake. That is not something I knew.
I appreciate your advice and obviously you were very irritated that I could not comprehend your adivce so easily. I was not at all ignoring any of the posts...I always try to best to understand it.
And my last comment, well each of your posts have been negative and mean. Sorry but its just how I read them and it was just very discouraging.
thank you for your time.
> And my last comment, well each of your posts have been negative and mean. Sorry but its just how I read them and it was just very discouraging
There you go again. Did you not read my comment from my previous posting?
Quit wasting time posting comments like this and take the time to read the suggestions you've been given in the past. All I have been doing is giving you advice on problem solving, but you seem to be ignoring it.
That is exactly the reason why my comments are "negative and mean", because you don't listen to what has been said before and you repeat the same mistake.
It is not just this posting either. This posting is now at 18 replies because you continue to ask question after question:
http://forum.java.sun.com/thread.jspa?threadID=780207
And this posting is at 51 replies:
http://forum.java.sun.com/thread.jspa?threadID=777408
Sometimes you have to take responsibility for you own actions and quit blaming other when you don't understand what they are suggesting.
Maybe you've noticed the number of postings I've made over time. I don't have problems making suggestions to other poster on the forum.