Removing a textfield from JFrame dynamically - java

I want to remove the text field upon selecting the radio button "don't show field" but unable to do so. I have used repaint and revalidate methods. I also tried making the field invisible by setting visibility to false but nothing seems to be working.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
new Frame();
}
}
class Frame
{
private JFrame frame;
private JButton button;
private ImageIcon img;
private JRadioButton b1;
private JRadioButton b2;
private JTextField text;
public Frame()
{
frame = new JFrame();
text=new JTextField(10);
b1=new JRadioButton("show Field");
b2=new JRadioButton("Dont show field");
ButtonGroup bg = new ButtonGroup();
bg.add(b1);
bg.add(b2);
img = new ImageIcon("Letter-S-icon.png");
b1.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent arg) {
if(arg.getSource()==b1)
{
frame.add(text);
frame.revalidate();
frame.repaint();
}
else if(arg.getSource()==b2)
{
text.setVisible(false);
frame.remove(text);//cannot remove this even after repaint();
frame.revalidate();
frame.repaint();
}
}
});
frame.add(b1);
frame.add(b2);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setBounds(500, 200, 200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(img.getImage());
}
}

Related

ActionListener works but it never stops checking for user input and does not execute code after

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
public class Main {
String newLine = System.getProperty("line.separator");
public boolean isTrue = false;
public boolean isOn = false;
JFrame frame;
JLabel headerLabel;
JPanel controlPanel;
JTextArea textArea;
JTextField textField;
public Main (){
JPanel controlPanel = new JPanel();
JPanel buttonPanel = new JPanel();
frame = new JFrame("Test");
frame.setSize(800,600);
frame.setLayout(null);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
textArea = new JTextArea();
buttonPanel.add(textArea);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
controlPanel.add(scrollPane);
scrollPane.setVisible(true);
textField= new JTextField();
controlPanel.add(textField);
JButton btn = new JButton("btn");
buttonPanel.add(btn);
controlPanel.add(buttonPanel, BorderLayout.SOUTH);
headerLabel = new JLabel("", JLabel.CENTER);
frame.add(controlPanel);
frame.add(btn);
frame.add(headerLabel);
frame.add(textField);
frame.add(scrollPane);
frame.setVisible(true);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn.setVisible(false);
textArea.append(newLine+"You pressed the btn");
isOn=true;
textField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(isOn){
isOn=false;
isTrue=true;
String name=textField.getText();
textArea.append(newLine+"your chose the name"+name );
//... irrelevant code
if(isTrue){
System.out.println("This is true");
}
} else{
textArea.append(newLine+"Its not time yet.");
}
}
});
}
});
System.out.println(isTrue)
}
public static void main(String[] args) {
new Main();
}
}
Once the method reaches actionPerformed it seems to never leave. However whenever there is a new input in textField it prints out the statement in if isTrue but bellow the ActionListener isTrue is false. Some help understand correcting and understanding this would be fantastic.

How to add a button to a JFrame Gui

I'm trying to add a button to a frame gui.
i tried making a panel and adding it to that, but it does not work.
please help!
here is my code:
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton button;
JPanel panel;
// my error lines are under the "panel" and "button"
// it says i must implement the variables. what does that mean???
panel.add(button);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
Change:
JButton button;
JPanel panel;
to:
JButton button = new JButton();
JPanel panel = new JPanel();
You can also pass a String value in JButton() constructor for that string value to be shown on the JButton.
Example: JButton button = new JButton("I am a JButton");
Example Code:
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
// Create JButton and JPanel
JButton button = new JButton("Click here!");
JPanel panel = new JPanel();
// Add button to JPanel
panel.add(button);
// And JPanel needs to be added to the JFrame itself!
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
Output:
Note:
Create the JButton and JPanel using new JButton("..."); and new JPanel()
Add the JPanel to the JFrame's content pane using getContentPane().add(...);
If you can Change this Program, You can adjust the button place also
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class agui extends JFrame
{
agui()
{
setTitle("My GUI");
setSize(400,400);
setLayout(null);
JButton button = new JButton("Click Here..!");
button.setBounds(50,100,100,50); /*Distance from left,
Distance from top,length of button, height of button*/
add(button);
setVisible(true);
}
public static void main(String[] args)
{
JFrame agui = new agui();
}
}
Output

Centering Swing Components

How do I make my textfields and button centered?
Edit: Updated my post with SSCE. Hopefully that helps.
By the way, I want the left image to look like the right.
package pkg;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
public class Game {
private static JPanel panel = new JPanel();
public static JTextField username = new JTextField(20);
public static JPasswordField password = new JPasswordField(20);
JButton login = new JButton("Login");
JLabel status = new JLabel();
private static JPanel game = new JPanel();
private JButton logout = new JButton("Logout");
private static JFrame frame = new JFrame("RuneShadows");
public Game() {
panel.add(username);
panel.add(password);
panel.add(login);
panel.add(status);
game.add(logout);
frame.add(panel);
frame.setSize(806, 553);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new Game();
}
public void loadGame() {
frame.remove(panel);
frame.revalidate();
frame.add(game);
frame.revalidate();
logout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(game);
frame.revalidate();
frame.add(panel);
frame.revalidate();
try {
Client.socketOut.writeUTF("logout");
Client.socketOut.writeUTF(Client.username);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
}
What I want it to look like:
I've tried to use many different layout styles but I can't seem to make it work...
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.border.*;
public class Game {
private static JPanel panel = new JPanel();
public static JTextField username = new JTextField(20);
public static JPasswordField password = new JPasswordField(20);
JButton login = new JButton("Login");
JLabel status = new JLabel();
private static JPanel game = new JPanel(new FlowLayout(FlowLayout.CENTER));
private JButton logout = new JButton("Logout");
private static JFrame frame = new JFrame("RuneShadows");
public Game() {
panel.setLayout(new GridLayout(0,1,15,15));
panel.setBorder(new EmptyBorder(50,100,50,100));
panel.add(username);
panel.add(password);
panel.add(status);
JPanel logoutConstrain = new JPanel(new FlowLayout(FlowLayout.CENTER));
logoutConstrain.add(logout);
panel.add(logoutConstrain);
frame.setLayout(new GridBagLayout());
frame.add(panel);
//frame.setSize(806, 553); // forget this nonsense, instead..
frame.pack(); // best!
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new Game();
}
public void loadGame() {
frame.remove(panel);
frame.revalidate();
frame.add(game);
frame.revalidate();
logout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(game);
frame.revalidate();
frame.add(panel);
frame.revalidate();
}
});
}
}

Swing Methods on submission of button

How to close current frame (Frame1) and open a new frame (Frame2) already created and pass the data to frame2 from frame1 on the clicking of button?
Use a CardLayout1. Either that or one JFrame and one or more JDialog2 instances.
How to Use CardLayout
How to Make Dialogs
The very best way to accomplish this, is very much told to you by #Andrew Thompson.
And the other way to accomplish, the motive of the question as described in the code. Here as you make object of your new JFrame, you have to pass the things you need in the other class as an argument to the other class, or you can simply pass the object(with this you be passing everything in one go to the other class)
A sample code for a bit of help :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TwoFramesExample
{
public JFrame frame;
private JPanel panel;
private JButton button;
private JTextField tfield;
private SecondFrame secondFrame;
public TwoFramesExample()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new BorderLayout());
tfield = new JTextField(10);
tfield.setBackground(Color.BLACK);
tfield.setForeground(Color.WHITE);
button = new JButton("NEXT");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
// Here we are passing the contents of the JTextField to another class
// so that it can be shown on the label of the other JFrame.
secondFrame = new SecondFrame(tfield.getText());
frame.dispose();
}
});
frame.setContentPane(panel);
panel.add(tfield, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TwoFramesExample();
}
});
}
}
class SecondFrame
{
private JFrame frame;
private JPanel panel;
private JLabel label;
private JButton button;
private TwoFramesExample firstFrame;
public SecondFrame(String text)
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new BorderLayout());
label = new JLabel(text);
button = new JButton("BACK");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
firstFrame = new TwoFramesExample();
frame.dispose();
}
});
frame.setContentPane(panel);
panel.add(label, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
}
Hope this be of some help.
Regards

Swing JLabel text change on the running application

I have a Swing window which contains a button a text box and a JLabel named as flag. According to the input after I click the button, the label should change from flag to some value.
How to achieve this in the same window?
Use setText(str) method of JLabel to dynamically change text displayed. In actionPerform of button write this:
jLabel.setText("new Value");
A simple demo code will be:
JFrame frame = new JFrame("Demo");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(250,100);
final JLabel label = new JLabel("flag");
JButton button = new JButton("Change flag");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
label.setText("new value");
}
});
frame.add(label, BorderLayout.NORTH);
frame.add(button, BorderLayout.CENTER);
frame.setVisible(true);
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener
{
private JLabel label;
private JTextField field;
public Test()
{
super("The title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(400, 90));
((JPanel) getContentPane()).setBorder(new EmptyBorder(13, 13, 13, 13) );
setLayout(new FlowLayout());
JButton btn = new JButton("Change");
btn.setActionCommand("myButton");
btn.addActionListener(this);
label = new JLabel("flag");
field = new JTextField(5);
add(field);
add(btn);
add(label);
pack();
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("myButton"))
{
label.setText(field.getText());
}
}
public static void main(String[] args)
{
new Test();
}
}

Categories

Resources