Reputation:
i am writing up a project just as part of my experiments... (for everyone that has already help with my other questions, thank you for very much.)
Alright i have one file that already works.. both of my source files compiled correctly i have two jpanel's that are overridden so i can change their paintcomponents. One i use as a sort of background image for the application this is the one that works. the problem is with my second panel i am trying to add, for some reason when i start the application, it is either not showing above the first JPanel or it is not showing at all..
here is my code..
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class COS extends JPanel implements ActionListener{
static JFrame f=new JFrame();
static Image bgImage=null;
static String message="";
JButton chbg=new JButton("change background");
public COS(){
chbg.setBounds(10,10,150,25);
chbg.addActionListener(this);
add(chbg);
}
public void paintComponent(Graphics g){
if(bgImage!=null){
g.drawImage(bgImage,0,0,this);
}
else{
g.drawString(message,40,40);
}
}
public static void loadbg(){
try{
String xmlpath="background.xml";
SAXBuilder builder=new SAXBuilder();
Document xdoc=builder.build(xmlpath);
String fimg="";
fimg=xdoc.getRootElement().getChild("bgimage").getText();
getFileImage(fimg);
} catch(Exception e){
message="File load failed: "+e.getMessage();
}
}
public static void getFileImage(String filein) throws IOException, InterruptedException{
FileInputStream in=new FileInputStream(filein);
byte[] b=new byte[in.available()];
in.read(b);
in.close();
bgImage=Toolkit.getDefaultToolkit().createImage(b);
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
JFileChooser jfc=new JFileChooser();
if(source==chbg){
int returnVal=jfc.showOpenDialog(null);
if(returnVal==JFileChooser.APPROVE_OPTION){
File file=jfc.getSelectedFile();
String fileone=file.getName();
changebg(fileone);
}
}
}
public void changebg(String filein){
try{
getFileImage(filein);
saveDefaultImage(filein);
repaint();
} catch(IOException e){
} catch(InterruptedException ie){
}
}
public void saveDefaultImage(String filein){
try{
String xmlpath="background.xml";
SAXBuilder builder=new SAXBuilder();
Document xdoc=builder.build(xmlpath);
xdoc.getRootElement().removeChild("bgimage");
xdoc.getRootElement().addContent(new Element("bgimage").setText(filein));
FileOutputStream fos=new FileOutputStream(xmlpath);
XMLOutputter out=new XMLOutputter();
out.output(xdoc, fos);
fos.flush();
fos.close();
} catch(Exception e){
}
}
public static void main(String[] args){
COS newcos=new COS();
COSmp cmp=new COSmp();
cmp.setBounds(720,0,25,600);
cmp.setLayout(null);
loadbg();
f.setSize(825,640);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);
f.setLayout(null);
newcos.setBounds(5,5,800,600);
newcos.setOpaque(false);
newcos.setLayout(null);
f.setLocation(10,5);
f.getContentPane().add(newcos);
f.add(cmp);
f.setVisible(true);
}
}
my second source file..
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class COSmp extends JPanel implements ActionListener{
public COSmp(){
JLabel whatisthis=new JLabel("I am going to be a start menu i think");
add(whatisthis);
}
public void actionPerformed(ActionEvent e){
}
}
the second one is very simple but all of it's methods are in the second the setbounds, add, etc..
i cannot seem to get it to display, even if i set the first one "newcos" is what i call the first one, to not display..
could someone help me? if i didn't explain well enough please tell me and i will try again.
also i just thought about this, the xml file, background.xml
background2.png
Upvotes: 0
Views: 306
Reputation: 64026
You don't need the complication of two panels; you can just add components to the one which is painting the background.
Also, you need to install a LayoutManager (setLayout()) to layout your components.
EDIT (for comment): Yes, you have a setLayout(null), but that removes the layout manager. When you add your label to cmp, it has no layout manager so that component is unsized and unlocated (it bounds are probably 0,0,0,0), and therefore not visible.
I say again, you need to research how to use layout managers and use them - follow the Swing tutorial on layout - it's the best way to construct a UI (manual layout is absolutely not the way to go).
Sun Tutorials: Main Tutorial Page - Creating a GUI With Swing - Layout Tutorial
BTW, pack didn't work because it asks the layout manager to size the components and there is none in your case.
Lastly, look into a good table-based layout manager - there are several available for free on the Web. They make container layout much easier than using just the layouts built into Java. I have one which is completely free, called MatrixLayout.
Upvotes: 1
Reputation: 44821
You can't see anything because:
You need to learn about layout managers and how to use them, the code you have here is untenable.
Upvotes: 1