I have two jFrames: Frame1 and Frame2.
Frame1 has a jComboBox and a jButton; Frame2 has just a jButton.
Frame1 can open Frame2.
I have this code on Frame 1:
public class Frame1 extends javax.swing.JFrame {
public void addTextToComboBox(){
this.jComboBox1.removeAllItems();
this.jComboBox1.addItem("Hello");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.addTextToComboBox();
}
}
It works fine: The "Hello" string is added to the jComboBox when i click the jButton.
Now I have this code on Frame2:
public class Frame2 extends javax.swing.JFrame {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Frame1 frame1=new Frame1();
frame1.addTextToComboBox();
}
}
This way, the "Hello" string is not added to the jComboBox on Frame1 when i click on the jButton on Frame2.
Why? Could someone get me a solution please?
Thanks in advance.
Because you are trying to add string to another instance of jComboBox on Frame1 that is not showing now.
If you want to add string to jComboBox that is showing now, you need to pass the object of Frame1 to Frame2 then call addTextToComboBox();.
Example:
One of the way you can write your Frame2 class as
public class Frame2 extends javax.swing.JFrame {
Frame1 frame1;
public Frame2(Frame1 frame1) {
this.frame1 = frame1;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
frame1 = new Frame1();
frame1.addTextToComboBox();
}
}
And using it
public static void main(String[] args) {
Frame1 f1 = new Frame1();
Frame2 f2 = new Frame2(f);
}
You can read Object-Oriented Programming Concepts for better understanding of OOP concept.
Related
Started of with java coding for school. when i create the following code it won't work.
Please help me out.
import javax.swing.*;
import java.awt.*;
public class StartScherm extends JFrame
{
public static void main( String[] args ){
JFrame frame = new StartScherm();
frame.setSize( 800, 800 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setTitle( "CityTours" );
JPanel Paneel = new Paneel();
frame.setContentPane( Paneel );
frame.setVisible( true );
}
}
class Paneel extends JPanel {
private JButton Eng, LoginAdmin, LoginUser;
private JTextField Text;
public Paneel(){
setLayout (null);
Eng = new JButton ("Bring me to the English version");
Eng.setBounds(250,20,300,20);
Eng.addActionListener(newEngHandler());
Text = new JTextField (" Welkom bij CityTours ");
Text.setBounds(100,80,600,600);
Text.setEditable (false);
LoginAdmin = new JButton ("Login administrator");
LoginAdmin.setBounds(100,720,200,20);
LoginUser = new JButton ("Login gebruiker");
LoginUser.setBounds(500,720,200,20);
add (Eng);
add (Text);
add (LoginAdmin);
add (LoginUser);
}
class EngHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
this.dispose();
new MainScreen().setVisible(true);
}
}
}
I'm trying to create a JButton called Eng to close the frame of StartScherm.Java and open a frame of MainScreen.Java (all in the same project)
All it does is create 3 JButtons and a JTextField, and the Eng JButton action won't work. (error: cannot find symbol)
Please help me and explain to me what i do wrong.
It is not clear from your code what is MainScreen but, let me give you an idea on how you can switch between two JFrame windows with a button click.
You need access to isVisible() and setVisible() methods of JFrame in order to hide/display a window. Since, I do not know what MainScreen is, I will use StartScherm and create two instance of it (two windows with different titles) and then switch between the two.
Declare two instances of StartScherm outside your main method in StartScherm as shown below:
public class StartScherm extends JFrame {
static JFrameCloseEvent frame1;
static JFrameCloseEvent frame2;
public static void main(String[] args) {
//your main code initializing frame1 and frame2 goes here
}
}
The reason we declare frame1 and frame2 outside main method is because we want to be able to reference them in Paneel class. The reason they are static is because we initialize them in main method and main is static that is why.
Now that we have to instances of StartScherm as frame1 and frame2 we can initialize them differently in main method of StartScherm, see below:
//already declared frame1 outside main method
frame1 = new JFrameCloseEvent();
frame1.setSize(800, 800);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setTitle("CityTours Default Language");
JPanel Paneel = new Paneel();
frame1.setContentPane(Paneel);
//frame1 be visible when program start
frame1.setVisible(true);
And we will will initialize the frame2 in a similiar fashion but, with different title and set its visibility to false. See below:
frame2 = new JFrameCloseEvent();
frame2.setSize(800, 800);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setTitle("CityTours English Version");
JPanel Paneel2 = new Paneel();
frame2.setContentPane(Paneel2);
//set visibility to false because frame1 is visible at beginning
frame2.setVisible(false);
Now that we have both frame1 and frame2 initialized. We can tweak our Paneel to properly implement an ActionListener and switch visibility of the frame1 and frame2. In order to set ActionListener on a button, I prefer the following approach:
Eng = new JButton("Bring me to the English version");
Eng.setBounds(250, 20, 300, 20);
Eng.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(StartScherm.frame1.isVisible()) {
StartScherm.frame1.setVisible(false);
StartScherm.frame2.setVisible(true);
} else {
StartScherm.frame1.setVisible(true);
StartScherm.frame2.setVisible(false);
}
}
});
As you can see above, I attach the ActionListener straight to button. Since both frame1 and frame2 use Paneel hence, they will get the same button with action listener. When the button is clicked, the actionPerformed is executed which checks whether frame1 is visible or frame2. If frame1 is visible then it will set its visibility to false and set visibility of frame2 to true, else other way round.
Make above changes to your code, that is properly implement ActionListener as shown above, and make sure that your frame1 and frame2 instances are accessible in actionPerformed method.
I have modified your code to make it runnable, see below (obviously I removed references to MainScreen because, the only difference between frame1 and frame2 is title).
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StartScherm extends JFrame {
static StartScherm frame1;
static StartScherm frame2;
public static void main(String[] args) {
frame1 = new StartScherm();
frame1.setSize(800, 800);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setTitle("CityTours Default Language");
JPanel Paneel = new Paneel();
frame1.setContentPane(Paneel);
frame1.setVisible(true);
frame2 = new StartScherm();
frame2.setSize(800, 800);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setTitle("CityTours English Version");
JPanel Paneel2 = new Paneel();
frame2.setContentPane(Paneel2);
frame2.setVisible(false);
}
}
class Paneel extends JPanel {
private JButton Eng, LoginAdmin, LoginUser;
private JTextField Text;
public Paneel() {
setLayout(null);
Eng = new JButton("Bring me to the English version");
Eng.setBounds(250, 20, 300, 20);
Eng.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(StartScherm.frame1.isVisible()) {
StartScherm.frame1.setVisible(false);
StartScherm.frame2.setVisible(true);
} else {
StartScherm.frame1.setVisible(true);
StartScherm.frame2.setVisible(false);
}
}
});
Text = new JTextField(" Welkom bij CityTours ");
Text.setBounds(100, 80, 600, 600);
Text.setEditable(false);
LoginAdmin = new JButton("Login administrator");
LoginAdmin.setBounds(100, 720, 200, 20);
LoginUser = new JButton("Login gebruiker");
LoginUser.setBounds(500, 720, 200, 20);
add(Eng);
add(Text);
add(LoginAdmin);
add(LoginUser);
}
}
Eng.addActionListener(newEngHandler());
should be
Eng.addActionListener(new EngHandler());
newEngHandler() would denote a method which cannot be found new EngHandler() would denote instantiating a new class
Basically I have 1 main class which extends JFrame and implements ActionListener and i have created a window which is my main window. Now I wanted to make another window which opens when I click a button on the main window. So to do this I created another class which extends JFrame and created another window. Now I am unsure how to set it so that the new window I created opens when I click a button in the main window. I have my actionPerformed in that main class. Im not sure what statement would make my window pop up.
What you want to do is have a listener on the main window "Add credits" button. That is, you want to implement ActionListener in your SlotMachine class (that's the class containing the button, right?).
You will indeed want to add this code to your SlotMachine class, and not in the other one :
public void actionPerformed(ActionEvent e){
String butonCommand = e.getActionCommand();
if(butonCommand.equals("Add Credits")){
// The add credits button has been fired. Make the other window visible
}
}
You want to make the add credits window visible, that is, you want to do the following :
addCreditsFrame.setVisible(true);
If you add the listener in your main class, it needs to have access to the frame that has to be visible. You need to send a new AddCreditsClass to your SlotMachine constructor so he can set it visible. You can't use AddCreditsClass.setVisible(true) because you're setting a frame visible, not the entire class.
That is, something along the lines of :
public class SlotMachine {
AddCreditsClass otherframe;
public SlotMachine(AddCreditsClass otherFrame) {
this.otherFrame=otherFrame;
// ...
}
public void actionPerformed(ActionEvent e){
String butonCommand = e.getActionCommand();
if(butonCommand.equals("Add Credits")){
otherFrame.setVisible(true);
}
}
}
You would have the following code :
public static void main(String... args) {
SlotMachine mainFrame = new SlotMachine(new AddCreditsClass());
mainFrame.setVisible(true); // The main window has to be visible at the beginning
public class AddCreditsClass extends JFrame implements ActionListener{
public static final int WIDTH = 700;
public static final int HEIGHT = 500;
private static JLabel addCreditsLabel;
public AddCreditsClass(){
super("Add Credits");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
// setVisible(true); Do _not_ do this! You want the window to be hidden until you set it visible by yourself.
}
// No listener here, the button listener should be in the main class
}
In your SlotMachine class, which contains the "add credits" button, you would add the listener I mentioned earlier :
public void actionPerformed(ActionEvent e){
String butonCommand = e.getActionCommand();
if(butonCommand.equals("Add Credits")){
addCreditsFrame.setVisible(true);
}
}
Duh! Sorry I hadn't seen you answered before me. Should I delete this answer?
Well, when the button is clicked you want actionPerformed() to be called. But the button is in SlotMachine so actionPerformed() needs to be in the SlotMachine class as well. You didn't post SlotMachine but it would look something like this:
public class SlotMachine extends JFrame implements ActionListener {
public static void main(String[] args) {
SlotMachine mainFrame = new SlotMachine();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(640, 480);
mainFrame.setLocationRelativeTo(null); // center on screen
mainFrame.setVisible(true);
}
public SlotMachine() {
setLayout(new BorderLayout());
JButton button = new JButton("Open new window");
button.setActionCommand("Add Credits");
button.addActionListener(this);
add(button, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent e) {
String butonCommand = e.getActionCommand();
if (butonCommand.equals("Add Credits")) {
AddCreditsClass addCredits = new AddCreditsClass();
addCredits.setLocationRelativeTo(this);
addCredits.setVisible(true);
}
}
}
In SlotMachine's constructor we add a button and add a listener to the button. So when that button is pressed actionPerformed() will be called. And as you can see in actionPerformed() we create a new instance of AddCreditsClass and make it visible.
We moved most code from AddCreditsClass to SlotMachine but what is left looks like this:
public class AddCreditsClass extends JFrame {
public static final int WIDTH = 700;
public static final int HEIGHT = 500;
private static JLabel addCreditsLabel;
public AddCreditsClass() {
super("Add Credits");
setSize(WIDTH, HEIGHT);
setLayout(null);
}
}
I'm trying to parse String to another frame Using JAVA
i have 2 jFrames. jFrame1 have 1 text field and jFrame 2 have 1 text field. i want to parse jFrame1 text filed's text to jframe2's text field.
It's look like this : But this is not code :(
jFrame2.textfield1.setText(jFrame1.textfield1.gettext());
Anyone know how to parse String to another frame Using JAVA?
Thanks in advance!
Assuming that you have two separate GUIs on the screen at the same time because both textFields have the same reference, each JFrame will be a Object in its own right.
Therefore the only way to access another objects variables is with its methods.
Create a setter method in jFrame2 to change the textField in question.
See Working code below.
import java.awt.*;
import javax.swing.*;
import javax.swing.BoxLayout;
import java.awt.event.*;
public class JframeLink {
public static void main(String[] args)
{
new JframeOneGui();
new JframeTwoGui();
}
//JFrame one Object
public static class JframeOneGui extends JFrame implements ActionListener
{
JPanel jPanelOne = new JPanel();
JTextField textField1 = new JTextField("Message for transfer");
JButton buttonOne = new JButton("Transfer");
public JframeOneGui()
{
//setup swing components
textField1.setSize(100,10);
buttonOne.addActionListener(this);
//setup jPanelOne
jPanelOne.setLayout(new BoxLayout(jPanelOne, 1));
jPanelOne.add(textField1);
jPanelOne.add(buttonOne);
//setup JframeOneGui
this.add("Center",jPanelOne);
this.setLocation(25,25);
this.setTitle("JframeOneGui");
this.setSize(200,200);
this.setResizable(false);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == buttonOne)
{
//Here we are calling JframeTwoGui's Setter method
JframeTwoGui.setTextFieldOne(textField1.getText());
}
}
}
//JFrame two Object
public static class JframeTwoGui extends JFrame
{
JPanel jPanelOne = new JPanel();
static JTextField textField1 = new JTextField();
public JframeTwoGui()
{
jPanelOne.setLayout(new BoxLayout(jPanelOne, 1));
jPanelOne.add(textField1);
this.add("Center",jPanelOne);
this.setLocation(300,25);
this.setTitle("JframeTwoGui");
this.setSize(200,200);
this.setResizable(false);
this.setVisible(true);
}
//Setter to change TextFieldOne in this Object
public static void setTextFieldOne(String text)
{
textField1.setText(text);
}
}
}
I have a basic GUI class with a frame, a table, and a button. I want to make it launch from the ActionListener of another one of my basic GUI frames located in a different class
this is my main class:
public class IA {
public static void main(String[] args) {
MainFrame m1 = new MainFrame();
m1.setVisible(true);
} /*enter code here*/
public static void vts1 () {
ViewTeamStatistics v1 = new ViewTeamStatistics();
v1.setVisible(true);
}
}
It initiates my main menu and from the main menu i want to initiate another class named ViewTeamStatistics
this is the actionperformed. this is what is supposed to tell the program to open the frame after i press the button
private void vtsActionPerformed(java.awt.event.ActionEvent evt) {
ViewTeamStatistics v1 = new ViewTeamStatistics();
v1.setVisible(true);
}
The compiler comes back with no errors but when I run the program and press the button nothing happens.
I don't fully understand your question, do you want to launch a new frame upon pressing a button? If that's it here is a sample code :
public class ExampleWindow implements ActionListener{
private JFrame mainFrame;
private JButton button;
public ExampleWindow(){
button = new JButton("Press me!");
button.addActionListener(this);
mainFrame = new JFrame("Frame name");
mainFrame.add(button);
mainFrame.setVisible(true);
//Remember about this line
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e) {
new SomeWindow();
}
}
class SomeWindow{
private JFrame frame;
public SomeWindow(){
frame = new JFrame;
frame.setVisible(true);
}
}
I didn't try to compile it so there might be some errors.
I am having problem with JFrames.
Currently I have 2 JFrames running,
MainFrame with a button to call Frame2.
And from Frame2 with JButton, I wan to call the current running/background MainFrame without calling another new MainFrame.
Actually I am making a search function on Frame2 and when click button search, wanna display the results in the Main Frame.
If Frame2 inherits MainFrame then do this:
Frame2.getParent().getBackground();
There are a multitude of solutions for such problems. It really depends on what best suits your use case.
In the example below I use an interface to issue callbacks to MainFrame from Frame2. I assume the latter is a member of MainFrame. This sort of solution makes it easy for you to use the same Frame2 implementation in multiple implementations of MainFrame (a common search for more than one frame).
Note that code below is just skeleton code to demonstrate the pattern being used.
public class Frame2 extends JFrame {
private final Controller controller;
private JButton button;
public Frame2(Controller controller) {
this.controller = controller;
button = new JButton("Search");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// do search and create result object
Object results = new Object();
Frame2.this.controller.displaySearchResults(results);
}
});
}
public interface Controller {
// users implement this
public void displaySearchResults(Object results);
}
}
public class MainFrame extends JFrame {
private Frame2 search;
private JButton button;
public MainFrame() {
search = new Frame2(new ControllerImplementation());
button = new JButton("Show search");
button.addActionListener(new ShowSearch());
}
private class ShowSearch implements ActionListener {
public void actionPerformed(ActionEvent e) {
search.setVisible(true);
}
}
private class ControllerImplementation implements Frame2.Controller {
public void displaySearchResults(Object results) {
// display results by accessing members of MainFrame
}
}
}
This may help you achieve what you want.