Hide radio button on show jframe - java

I'm using netbeans 8. I have 2 radio button that I want to hide when the frame is shown. How can I do that? I successfully do that when I click other button such as this:
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jRadioButton3.setVisible(false);
jRadioButton4.setVisible(false);
}
but that's not what I want. I want to set it to invisible and only show it when I click other radio button. For some reason netbean prevent me from editing certain area in my source code so I can't test or explore it. Please help and thanks in advance.

Set the JRadioButton setVisible method as false by default and then change it when an action is performed.
For example, here under, the JRadioButtons will be visible once the first JRadioButton is selected. If it is deselected, they disappear.
I did it with a JRadioButton but it can be done with other components of course.
Solution
public class Test extends JFrame{
private JRadioButton but1, but2, but3;
public Test(){
setSize(new Dimension(200,200));
initComp();
setVisible(true);
}
private void initComp() {
but1 = new JRadioButton();
but1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
but2.setVisible(but1.isSelected());
but3.setVisible(but1.isSelected());
}
});
but2 = new JRadioButton();
but2.setVisible(false);
but3 = new JRadioButton();
but3.setVisible(false);
setLayout(new FlowLayout());
JPanel pan = new JPanel();
pan.add(but1);
pan.add(but2);
pan.add(but3);
getContentPane().add(pan);
}
public static void main(String[] args) {
new Test();
}
}

You can set the radio button to invisible when you add it to the frame and make it visible on some event:
public class InvisibleRadioButton {
public static void main(String[] args) {
JFrame frame = new JFrame();
final JRadioButton jRadioButton1 = new JRadioButton("1");
JRadioButton jRadioButton2 = new JRadioButton("2");
frame.setLayout(new FlowLayout());
frame.add(jRadioButton1);
frame.add(jRadioButton2);
frame.setVisible(true);
jRadioButton1.setVisible(false); // initialize as invisible
jRadioButton2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jRadioButton1.setVisible(true); // set it to be visible
}
});
frame.pack();
}
}

Related

How do I get my button actionListener to count the number of times someone clicks?

public class Button {
public static void main(String[] args) {
int numClicks = 0;
JButton button1 = new JButton ();
button1.setText("1 click = 1 dollar for animals you love");
JFrame god = new JFrame ();
JPanel panel = new JPanel();
god.getContentPane().add(button1);
god.add(button1);
god.setSize(new Dimension(400, 400));
god.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
}
}
Can anyone help me figure out how to make the actionlistener count the number of times the button is clicks? Im a noobie coder (3 months) and Im really stuck on this
You need to add ActionListener to you button.
button1.addActionListener(new MyActionList());
//MyActionList is an object which implements ActionListener
In your case is the same object.
//---implements ActionListener---
public class Button implements ActionListener{
private int clickCount;
public Button() {
clickCount = 0;
JFrame god = new JFrame ();
god.setSize(new Dimension(400, 400));
//if you don't do this, your program still running, even if you click on X.
god.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
god.add(panel);
JButton button1 = new JButton("1 click = 1 dollar for animals you love");
//here we add ActionListener
button1.addActionListener(this);
//'this' refer to the current object. In our case 'new Button()' from 'main'...
//...which has implemented ActionListener.
panel.add(button1);
god.setVisible(true);
}
public static void main(String[] args){
new Button();
}
//Override from Interface ActionListener and...
//...here describe what happend when button was pressed.
#Override
public void actionPerformed(ActionEvent e) {
clickCount++;
System.out.println(clickCount);
}
}

How to be back on main class panel from panels of different class?

I have two classes one is main class and other is pro class. The main class has introductory panel which further shows the panel of pro class. The second panel of pro class has "Home" button which needs to show the main class panel. please tell me how do I make "Home" button work?
class proMain extends JPanel {
JPanel pan2 = new JPanel();
JButton b1, b2, b3;
CardLayout lay = new CardLayout();
public void pshow() {
// First Panel
pan.setBackground(Color.red);
b1 = new JButton("Next");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Next")) {
pan2.setVisible(true);
pan.setVisible(false);
}
}
});
pan.add(b1);
// Second Panel
b2 = new JButton("Previous");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Previous")) {
pan.setVisible(true);
pan2.setVisible(false);
}
}
});
b3 = new JButton("Home");
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand() == "Home") {
//
}
}
});
pan2.setBackground(Color.cyan);
pan2.add(b2);
pan2.add(b3);
pan.setVisible(true);
pan2.setVisible(false);
add(pan);
add(pan2);
}
}
Above class is proMain class and the main class is:
public class proDis {
public static void main(String[] args) {
JFrame fr = new JFrame("CrdLay");
JPanel pan3 = new JPanel();
JButton b = new JButton("Next");
CardLayout cl = new CardLayout();
fr.setLayout(cl);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Next")) {
pan3.setVisible(false);
proMain pm = new proMain();
pm.pshow();
pm.setLayout(cl);
fr.add(pm);
pm.setVisible(true);
}
}
});
pan3.setBackground(Color.gray);
pan3.add(b);
fr.setSize(100, 300);
fr.setVisible(true);
fr.add(pan3);
}
}
You are attempting to use a CardLayout, which is the correct approach. However, your implementation is incorrect.
The problem with your code is that you are not using the methods of the CardLayout to control which panel is being displayed. The CardLayout supports methods. like next(...) and previous(...) to sequentially move through all the panels and the method show(...) to display a specific panel.
So the first thing to do is give your panels a name when you add each panel to the layout. Then change your code to use the above methods to control which panel is being displayed. There is no need to play with the visibility of any panel, this is the job of the CardLayout.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.

Why is my text field not appearing?

I have to create a text field, a text area, and two buttons, but I am stuck on the text field because I cant get it to appear when I run my program. My JFrame keeps appearing empty.
public class SentanceBuilder extends JFrame {
private JTextField textField = new JTextField(50);
private JTextArea textArea = new JTextArea(200,200);
private JButton Submit = new JButton();
private JButton Cancel = new JButton();
public SentanceBuilder(){
textField.setVisible(true);
textField.setLocation(50, 50);
this.textField();
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void textField(){
String textContent = textField.getText();
}
}
You never add the textField variable to a container such as a JPanel that is held by the top-level window, such as a JFrame. In fact, in your code, you add nothing to your JFrame!
If this were my GUI, I'd
Create my JTextField
Create a main JPanel to hold my components.
Add it and other components to the main JPanel via the JPanel's add(...) method.
Add the main JPanel to the JFrame's contentPane via the JFrame's add(...) method.
Call pack() and then setVisible(true) on the JFrame, but only after adding all components to it, not before.
Read the Swing tutorials since this beats guessing every time. You can get links to the tutorials at the Swing Tag Info link.
e.g.,
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyFoo extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField textField = new JTextField(10);
private JButton button = new JButton("Foo Button");
public MyFoo() {
super("My JFrame");
// so Java will end when GUI is closed
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// code to be called when button is pushed
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO code that runs when button is pushed.
}
});
// panel to hold everything
JPanel mainPanel = new JPanel();
// add all to the panel
mainPanel.add(new JLabel("Text Field:"));
mainPanel.add(textField);
mainPanel.add(button);
// add the panel to the main GUI
add(mainPanel);
}
// start up code
private static void createAndShowGui() {
JFrame mainFrame = new MyFoo();
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
// call start up code in a Swing thread-safe way
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

How to replace current JPanel with another JPanel?

I am trying to transit from a UserAdminPanel to AdminLogin within the same JPanel when I press the Admin button.
UserAdmin Panel
transit to AdminLogin Panel
The problem I have now is that I am opening up a new panel instead of changing the current panel to the new panel.
This is my code for the UserAdminPanel
public class SelectAdminUserPanel extends JPanel
{
public SelectAdminUserPanel()
{
setLayout(new GridLayout(3,1));
JButton b1 = new JButton("User Login");
JButton b2 = new JButton("Admin Login");
JButton b3 = new JButton("Exit");
b1.addActionListener(new SelectUserButtonListener() );
b2.addActionListener(new SelectAdminButtonListener());
b3.addActionListener(new SelectExitButtonListener() );
add(b1);
add(b2);
add(b3);
}
private class SelectAdminButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
AdminModule am = new AdminModule();
am.run();
}
}
private class SelectUserButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
GameModule gm = new GameModule();
gm.run();
}
}
private class SelectExitButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
}
}
}
This is the code for the AdminLogin Panel
public class AdminLoginPanel extends JPanel
{
AdminLoginPanel()
{
JLabel pwlabel = new JLabel("Password");
JPasswordField pwfield = new JPasswordField(20);
JButton loginbutton = new JButton("Login");
add(pwlabel);
add(pwfield);
add(loginbutton);
}
}
I have looked at the following example and this example but it's not very applicable because it talks about CardLayout instead of like rewriting the current JPanel.
I think that you should have a reference to your main frame and just remove the components from it based on the button pressed and add only the required components. From what you say, UserAdminPanel is your main panel. I think it's added to a frame for which you can obtain a reference. When you click a button, you want to remove all the content shown on it and display only what the button clicked should show. I think it should look something like this:
private class SelectAdminButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
frame.getContentPane().removeAll();
AdminModule am = new AdminModule();
frame.add(am.getNewPanel());
frame.pack();
// am.run(); //it's not clear what does for you
}
}
Where the method getNewPanel() would return the underlying JPanel. I'm assuming that AdminModule has a reference to the AdminLoginPanel.

Java JFrame not updating settings of a button

I am currently having a minor issue with a Java Jframe and a button not updating.
I am trying to disable the Print Button until the printing of the new JFrame it opens is done and that JFrame is closed...
The button will only disable if and when a new window occurs, but will not until then, which can take a little bit of time....
I set the button to disable by doing this: PrintBttn.setEnabled(false);
I have tried calling mainPanel.revalidate(); mainPanel.repaint(); PrintBttn.revalidate(); PrintBttn.repaint as well as a mixture of the above as they recommended in other forums...
I am kind of lost at the moment on this and to why it is not disabling the button until a new window appears since the first thing i do is Disable it as shown above, and then go through and create the new window....
Thanks,
Erik
Most likely, it's a question of releasing the EDT to allow it to repaint the disabled button.
Generally, it will look something like this:
PrintBttn.setEnabled(false);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Code to display the second JFrame goes here
}
};
Might be you had failed to put your first frame in the EDT too, do watch the code, is this what you actually want :
import java.awt.event.*;
import javax.swing.*;
public class TwoFrames
{
private JFrame frame1, frame2;
private JPanel panel1, panel2;
private JButton button1, button2, button3;
private ActionListener action;
public TwoFrames()
{
frame1 = new JFrame("Frame One");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2 = new JFrame("Frame Two");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1 = new JPanel();
action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == button1)
{
// Here goes your code for displaying your Second Frame.
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
if (!frame2.isShowing())
{
panel2 = new JPanel();
button2 = new JButton("Click Me to HIDE FRAME.");
button2.setHorizontalTextPosition(AbstractButton.CENTER);
button2.setVerticalTextPosition(AbstractButton.CENTER);
button2.addActionListener(action);
panel2.add(button2);
panel2.setOpaque(true);
frame2.setContentPane(panel2);
frame2.setSize(200, 200);
frame2.setLocationRelativeTo(null);
frame2.setVisible(true);
}
}
});
button3.setEnabled(false);
}
else if (ae.getSource() == button2)
{
frame2.dispose();
button3.setEnabled(true);
}
}
};
button1 = new JButton("Click Me to Display FRAME.");
button1.setHorizontalTextPosition(AbstractButton.CENTER);
button1.setVerticalTextPosition(AbstractButton.CENTER);
button1.addActionListener(action);
button3 = new JButton("Watch Me getting DISABLED");
button3.setHorizontalTextPosition(AbstractButton.CENTER);
button3.setVerticalTextPosition(AbstractButton.CENTER);
button3.addActionListener(action);
panel1.add(button1);
panel1.add(button3);
panel1.setOpaque(true);
frame1.setContentPane(panel1);
frame1.setSize(200, 200);
frame1.setVisible(true);
}
public static void main(String... args)
{
// Here we are Scheducling a JOB for Event Dispatcher Thread.
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TwoFrames();
}
});
}
}

Categories