I'm creating a Java GUI using Swing with Eclipse and Window Builder Pro. I'm using JButtons and JToggleButtons. I want to change toggle button's state from another button.
For example, when I click the clear grid, all the toggle buttons will be 'not selected'.
How can I do this? What are the methods that I have to use for toggle buttons and buttons?
toggleButton.setSelected(boolean b)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class JToggleButtonAction {
public static void main(String args[]) {
JFrame frame = new JFrame("Selecting Toggle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToggleButton toggleButton = new JToggleButton("Toggle Button");
final JToggleButton toggleButton1 = new JToggleButton("Another Toggle Button");
ActionListener actionListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println("Action - selected=" + selected + "\n");
toggleButton1.setSelected(selected);
}
};
toggleButton.addActionListener(actionListener);
frame.add(toggleButton, BorderLayout.NORTH);
frame.add(toggleButton1, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
Add actionListener to your JButton and in actionPerformed(ActionEvent) method change the state of all JToggleButtons. Make sure all your JToggleButton is accessible in this method. A simple example will be..
JFrame frame = new JFrame("Panel image demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(new FlowLayout());
final JToggleButton[] button = new JToggleButton[10];
for (int i = 0; i < button.length; i++) {
button[i] = new JToggleButton("Toggle Us");
frame.add(button[i]);
}
JButton jButton = new JButton("Toggle that button");
jButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (JToggleButton jToggleButton : button) {
jToggleButton.setSelected(!jToggleButton.isSelected()); // <-- this will change the state of toggle button
}
}
});
frame.add(jButton);
frame.setVisible(true);
Register an ActionListener to the JButton instance and make sure you can access the toggle buttons therein to manipulate their state.
i was looking for this, this might help somebody out
B1.setSelected(false);
i made a method that make all my button false (unselect the toggles when i wanted it)
Related
I'm trying to get my labels to be in the center of the window when I press the corresponding button, but it instead just sits on top of the button instead of sitting in the middle and i'm not sure why
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class navigator extends JFrame
{
Container con;
public navigator(){
super("JFrame");
JFrame newFrame = new JFrame("Navigator");
newFrame.setSize(400, 400);
newFrame.setVisible(true);
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = getContentPane();
BorderLayout newLayout = new BorderLayout();
con.setLayout(newLayout);
JButton newButton = new JButton("Up");
newFrame.add(newButton, BorderLayout.NORTH);
JButton newButton2 = new JButton("Left");
newFrame.add(newButton2, BorderLayout.WEST);
JButton newButton3 = new JButton("Down");
newFrame.add(newButton3, BorderLayout.SOUTH);
JButton newButton4 = new JButton("Right");
newFrame.add(newButton4, BorderLayout.EAST);
JLabel newLabel = new JLabel("Going up!");
newFrame.add(newLabel, BorderLayout.CENTER);
newLabel.setVisible(false);
newButton.add(newLabel);
JLabel newLabel2 = new JLabel("Going left!");
newFrame.add(newLabel2, BorderLayout.CENTER);
newLabel2.setVisible(false);
newButton2.add(newLabel2);
JLabel newLabel3 = new JLabel("Going down!");
newFrame.add(newLabel3, BorderLayout.CENTER);
newLabel3.setVisible(false);
newButton3.add(newLabel3);
JLabel newLabel4 = new JLabel("Going right!");
newFrame.add(newLabel4, BorderLayout.CENTER);
newLabel4.setVisible(false);
newButton4.add(newLabel4);
newButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
newLabel.setVisible(true);
newLabel2.setVisible(false);
newLabel3.setVisible(false);
newLabel4.setVisible(false);
}
});
newButton2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
newLabel2.setVisible(true);
newLabel.setVisible(false);
newLabel3.setVisible(false);
newLabel4.setVisible(false);
}
});
newButton3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
newLabel3.setVisible(true);
newLabel2.setVisible(false);
newLabel.setVisible(false);
newLabel4.setVisible(false);
}
});
newButton4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
newLabel4.setVisible(true);
newLabel2.setVisible(false);
newLabel3.setVisible(false);
newLabel.setVisible(false);
}
});
}
public static void main(String[] args){
navigator myNavigator = new navigator();
}
}
JLabel newLabel = new JLabel("Going up!");
newFrame.add(newLabel, BorderLayout.CENTER);
newLabel.setVisible(false);
newButton.add(newLabel); // ???
A component can only have a single parent. So you can't add the label to the frame and the button. I'm not even sure why you would be attempting to add the label to the button.
In any case you can't add four labels to the center of the frame. The BorderLayout only allows one component in each area, so only the last component added will ever be visible. The BorderLayout will only set the size of the last button added. All the other buttons will have a size of (0, 0) so there is nothing to paint.
So just add a single label and then change the text using the setText(...) method in your ActionListener.
However, once you fix this you will still have a problem. By default a label is painted at the left of the space available to the label.
If you want the text displayed in the center then you need to use:
label.setHorizontalAlignment(JLabel.CENTER);
Also, all components should be added to the frame before making the frame visible.
Finally, class names should start with an upper case character. Look at the class names of the JDK API and follow the conventions used their.
When using a BorderLayout, you can only put one control in each section. So you can only put one button in CENTER, for instance.
If you want to put more things in one area, then you need to create a new JPanel, put it in CENTER, and then place the buttons on the newly created JPanel. (Of course following the same layout rules for it). You can recursively add as many jpanels as you like.
Hello I am writing a sort of Menu for an encryption program I am writing. I finished the core of it, and now i want to try to create a GUI for it. Here is the code for the first Menu:
package matrix_with_GUI;
import javax.swing.*;
import java.awt.event.* ;
import java.awt.* ;
public class Main_Menu extends JFrame implements ActionListener{
private JButton action1 = new JButton ("");
private JButton action2 = new JButton ("");
private JPanel pane = new JPanel();
private JLabel lbl;
public static void main(String[] args) {
Main_Menu main = new Main_Menu();
}
public Main_Menu(){
super();
JPanel pane=new JPanel();
setTitle ("Start Menu") ;
JFrame frame = new JFrame("");
setVisible(true);
setSize (380, 260) ;
setLocation (450, 200) ;
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ;
action1 = new JButton("Start");
action2 = new JButton("Exit");
lbl = new JLabel ("Welcome to the Matrix Encoder/Decoder!!!");
setLayout(new FlowLayout());
add (lbl) ;
add(action1, BorderLayout.CENTER);
action1.addActionListener (this);
add(action2, BorderLayout.CENTER);
action2.addActionListener (this);
}
#Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
OptionsMenu x = new OptionsMenu();
if (event.getSource() == action1)
{
System.exit(0);
x.OptionsMenu();
}
else if(event.getSource() == action2){
System.exit(0);
}
}
}
When the Start Button is clicked, the new menu comes up all fine and good, but the first menu stays open. Is there a way to close this first menu AND open the second menu with the first button click? I'm very new to GUI so the simplest solution would be very helpful. On a side note is there a simple way to move the Start button to the next line? Thanks
You have 2 options: you can use a Window Listener, or you can use the dispose() method. To do the dispose() just type
* This is better to be used with subframes and 2nd level windows.*
this.dispose();
or check this link for using the window listener
Closing JFrame
In order to close the Main_Menu, you can just call its dispose method:
this.dispose();
instead of calling System.exit(0), which will terminate the JVM altogether.
i create a dialog box in java desktop application . But when i hide/show label and button by applying condition on checkbox .its produce graphical noise by showing some part of background application part(like red box on both label and buttons also checkbox layout causes problem). i write this condition on checkbox.
checkbox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (checkbox.isSelected()) {
baisvalue.setVisible(true); //label
plusbais.setVisible(true); //button
minisbais.setVisible(true); //button
}
if (!checkbox.isSelected()) {
minisbais.setVisible(false); //label
plusbais.setVisible(false); //button
baisvalue.setVisible(false); //button
}
}
});
note:
i also call repaint(); & validate(); but same problem occurs.
I wasn't able to recreate your problem - I see no graphical noise. I've attached a sscce of what I tried - Can you reproduce your problem with this example? If so, can you provide us with more information about your java version/platform? If not, can you modify this example to recreate your problem (and edit your question with the code)?
import java.awt.event.*;
import javax.swing.*;
public class MainPanel extends Box{
JCheckBox checkbox = new JCheckBox("Select Me");
JLabel baisvalue = new JLabel("baisvalue");
JButton plusbais = new JButton("plusbais");
JButton minisbais = new JButton("minisbais");
public MainPanel(){
super(BoxLayout.Y_AXIS);
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (checkbox.isSelected()) {
baisvalue.setVisible(true); //label
plusbais.setVisible(true); //button
minisbais.setVisible(true); //button
}
if (!checkbox.isSelected()) {
minisbais.setVisible(false); //label
plusbais.setVisible(false); //button
baisvalue.setVisible(false); //button
}
}
};
checkbox.addActionListener(l);
add(checkbox);
add(baisvalue);
add(plusbais);
add(minisbais);
//Performs the action on initialization
l.actionPerformed(new ActionEvent(checkbox, ActionEvent.ACTION_PERFORMED, ""));
}
public static void main(String[] args){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPanel());
frame.pack();
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Try to use this.setOpaque(false); in constructor.
Here is the code of my Layout class.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Layout extends JFrame {
private JButton lb;
private JButton cb;
private JButton pb;
private FlowLayout layout;
private Container container;
public Layout() {
super("The Title");
layout = new FlowLayout();
container = new Container();
setLayout(layout);
//*Left
lb = new JButton("L");
add(lb);
lb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.LEFT);
layout.layoutContainer(container);
}
}
);
//*Center
cb = new JButton("C");
add(cb);
cb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(container);
}
}
);
//*Right
pb = new JButton("R");
add(pb);
pb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.RIGHT);
layout.layoutContainer(container);
}
}
);
}
}
I'm learning java through thenewboston youtube tutorials (this code is from this tutorial). But this one doesn't work like it should. When I click the right button (R) it should drag all the buttons to the right side of the window instantly. It doesnt. However when I click that right button and then forcibly resize the window then it do what it should. By adding setResizable(false) in the main method I cant resize the program so it doesnt work.
What have I done wrong?
Forgive me for my poor English btw.
replace
container = new Container();
by
container = getContentPane();
The simplest way would be to set a new FlowLayout and call invalidate():
setLayout(new FlowLayout(FlowLayout.LEFT));
invalidate();
validate();
Updating the current FlowLayout has no effect.
i am trying to make an actionListener on a button in another button which has also an actionlistener and i just couldn't figure it out for some way. I am trying to make an action on the 2nd button but i couldn't figure it out.If anyone helps me i'd appreciate! here is the code below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class basic implements ActionListener{
public static void main(String[] args) {
basic process = new basic ();
}
public basic(){
JFrame fan = new JFrame("Scheme");
JPanel one = new JPanel(new BorderLayout());
fan.add(one);
JPanel uno = new JPanel();
uno.setLayout(new BoxLayout(uno, BoxLayout.Y_AXIS));
JButton addB = new JButton("first choice");
addB.setAlignmentX(Component.CENTER_ALIGNMENT);
uno.add(addB);
addDButton.setActionCommand("hehe");
addDButton.addActionListener(this);
one.add(uno,BorderLayout.CENTER);
fan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fan.setSize(500,700);
fan.setLocationByPlatform(true);
fan.setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
JPanel markP = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,20));
JDialog dialog = new JDialog((JFrame)null);
dialog.getContentPane().add(markP,BorderLayout.CENTER);
if (evt.getActionCommand().equals("hehe")) {
JLabel title = new JLabel("Proceed");
title.setFont(new Font("Arial",Font.BOLD,15));
markP.add(title,BorderLayout.NORTH);
JButton exit = new JButton("Exit");
markP.add(exit);
//here i want to create another actionListener on the exit button only without affecting the other content which is in the button "addB " so that when i click on the addB button the J dialog pops up, and than when i click on exit button the program will return to the menu.I couldn't figure it out.
dialog.toFront();
dialog.setModal(true);
dialog.pack(); //
dialog.setLocationRelativeTo(null); //
dialog.setVisible(true);
}
// here the code goes on but the problem is that of the actionListener which is concerned.
JButton exit = new JButton("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// you code here
}
});
You should use better variables names. It is not easy to follow your code
You could use the same ActionListener if you check the source of the action using
if (evt.getSource().equals(addDButton) { original code }
else { code for the other button }