Trying to add a photo to first pane, but no go
I am trying to put an image on the first content pane, but when I do it comes up the tab of all the windows and makes the program look crazy. Can not figure this one out, any help would be great. Thanks in advance.
Code:
package classes_objects;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class ClearBluePools extends JFrame implements ActionListener
{
// variables
ImageIcon icon;
private JTextField txfLength;
private JTextField txfWidth;
private JTextField txfDepth;
private JTextField txfOutput;
private JButton[] btn = new JButton[3];
private JLabel[] lbl = new JLabel[3];
private JTextField[] txt = new JTextField[3];
private JTextField[] theDate = new JTextField[1];
private JPanel[] pnl = new JPanel[3];
private JPanel contentPane0;
private JPanel contentPane1;
private JRadioButton[] radio = new JRadioButton[2];
private ButtonGroup group;
private JTabbedPane tabbedPane;
public ClearBluePools(){String[] names = {"Length","Width","Depth"};
// pane 1 Date and time and company name xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
JLabel[] lbl1 = new JLabel [3];
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
lbl1[1] = new JLabel("Time and Date: " + dateFormat.format(date));
lbl1[2] = new JLabel("Clear Blue Pools");
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
// set image on pane
icon = new ImageIcon("C:/Users/Reyno/Desktop/COMP328 - 2012 Summer 'B'/Clear Blue Pools/bin/classes_objects/images/poolwater.jpg");
//String path = "C:/Users/Reyno/Desktop/COMP328 - 2012 Summer 'B'/Clear Blue Pools/in/classes_objects/images/poolwater.jpg";
JLabel iconLabel = new JLabel(icon);
ImageIcon icon1 = new ImageIcon();
// pane 2 Pool Calculations xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
for (int i = 0; i < 3; i++)
{
btn[i] = new JButton("Calculate");
lbl[i] = new JLabel(names[i]);
txt[i] = new JTextField(5);
pnl[i] = new JPanel(new FlowLayout());
}
// first decide how you want final layout to look
// row 1
radio[0] = new JRadioButton("Play");
radio[1] = new JRadioButton("Diving");
//pnl[0].setLayout(new FlowLayout(FlowLayout.LEFT, 135, 0));
pnl[0].add(radio[0]);
pnl[0].add(radio[1]);
// row2
//pnl[1].setLayout(new FlowLayout(FlowLayout.LEFT));
pnl[1].add(lbl[0]);
pnl[1].add(txt[0]);
pnl[1].add(lbl[1]);
pnl[1].add(txt[1]);
pnl[1].add(lbl[2]);
pnl[1].add(txt[2]);
// row 3
//pnl[2].setLayout(new FlowLayout(FlowLayout.LEFT));
pnl[2].add(btn[0]);
btn[0].addActionListener(
new ActionListener()
{
private Component app;
public void actionPerformed(ActionEvent ev)
{
try
{
double gall = Double.parseDouble(txfLength.getText() + txfWidth.getText() + txfDepth.getText());
txfOutput.setText(String.format("%.0f",(gall * 7.48)));
}
catch(NumberFormatException ex)
{
JOptionPane.showMessageDialog(app, "Error. Enter numbers only.", "Improper Format", JOptionPane.WARNING_MESSAGE);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(app, "Error. " + ex.getMessage(), "Error Occured", JOptionPane.ERROR_MESSAGE);
}
}
}
);
//lblLength = new JLabel("Length (ft): ");
txfLength = new JTextField(4);
//lblWidth = new JLabel("Output (ft): ");
txfWidth = new JTextField(4);
//lblDepth = new JLabel("Depth (ft):");
txfDepth = new JTextField(4);
//lblOutput = new JLabel("Output (ft^3): ");
txfOutput = new JTextField(10);
// row 4
JLabel lb1 = new JLabel("# of Gallons");
JTextField gall = new JTextField(10);
//the panel
JPanel fu = new JPanel(new FlowLayout());
fu.add(lb1);
fu.add(gall);
// pane 3 will calculate temp xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// content pane
contentPane0 = new JPanel(new GridLayout(0,1));
contentPane0.add(iconLabel);
contentPane0.add(lbl1[1]);
contentPane0.add(lbl1[2]);
contentPane1 = new JPanel(new GridLayout(4,1));
contentPane1.setPreferredSize(new Dimension(400,400));
contentPane1.add(pnl[0]);
contentPane1.add(pnl[1]);
contentPane1.add(pnl[2]);
contentPane1.add(fu);
tabbedPane = new JTabbedPane();
JPanel[] panels = new JPanel[4];
for (int i = 0; i < panels.length; i++)
{
panels[i] = new JPanel();
tabbedPane.add(panels[i]);
}
panels[0].add(contentPane0);
panels[1].add(contentPane1);
tabbedPane.addTab("General", icon, panels[0], "Information about company");
tabbedPane.addTab("Pool Info", icon, panels[1], "Get pool information");
tabbedPane.addTab("Extras", icon, panels[2], "Get accesories for pool");
tabbedPane.addTab("Chemicals", icon, panels[3], "Get prices for extras");
// top level window( JFrame )
setTitle("Clear Blue Pools");
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add(tabbedPane);
this.setBounds(200, 200, 400, 400);
this.setVisible(true);
pack();
// must set location relevant to (null) after pack() or window will not center
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
Re: Trying to add a photo to first pane, but no go
What exactly do you mean? That sure is a lot of code for just a couple JPanels and a single image. I'd recommend throwing together an SSCCE that demonstrates exactly what you're talking about.
Re: Trying to add a photo to first pane, but no go
Moved from New to Java
db