I have a GridBagConstraints gbcImage and a JLabel that is initialized like this:
gbcImage.gridx = 1; // column 0
gbcImage.gridy = 2; // row 2
gbcImage.ipady = 100;
gbcImage.ipadx = 100;
JLabel label = new JLabel("", null, JLabel.CENTER);
label.setOpaque(true);
label.setBackground(Color.WHITE);
panel.add(label, gbcImage);
Where panel is added to a JFrame.
So I implemented a MouseListener to the label:
public void mouseClicked(MouseEvent e) {
JFileChooser jfc = new JFileChooser();
int iRet = jfc.showOpenDialog(panel);
if (iRet == jfc.APPROVE_OPTION)
{
File file = jfc.getSelectedFile();
try
{
BufferedImage bi = ImageIO.read(file);
image = new ImageIcon(bi);
JLabel label = new JLabel("", image, JLabel.CENTER);
panel.add(label, gbcImage);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
But it didn't work. The image doesn't show in the panel at runtime.
What am I missing?
There is no need to create a new JLabel. The problem is you added a new label to the panel but its default size is (0, 0) because you didn't reavalidate() and repaint() the panel.
There is no need to create a new label.
Instead you keep a reference to the original label (like you do for the panel) and then you just replace the icon:
image = new ImageIcon(bi);
label.setIcon( image );
Related
Currently, the code is:
JFrame frame = new JFrame("App");
frame.setSize(1200, 800);//Give it a size
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//Make it go away on close
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //TU ZMIENIAC
frame.add(panel);//Add it to your frame
(...)
JPanel panelForm = new JPanel(new GridBagLayout());
panel.add(panelForm);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,10,10,10);
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.LINE_END;
(...)
panelForm.add(label_pageCount, c);
c.gridy++;
try {
URL url = new URL(JSONLists.thumbnail.get(page));
BufferedImage image = ImageIO.read(url);
JLabel label = new JLabel(new ImageIcon(image));
panelForm.add(label);
} catch (Exception exp) {
exp.printStackTrace();
}
Which results in:
Every Jlabel is put properly in its place on the grid, except the image which appears in top right corner instead of assigned place.
In this part:
try {
URL url = new URL(JSONLists.thumbnail.get(page));
BufferedImage image = ImageIO.read(url);
JLabel label = new JLabel(new ImageIcon(image));
panelForm.add(label); //here
} catch (Exception exp) {
exp.printStackTrace();
}
you add the component to the container without the GridBagConstratints. That's why the component is not being added in the proper location. So, changing it to:
panelForm.add(label,c); //Add with constraints
will fix it.
I need to put a menu in a game, so I've created a frame called menuFrame and a panel called menuPanel. I've been able to get a button and a label with text to appear on this panel, but I can't get an image to display.
Here is the bulk of my code:
try {
JPanel menuPanel = new JPanel();
BufferedImage img = ImageIO.read(this.getClass().getResource("background2.png"));
JLabel menuLabel = new JLabel(new ImageIcon(img));
menuLabel.setSize(800, 540);
menuLabel.setLocation(0, 0);
menuLabel.setVisible(true);
menuPanel.add(menuLabel);
this.add(menuPanel);
menuPanel.grabFocus();
menuPanel.requestFocusInWindow();
} catch (IOException e) {
e.printStackTrace();
}
I've been messing with it for a very long time, and the image simply wont appear. I've tried using just ImageIcon and no BufferedImage and that didn't work. I put the image in the same package as the class.
You could do something like this:
ImageIcon imgIcon = new ImageIcon("background2.png");
JLabel menuLabel = new JLabel();
label.setBounds(0, 0, x, y);
label.setIcon(imgIcon);
try {
JPanel menuPanel = new JPanel();
BufferedImage img = ImageIO.read(this.getClass().getResource("background2.png"));
JLabel menuLabel = new JLabel(new ImageIcon(img));
menuLabel.setSize(800, 540);
menuLabel.setLocation(0, 0);
menuLabel.setVisible(true);
menuPanel.add(menuLabel);
this.add(menuPanel); //This might not work, try the name of the JFrame instance
menuPanel.grabFocus();
menuPanel.requestFocusInWindow();
this.revalidate() //Use the name of the JFrame if this is not a JFrame or if this.add(menuPanel); doesnt work
} catch (IOException e) {
e.printStackTrace();
}
This is my code
private static KeyEvent e;
private static String text1 = null;
private static String text = null;
public fysikdel() {
super("Fysikformler");
setSize(700, 502);
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
setResizable(true);
setVisible(true);
}
public void init() {
JPanel main = new JPanel();
JPanel p = new JPanel();
CardLayout c1 = new CardLayout();
JScrollPane scrollpane = new JScrollPane(p);
JPanel Mekanik = new JPanel();
p.setSize(700, 502);
Mekanik.setLayout(new FlowLayout());
//637*237
ImageIcon likformigrorelsei = new ImageIcon();
JLabel likformigrorelsel = new JLabel();
ImageIcon lagesenergii = new ImageIcon();
JLabel lagesenergil = new JLabel();
ImageIcon a = new ImageIcon();
JLabel aa = new JLabel();
ImageIcon b = new ImageIcon();
JLabel bb = new JLabel();
try {
likformigrorelsei = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
likformigrorelsel.setIcon(likformigrorelsei);
try {
lagesenergii = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
lagesenergil.setIcon(lagesenergii);
try {
a = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
aa.setIcon(a);
try {
b = new ImageIcon(new URL("http://i.imgur.com/TZFkXYa.jpg"));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
bb.setIcon(b);
Mekanik.add(likformigrorelsel);
Mekanik.add(lagesenergil);
Mekanik.add(aa);
Mekanik.add(bb);
JPanel Tryck = new JPanel();
main.setLayout(new GridLayout(1,1));
p.setLayout(c1);
this.add(main);
main.add(scrollpane);
p.add(Mekanik, "1");
p.add(Tryck, "2");
c1.show(p, "1");
When I add more pictures I want them to fill up from left to right untill one row is filled, then fill the next row. At the moment it just continue to fill the first row.
If I add
scrollpane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
it just removes the horizontal scroll bar but the pictures are still ending up on one row.
I dont know what is wrong. Thanks for any help!
So you put all your images in the Mekanik panel, which uses a FlowLayout. As the java tutorial states,
The FlowLayout class puts components in a row, sized at their preferred size
which is obviously not what you want. So you'll have to change the layout used by your panel.
To my mind, the GridLayout would be a better fit for your problem.
I've been searching for an answer, but everything else comes very close, but isn't the problem I'm having.
So my main class creates a new JFrame, adds a panel as the content panel, and I add a scrollpanel to that content panel.
Now I create some of my extending JPanel classes, add them to the scroll pane and see nothing but an empty frame.
And I have checked to make sure there is indeed a list of FTPFile's
Here is the main code:
public browser(ftpHandler _FTP) {
FTP = _FTP;
Panels = new ArrayList<JPanel>();
frame = new JFrame("File Browser");
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(750, 500));
frame.setSize(frame.getPreferredSize());
frame.setMinimumSize(frame.getSize());
mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
listFiles();
frame.pack();
frame.setVisible(true);
}
public void listFiles(){
Panels.clear();
FTPFile[] list = null;
try {
list = FTP.listFiles();
} catch (Exception e) {
e.printStackTrace();
}
for(FTPFile file : list){
fileObject FO = new fileObject(file);
Panels.add(FO);
scrollPane.add(FO);
}
scrollPane.updateUI();
}
And my extended JPanel, fileObject
public class fileObject extends JPanel {
private FTPFile file;
private JLabel Label;
private ImageIcon Icon;
private int FileType;
private final int IconSize = 25;
private final Dimension panelSize = new Dimension(150, 40);
public fileObject(FTPFile FILE){
file = FILE;
FileType = file.getType();
this.setSize(panelSize);
this.setPreferredSize(panelSize);
this.setMinimumSize(panelSize);
this.setLayout(new WrapLayout());
switch (FileType){
case FTPFile.TYPE_DIRECTORY:
Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/folder.png")),IconSize);
break;
case FTPFile.TYPE_FILE:
try {
String FileExtension = file.getName().substring(file.getName().lastIndexOf(".")+1);
Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/"+FileExtension+".png")),IconSize);
} catch(Exception e) {
Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/_blank.png")),IconSize);
}
break;
case FTPFile.TYPE_LINK:
Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/_page.png")),IconSize);
break;
}
Label = new JLabel(file.getName(), Icon, JLabel.LEFT);
this.add(Label);
}
private ImageIcon resizeImage(ImageIcon II, int Size){
Image img = II.getImage();
BufferedImage resizedImage = new BufferedImage(Size, Size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0, Size, Size, null);
g2.dispose();
return new ImageIcon(resizedImage);
}
}
I got this working, it turns out I was had my panels set up wrong, they were setup like this:
JFrame
--mainPanel
----scrollPane
And I was adding my extended panel to the scrollPane
And this seemed to not work, so I added another panel inside the scrollPane and started adding my extended panels to this new panel and it started working!
Good time of the day, I'm trying to add scrolls to the GridBagLayout but definitely missing the correct way to do it.
The code:
public GUI(Map map)
{
//declare image icons and try to read them
ImageIcon missing = new ImageIcon();
ImageIcon wall = new ImageIcon();
ImageIcon floor = new ImageIcon();
//set texture for missing cases
try
{
Image tempImage = ImageIO.read(this.getClass().getResource("/resources/images/missing.png"));
missing = new ImageIcon(tempImage.getScaledInstance(16, 16, Image.SCALE_DEFAULT));
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
Image tempImage = ImageIO.read(this.getClass().getResource("/resources/images/wall.png"));
wall = new ImageIcon(tempImage.getScaledInstance(16, 16, Image.SCALE_DEFAULT));
tempImage = ImageIO.read(this.getClass().getResource("/resources/images/floor.png"));
floor = new ImageIcon(tempImage.getScaledInstance(16, 16, Image.SCALE_DEFAULT));
}
catch(Exception e)
{
e.printStackTrace();
}
Container pane = getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
for (int i=0;i<map.getMapSize();i++)
{
for (int j=0;j<map.getMapSize();j++)
{
c.gridy=i;
c.gridx=j;
if (i==0 && j==0)
{
c.weightx=1;
c.weighty=1;
}
else
{
c.weightx=0;
c.weighty=0;
}
c.gridwidth=1;
c.gridheight=1;
c.fill = GridBagConstraints.BOTH;
JLabel tile = new JLabel(missing);
if (map.getElementAt(i, j)==0)
{
tile.setIcon(wall);
}
else
{
tile.setIcon(floor);
}
pane.add(tile, c);
}
}
//c.gridx=map.getMapSize();
JScrollPane thePane = new JScrollPane();
pane.add(thePane, c);
setTitle("Game GUI");
setSize(640, 480);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
As far as I understand adding JScrollPane just after filling the pane is incorrect and of course it fails. The question is: how to add them correctly in this case? The aim overall I'm trying to achieve is to be able to scroll (move) the map in the game if it exceeds the game window size.
Thanks in advance:)
I guess the issue is that you try to add the GridBagConstraints to the JScollPane instead of adding your JPanel....
JScrollPane thePane = new JScrollPane();
pane.add(thePane, c);
try this:
JPanel myMapPanel = new Jpanel();
myMapPanel.setLayout(new GridBagLayout());
// add all the stuff to myMapPanel here (do the looping stuff here)
JScrollPane thePane = new JScrollPane(myMapPanel);
pane.add(thePane, c);
take care of this:
new JScrollPane(myMapPanel);