ActionListener - Radio buttons - java

I'm trying to get a program working where you press the specific radio button that says a color on it, and it makes the entire page that color. The window opens as a square with 4 different color options in it. I just can't for the life of me get the actionPerformed method working. Here is my code. Any help is appreciated.
public class ColorPanel3 extends JPanel implements ActionListener {
Color darkBlue = new Color(5,41,186);
Color lightBlue = new Color(35,253,253);
Color darkRed = new Color(158,19,47);
Color lightRed = new Color(255,105,105);
JRadioButton lightRedButton = new JRadioButton("Light Red");
JRadioButton darkBlueButton = new JRadioButton("Dark Blue");
JRadioButton lightBlueButton = new JRadioButton("Light Blue");
JRadioButton darkRedButton = new JRadioButton("Dark Red");
public ColorPanel3() {
setName("Color Panel");
setLayout(new GridLayout(1, 0, 0, 0));
JPanel panel = new JPanel();
add(panel);
panel.setLayout(null);
panel.setOpaque(true);
lightRedButton.setHorizontalAlignment(SwingConstants.CENTER);
lightRedButton.setBounds(0, 150, 150, 150);
lightRedButton.setBackground(lightRed);
panel.add(lightRedButton);
darkBlueButton.setHorizontalAlignment(SwingConstants.CENTER);
darkBlueButton.setBounds(0, 0, 150, 150);
darkBlueButton.setBackground(darkBlue);
panel.add(darkBlueButton);
lightBlueButton.setHorizontalAlignment(SwingConstants.CENTER);
lightBlueButton.setBounds(150, 0, 150, 150);
lightBlueButton.setBackground(lightBlue);
panel.add(lightBlueButton);
darkRedButton.setHorizontalAlignment(SwingConstants.CENTER);
darkRedButton.setBounds(150, 150, 150, 150);
darkRedButton.setBackground(darkRed);
panel.add(darkRedButton);
ButtonGroup group = new ButtonGroup(); //creates a button group so that only one radio button may be pressed at a time.
group.add(darkBlueButton);
group.add(lightBlueButton);
group.add(darkRedButton);
group.add(lightRedButton);
lightRedButton.addActionListener(actionListener);
darkRedButton.addActionListener(actionListener);
lightBlueButton.addActionListener(actionListener);
darkBlueButton.addActionListener(actionListener);
}
ActionListener actionListener = new ActionListener(){
public void actionPerformed (ActionEvent e){
if(darkBlueButton.isSelected()){
darkBlueButton.setBackground(darkBlue);
lightBlueButton.setBackground(darkBlue);
lightRedButton.setBackground(darkBlue);
darkRedButton.setBackground(darkBlue);
}
if(lightBlueButton.isSelected()){
darkBlueButton.setBackground(lightBlue);
lightBlueButton.setBackground(lightBlue);
lightRedButton.setBackground(lightBlue);
darkRedButton.setBackground(lightBlue);
}
if(darkRedButton.isSelected()){
darkBlueButton.setBackground(darkRed);
lightBlueButton.setBackground(darkRed);
lightRedButton.setBackground(darkRed);
darkRedButton.setBackground(darkRed);
}
if(lightRedButton.isSelected()){
darkBlueButton.setBackground(lightRed);
lightBlueButton.setBackground(lightRed);
lightRedButton.setBackground(lightRed);
darkRedButton.setBackground(lightRed);
}
}
And I made a new class file to put the panel in. This worked.
public class ColorFrame{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new ColorPanel3());
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Color Frame");
}
}
};
}
Final edit: the above code is what I ended up with.

Your action listener should look like this:
ColorPanel3.setOpaque(true);
ColourPanel3.this.setBackground(yourcolor);
And moreover, if you are not writing this code for a code obfuscation contest please use different actionlisteners for all the radio buttons.
Cheers.
Edit: You will have to add the panel to a JFrame

First of all, get rid of these lines:
JRadioButton darkBlueButton = (JRadioButton) e.getSource();
JRadioButton lightBlueButton = (JRadioButton) e.getSource();
JRadioButton darkRedButton = (JRadioButton) e.getSource();
JRadioButton lightRedButton = (JRadioButton) e.getSource();
What is happening is that you are creating four JRadioButton instances with a reference to the JRadioButton given by e.getSource(). If you want the background to change color, then this isn't necessary. I get the feeling that the effect of this isn't what you intended to do, anyway.
When creating your JRadioButton objects with the constructor that takes a String, you're giving it a label (i.e. Dark Blue), along with an action command, which is the same as the text. You can take advantage of this by using the getActionCommand method of the ActionEvent class to determine what color the JPanel should be:
if (e.getActionCommand().equals("Dark Blue")) {
ColorPanel3.this.setBackground(darkBlue);
}
But in order for any color change to be apparent, you must set the transparency of the top panel:
panel.setOpaque(false);

Related

How do you convert a JLabel to a Button in Java?

I have a JLabel with ImageIcons of t-shirts. I want to make each t-shirt have the ability to be clicked and then it will lead to another window. How can I make each t-shirt a button while maintaining the pictures?
This is just part of one of my methods and I want the shirts to become JButtons.
Here is my code:
final JFrame shirts = new JFrame("T-shirts");
JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
for (int i = 1; i < 13; i++) {
l = new JLabel(new ImageIcon("T-shirts/"+i+".jpg"), JLabel.CENTER);
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
l.setFont(l.getFont().deriveFont(20f));
panel.add(l);
}//end of for loop
shirts.setContentPane(panel);
shirts.setSize(1000, 1000);
shirts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shirts.setVisible(true);
There is no need to change to a JButton. The simplest option here is to implement a MouseListener.
This will allow you to test for mouse clicked events:
yourLabelName.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
//point to the frame you want it to go to from here
yourFrame = new JFrame("Next JFrame");
frame.setVisible(true);
}
});
UPDATE
shirts.this.add(l);
That will add the label to the next JFrame
Hope this helps.
Let me know of the outcome :)

Trying to display images in multiple JPanels

I'm creating a program that features a grid of 12 JPanels. When the "add image" button is pressed, an image appears in the first JPanel in the grid and a counter is incremented by one. From then onwards, every time the "add image" is clicked again, an image would be added to the next JPanel. For some reason, the button only adds an image to the first JPanel and then stops working. Here's the code I've got so far.
public class ImageGrid extends JFrame {
static JPanel[] imageSpaces = new JPanel[12];
int imageCounter = 0;
ImageGrid() {
this.setTitle("Image Grid");
setSize(750, 750);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(3, 4, 10, 5));
p3.setBackground(Color.WHITE);
p3.setOpaque(true);
p3.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
for (int j = 0; j < imageSpaces.length; j++) {
imageSpaces[j] = setImageSpace();
p3.add(imageSpaces[j]);
}
MyButtonPanel p1 = new MyButtonPanel();
add(p1, BorderLayout.SOUTH);
add(p3, BorderLayout.CENTER);
}
public JPanel setImageSpace() {
JPanel test;
test = new JPanel();
test.setOpaque(true);
test.setPreferredSize(new Dimension(100, 100));
return test;
}
class MyButtonPanel extends JPanel implements ActionListener {
final JButton addImage = new JButton("Add Image");
ImageIcon lorryPicture = new ImageIcon(ImageGrid.class.getResource("/resources/lorry.png"));
JLabel lorryImage = new JLabel(lorryPicture);
MyButtonPanel() {
add(addImage);
addImage.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addImage) {
imageSpaces[imageCounter].add(lorryImage);
revalidate();
repaint();
imageCounter++;
}
}
}
public static void main(String[] args) {
ImageGrid test = new ImageGrid();
test.setVisible(true);
}
}
You should be revalidating and repainting the panel, (which is the containter being affected by the addition), not the frame
imageSpaces[imageCounter].add(lorryImage);
imageSpaces[imageCounter].revalidate();
imageSpaces[imageCounter].repaint();
Diclaimer: This may work as a simple fix, but also note that a component (in this case your JLabel lorryImage) can only have one parent container. The reason the above fix still works is because you don't revalidate and repaint the previous panel, the label was added to. So you may want to think about doing it correctly, and adding a new JLabel to each panel.
if (e.getSource() == addImage) {
JLabel lorryImage = new JLabel(lorryPicture);
imageSpaces[imageCounter].add(lorryImage);
imageSpaces[imageCounter].revalidate();
imageSpaces[imageCounter].repaint();
imageCounter++;
}
Disclaimer 2: You should add a check, to only add a label if the count is less than the array length, as to avoid the ArrayIndexOutOfBoundsException
Side Notes
Swing apps should be run from the Event Dispatch Thread (EDT). You can do this by wrapping the code in the main in a SwingUtilities.invokeLater(...). See more at Initial Threads
You could also just use a JLabel and call setIcon, instead of using a JPanel

How to check if mouse pressed and still pressed on a button in java swing app

I have array of buttons and what I want is that when I press button and drag to another button color the path that I travel.
When I press a button it will change it's color by mousePressed method, then I drag to next button while still pressing, and will change the color of the 2nd button too. How can I do this?
This is the main class
public class Flowcolor extends JFrame{
public Flowcolor(int rows,int cols,int hgap,int vgap){
setTitle("flow color");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
/**top panel in north of borderlayout
* have two label time , number of moves
* menue button
*/
JPanel toppanel = new JPanel();
add(toppanel,BorderLayout.NORTH);
toppanel.setLayout(new BoxLayout(toppanel,BoxLayout.X_AXIS));
JTextField t1=new JTextField("Time:");
t1.setEditable(false); // can't write in this textfeild
JTextField t2=new JTextField("movements number:");
t2.setEditable(false);
toppanel.add(t1);
toppanel.add(t2);
JPanel centerpanel = new JPanel();
add(centerpanel,BorderLayout.CENTER);
centerpanel.setBorder(BorderFactory.createLineBorder(Color.blue,3));
centerpanel.setLayout(new GridLayout(rows,cols));
PosButton z[][]=new PosButton[rows+1][cols+1];
for (int i=1;i<=rows;i++)
{
for (int j=1;j<=cols;j++)
{
PosButton btn= new PosButton(" ",Color.DARK_GRAY,Color.lightGray);
z[i][j]=btn;
System.out.println(i+" "+j);
centerpanel.add( z[i][j]);
}
}
setVisible(true);
}
public static void main(String args[])
{
Flowcolor z= new Flowcolor (3,3,0,0);
//startmenu s=new startmenu();
}
}
and there is posbutton class that implements mouselistener.
You could set up a glass pane that detects the mouse events, but it might get tricky if you're interfering with the default behavior of the JButtons. Worth checking out though: https://weblogs.java.net/blog/alexfromsun/archive/2006/09/a_wellbehaved_g.html

Resize JButtons in a BoxLayout

I'm trying to make a simple menu for my game. I have 4 buttons in the center and I want to make them a little bit bigger and center them.
The last part worked but I can't seem to call any of my JButtons and do a .setSize / .setPreferedSize(new Dimension()) on it.
public class mainMenu extends JFrame {
private JButton start, highscore, help, stoppen;
public mainMenu() {
super("Master Mind");
maakComponenten();
maakLayout();
toonFrame();
}
private void maakComponenten() {
start = new JButton("Start");
start.setBackground(Color.gray);
highscore = new JButton("Higscores");
help = new JButton("Help");
stoppen = new JButton("Stoppen");
}
private void maakLayout() {
JPanel hoofdmenu = new JPanel();
hoofdmenu.setLayout(new BoxLayout(hoofdmenu, BoxLayout.Y_AXIS ));
hoofdmenu.add(start);
start.setAlignmentX(CENTER_ALIGNMENT);
hoofdmenu.add(highscore);
highscore.setAlignmentX(CENTER_ALIGNMENT);
hoofdmenu.add(help);
help.setAlignmentX(CENTER_ALIGNMENT);
hoofdmenu.add(stoppen);
stoppen.setAlignmentX(CENTER_ALIGNMENT);
super.add(hoofdmenu);
}
private void toonFrame() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new mainMenu();
}
}
As an example, to change the size of the "Start" button,
change :
start1 = new JButton("Start");
to
start1 = new JButton("Start") {
{
setSize(150, 75);
setMaximumSize(getSize());
}
};
The problem is that JFrames use BorderLayout by default, which means that your JPanel will naturally fill the space.
Before adding your JPanel, call the following code to change the JFrame's layout to null and use the JPanel's settings instead.
this.setLayout(null);
JPanel hoofdmenu = new JPanel();
hoofdmenu.setBounds(0,0, 400, 100);
Alternatively, you could set the maximum size of the JButtons
Dimension maxSize = new Dimension(100, 100);
start.setMaximumSize(maxSize);
highscore.setMaximumSize(maxSize);
help.setMaximumSize(maxSize);
stoppen.setMaximumSize(maxSize);
Here's another example following behind the previous two - I'm making a soundboard program, and this is actually a sample from it - The JPanel actually is needed, agreeing to the second post.
JFrame frame = new JFrame();
JPanel menuPanel = new JPanel();
JButton Button1 = new JButton("<BUTTON NAME 1>");
Button1.setSize(80, 30);
Button1.setLocation(4, 4);
JButton Button2 = new JButton("<BUTTON NAME 2>");
Button2.setSize(80, 30);
Button2.setLocation(90, 4);
Ah, and another thing - You created the buttons in a different block from the second piece of code. Doing that causes the other blocks to not see it. You need to declare them outside the block so all the blocks can see them.

Java - How to add new JLabel and JTextPane to JPanel when button is pressed

I have a JPanel to which when a button is pressed I want to add a new JLabel and JTextField too. However, I can't seem to get it working.
Is there an issue with my ActionListener, and if not, how could this be achieved?
JPanel south = new JPanel();
JButton add = new JButton("Add");
ActionListener addListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JLabel mL = new JLabel("MOD: ");
mR.add(mL);
JTextField mM = new JTextField(10);
mR.add(mM);
mR.repaint();
}
};
add.addActionListener(addListener);
south.add(add);
add(south, BorderLayout.NORTH);
The layout of the mR panel is a grid layout set to allow multiple rows and two columns.
Call mR.revalidate() before repaint();
See my answer on a previous SO question for some sample code which dynamically adds a component to a container

Categories