Java ImageIcon to JLabel Array - java

Why do I get an error when I try to set the ImageIcon of this JLabel to something. It returns a null pointer exception. Does anyone know the problem?
public class Window extends JFrame{
JPanel panel = new JPanel();
JLabel stick[] = new JLabel[10];
Window(){
super("ThisIsWindow");
setSize(650,550);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
add(panel);
panel.setLayout(null);
stick[5].setIcon(new ImageIcon("stick.gif"));
}
}
The error occurs on the last line of code that sets stick[5] to stick.gif. Can anyone help?

Add
stick[5] = new JLabel();
before
stick[5].setIcon(new ImageIcon("stick.gif"));
Basically, you are creating an array that holds 10 references of type JLabel, and those references are referring to nothing (null) at the beginning:
JLabel stick[] = new JLabel[10];
So you need to create 10 instances of JLabel with new JLabel() and let those 10 references pointing to them:
for(int i = 0; i < 10; i++) stick[i] = new JLabel();

Is there a JLabel object in stick[5]?

Related

Java Gui won't display panels and components

im trying to make a program to add an admin to a ms access database
I researched many times, figured out all the components need to be in a panel, and only the same type of J stuff can be in a panel, so i made many panels and combined them in a big panel
//frame details
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 1000;
JFrame aFrame = new JFrame("Add admin");
aFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
aFrame.setVisible(true);
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//panel declaration
JPanel BigPanel = new JPanel();
JPanel adminnameenter = new JPanel();
JPanel typeadminname = new JPanel();
JPanel adminlastnameenter = new JPanel();
JPanel typeadminlastname = new JPanel();
JPanel buttonaddadmin = new JPanel();
//labels, textfields, and buttons
JLabel newAdminName = new JLabel("Enter admin name");
JTextField adminName = new JTextField(7);
JLabel newadminlastname = new JLabel("Enter admin last name");
JTextField adminlastname = new JTextField(7);
JButton addadmin = new JButton("Add Admin");
//add things to panel
adminnameenter.add(newAdminName);
typeadminname.add(adminName);
adminlastnameenter.add(newadminlastname);
typeadminlastname.add(adminlastname);
buttonaddadmin.add(addadmin);
//add things to big jPanel
BigPanel.add(adminnameenter);
BigPanel.add(typeadminname);
BigPanel.add(adminlastnameenter);
BigPanel.add(typeadminlastname);
BigPanel.add(buttonaddadmin);
//add things to frame
aFrame.add(BigPanel);
the only thing that popped up was a frame that said add admin
Add this code to the end of your function:
aFrame.setVisible(false);
aFrame.setVisible(true);
Or alternatively put
aFrame.setVisible(true);
at the end of your function instead of the beginning.
And all the components will appear. This is because whenever you change anything to your JFrame, it will only change on the user side once it is told to resize or refresh the frame. Also you don't need to put every single component in it's own JPanel, you can simply insert them directly in your BigPanel (small nitpick, but the b in bigPanel, shouldn't be capitalized, seeing as variables start with non-capitalized letters).
Also look into LayoutManagers, they will probably be useful for your application.
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Hello,How can i add two panels in one frame? [duplicate]

This question already has answers here:
How to add JTable in JPanel with null layout?
(11 answers)
Closed 6 years ago.
Can any one help me?
Hello,How can i add two panels in one frame?
public class test{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(5, 5, 300, 300);
JPanel panel2 = new JPanel();
panel2.setLayout(null);
panel2.setBounds(1,200,300,300);
JLabel label2 = new JLabel("asddas");
label2.setBounds(30,30,20,20);
panel2.add(label2);
JLabel label[] = new JLabel[10];
int count = 1;
for(int i = 0; i < 10; i++){
label[i] = new JLabel("ds");
label[i].setBounds(1,count,20,20);
count +=20;
panel.add(label[i]);
}
frame.add(panel,panel2);
frame.setVisible(true);
}
}
You can think of the JPanel as one big panel that contains the all of the other elements. So you can have a main JPanel and then put others inside it. You should set a layout that fits your needs to the main panel. A good introduction to layouts can be found here http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
Also see this answer
How to layout multiple panels on a jFrame? (java)

Java Button Action Command

I'm creating a simple Minesweeper game in Java. Size 9x9.
I create an array of JPanels and an array of buttons; I add each button to its respective JPanel. then i add the JPanels to the JFrame.
How do i distinguish between each button on the action event?
Here's some of my code:
int gridx = 9;
int gridy = 9;
JButton[] buttons = new JButton[gridx*gridy];
JPanel[] jpanels = new JPanel[gridx*gridy];
public Minesweeper(){
super("Minesweeper");
setLayout(new GridLayout(9,9));
JPanel panel = new JPanel();
int i = 0;
for(i = 0; i<gridx*gridy; i++){
jpanels[i] = new JPanel();
buttons[i] = new JButton();
buttons[i].addActionListener(buttonEvent);
jpanels[i].setLayout(new GridLayout(1,1));
jpanels[i].add(buttons[i]);
add(jpanels[i]);
}
//buttons[67].setEnabled(false);
setSize(300,300);
setVisible(true);
}
The only way i can think about doing this is adding text to the button like so:
buttons[i] = new JButton(i);
Then calling getActionCommand() but i dont want text to show up on the button. Any other ideas?
You can use AbstractButton#setActionCommand.
In your loop:
buttons[i].setActionCommand(i+"");
Then you'll get i back when you use getActionCommand
Note I did mention in a comment on another answer that I would create a new class Mine which extends JButton which I believe to be a better and more complete solution. This however gets the job done rather quickly.

NullPointerException: adding JButtons in an array & panel

JButton buttonArray[][] = new JButton [6][7];
JPanel grid;
JButton b1;
grid.setLayout (new GridLayout(6,7,0,0));
slot = new ImageIcon ("gameboard.png");
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 7; ++j)
{
b1 = new JButton (slot);
buttonArray[i][j] = b1;
buttonArray[i][j].setContentAreaFilled (false);
buttonArray[i][j].setBorderPainted (false);
grid.add(buttonArray[i][j]);
}
}
I am getting a NullPointerException which points to the grid.setLayout (new GridLayout(6,7,0,0)); part and to the new GameBoard(); which is in the main method at the bottom.
I add grid panel t o another panel as well, together with other panels:
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add("North", panel1);
panel.add("Center",grid);
panel.add("South",panel2);
add(panel);
I did initialize grid and buttonArray[][] already. What am I missing?
The grid variable is null as it has never been assigned an object. You need to either give it a new something or pass in its value via a setter method or constructor parameter.
More important than the actual solution to your current problem, is the knowledge of how to debug most common NullPointerExceptions. When you encounter a NullPointerException, you should carefully check all the variables on the line that throws the exception, find out which one is null, and then track back in your program to find out why it's null when you though otherwise.
You didn't initialize the grid variable:
JPanel grid; // null since it wasn't initialized
JButton b1;
grid.setLayout (new GridLayout(6,7,0,0));
You should just create some object there:
JPanel grid = new JPanel();
JButton b1;
grid.setLayout (new GridLayout(6,7,0,0));

How to get values of dynamically generated components?

I am dynamically generating a list of name boxes, sliders and labels, and am trying to figure out how to access the values of the sliders and change the labels. Other posts suggest using an array, but I have no idea where to begin with that.
My code is as such:
public Tailoring(int n) {
/*initComponents();*/
JPanel containerPanel = new JPanel();
containerPanel.setLayout(new BoxLayout(containerPanel, BoxLayout.PAGE_AXIS));
this.add(containerPanel);
JLabel Title = new JLabel("Tailoring:");
containerPanel.add(Title);
for(int i = 0; i < n; i++){
JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTextField NameBox = new JTextField("Guest " + (i+1));
JSlider TipSlider = new JSlider();
JLabel TipCost = new JLabel();
rowPanel.add(NameBox);
rowPanel.add(TipSlider);
rowPanel.add(TipCost);
containerPanel.add(rowPanel);
}
}
You can create a new class YourPanel which extends JPanel.
Instead of the statement
JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
you can use
YourPanel rowPanel = new YourPanel(new FlowLayout(FlowLayout.LEFT));
Define textfield, slider and label as the properties of this YourPanel class.
Provide getters/setters for each field. Then use an array or ArrayList of YourPanel objects in your application. You will be able to reach the nth panel's label with a call like:
panels.get(n).getJLabel();
It appears that you'd like to change the value displayed in the JLabel when the associated JSlider is modified, right? The best way to associate pairs of objects in Java is with a Map structure:
Map<Component, JSlider> sliderToLabel = new HashMap<Component, JSlider>();
for (int i = 0; i < n; i++) {
// after your loop code
sliderToLabel.put(TipSlider, TipCost); // map the slider to its label
}
You will be able to get a reference to the JSlider in the code that listens for changes on that component.
JLabel updateLabel = sliderToLabel.get(targetedSlider);
updateLabel.setText("updated text");
Notes
As a matter of convention, variable names should begin with lower case letters
The event listener I alluded to should also be attached in the loop. See Writing Event ListenersOracle

Categories