Change value of public variable in Java, used by multiple frames - java

I have two frames in NetBeans 9.0 as frame1.java, frame2.javaand the main class as main.java.
If I declare a public variable in frame1.java as
public String stringName;
and a function fn() which gives the value of stringName in frame1as say "abcd".
When I write this in frame2,
frame1 fm = new frame1();
String str = frame1.stringName;
System.out.print(str);
I get the output as null. But what I require is "abcd".
What am I doing wrong, and what should it be?
Thanks for help!
Edit:
I have linked frame1 and frame2 such that the GUI from frame1 leads to frame2, and so does the value.
Edit 2
The process goes like this:
GUI of frame1 is visible >> based on user's input, function fn() stores the value, say "abcd" in stringName >> a button click in frame1 leads to frame2>> variable str gets the value from stringName >> System.out.print(str) outputs the value as null .
CODE
frame1:
public class frame1 extends javax.swing.JFrame {
public String stringName;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
stringName = jTextField1.getText(); // gets a not null value
}
}}
frame2:
public class frame2 extends javax.swing.JFrame {
frame1 fm = new frame1();
String str = frame1.stringName;
System.out.print(str); //outputs a null value
}

The point ist that you are crating a new Instance (frame1, fm) in your class frame2. So the value from the string in this new Instance is null. You need a reference to your old Instance which you maybe have initialised in your main method?
Something like that:
String str = myOldInstance.stringName;
But you should create getter an setter and make your var private.
But to help you exactly we need more Code.

in this case the best is Listener pattern.
Create interface of listener, which will inform about change text. In class - target of this information - create instance of this listener and return that. In class - source of information - set listener and put on field.
When you want inform of change text, you fire method of listener, and on seconde frame will execute implementation of method.
Below example - I fire on button click.
Any way, field should be private, and add getter and setter. Public fields are bad.
Main class
public class App {
public static void main(String[] args) {
Frame1 f1=new Frame1();
Frame2 f2=new Frame2();
TextListener textListener = f2.getListener();
f1.setListener(textListener);
}
}
Listener
public interface TextListener {
public void onTextPropagate(String text);
}
Frame classes
public class Frame1 extends JFrame{
private TextListener listener;
JButton button;
public Frame1() {
super("Frame1");
setBounds(200, 200, 400, 600);
button=new JButton("Action");
button.setBounds(100, 200, 200, 100);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(listener!=null) {
String text = UUID.randomUUID().toString();
System.out.println("On Frame1:\t"+text);
listener.onTextPropagate(text);
}
}
});
this.add(button);
setVisible(true);
}
public void setListener(TextListener listener) {
this.listener=listener;
}
}
public class Frame2 extends JFrame{
public Frame2() {
super("Frame2");
setBounds(100, 100, 200, 400);
setVisible(true);
}
public TextListener getListener() {
return new TextListener() {
#Override
public void onTextPropagate(String text) {
reactOnChangeText(text);
}
};
}
private void reactOnChangeText(String text) {
System.out.println("On Frame2:\t"+text);
}
}

Related

Changing a TabbedPane index from an outer class

I have a TabbedPane in a class called App and i want to run a method in this class. I added two tabs with a JPanel from the class Login and an empty one. Here is the class:
public class App {
private static JTabbedPane tabbedPane;
public JPanel mainPanel;
public App(){
tabbedPane.addTab("Login", new Login().mainPanel);
tabbedPane.addTab("test", new JPanel());
changeFocus(0);
}
public void changeFocus(int i){
//CODE HERE
}
}
Now i want to run a method called changeFocus() from an outer class. A added an actionListener to the Login class with a constructor like this:
public Login() {
logInButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
App.changeFocus(1);
}
});
}
Now i ask why this doesn´t work and changeFocus() must be static. And if i change it to static why the JTabbedPane cannot be static and throws out an error.
Simply pass App as an argument to Login's constructor:
tabbedPane.addTab("Login", new Login(this).mainPanel);
and then:
public Login(App app) {
logInButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
app.changeFocus(1);
}
});
}

How to Show output Jlabel another Jframe

When i input Text in the JtextField of FrameIn, and then click button OK, the Text will display on the Jfield of FrameShow the last frame is what I want, cause I still don't know how to make it.
I am using NetBeans GUI builder.
package learn;
public class FrameIn extends javax.swing.JFrame {
private String Name = null;
public FrameIn() {
initComponents();
}
*
*
private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {
FrameShow show = new FrameShow();
Name = Text.getText();
this.dispose();
show.setVisible(true);
}
public String getName(){
return this.Name;
}
and This my FrameShow
public class FrameShow extends javax.swing.JFrame {
/**
* Creates new form Frame1
*/
public FrameShow() {
FrameIn inName = new FrameIn();
initComponents();
Label.setText(inName.getName());
}
So if i input Text in the JtextField of FrameIn, then output will display on the Jfield of FrameShow second Jframe
Output form this code is null on the Jfield
You can pass your parametres between the two Frame,
so when you click a your button, make an action that call your frameShow, and you can pass your values, in the constructor of your frame or you can create a field in your second frame and use setter to put your value, here is the idea.
class A{
...
//action
String v = textField.getText();
B b = new B(v);
...
}
class B{
public B(String v){
this.label.setText(v);
}
}
Second idea :
class A{
...
//action
String v = textField.getText();
B b = new B();
b.setLabelValue(v);
...
}
Here is your code should be look like:
private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {
Name = Text.getText();
FrameShow show = new FrameShow(Name);
this.dispose();
show.setVisible(true);
}
public FrameShow(String name) {
initComponents();
Label.setText(name);
}
Hope you get my point and you understand the idea.

Accessing data from another class which reads values

Ok, so here is a problem: I have main class which generates a window and two buttons:
...
public MainWindow()
{
...
b_read.addActionListener(new ReadStudents());
b_open_all.addActionListener(new OpenStudents());
...
In ReadStudents.java class I load data from a file with JFileChooser and print it on the screen. Sample of code thats load file:
#Override
public void actionPerformed(ActionEvent e) {
...
JFileChooser fc = new JFileChooser(".");
...
int rez = fc.showOpenDialog(fc);
if (rez == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
...
So now in my ReadStudents.java class variable "file" has my loaded information.
How do i pass this variable with containing information in it to a class that prints Students on the screen (second button OpenStudents.java) ?
Edit:1) I can not initialize an object of ReadStudents.java in OpenStudents.java class because in the new object variable "file" will be empty. Something is loaded to a "file" only when a button b_read is pressed.
One option would be to implement the action listener on your MainWindow class and the call a method in the ReadStudents/OpenStudents that return the list of files.
For example:
/**
* Main window class.
*/
public static void main(String args[]) {
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.setSize(100, 100);
final StudentReader student = new StudentReader();
JButton button = new JButton("Click Me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Make the call here. Take note!
// Look at the return type!
List<String> strings = student.fileNames();
for (String s : strings) {
System.out.println(s);
}
}
});
mainFrame.add(button);
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
}
public class StudentReader {
public List<String> fileNames() {
// Do your magic here :)
// Open a dialog. Get the files.
// Return it as a list
return Arrays.asList(new String[]{"Filename"});
}
}
I Hope this helps!

JButton not working [capture network traffic]

My code which is supposed to capture the network traffic AND display it on textarea but it ain't doing it.Please have a look at the code and check if there is any correction to be made.
public class NewClass {
public static JTextArea textarea = new JTextArea();
NewClass() throws IOException{
JButton capture = new JButton("Capture");
JFrame frame = new JFrame();
JScrollPane scroll;
NetworkInterface[] NI= JpcapCaptor.getDeviceList();
int INDEX=0;
JpcapCaptor JPCAP = JpcapCaptor.openDevice(NI[INDEX], 65536, false, 20);
frame.setSize(700,500);
frame.setLocation(200,200);
frame.getContentPane().setLayout(null);
frame.setBackground(Color.yellow);
textarea.setEditable(false);
textarea.setFont(new Font("Tahoma",0,14));
textarea.setForeground(Color.RED);
textarea.setLineWrap(true);
//textarea.setBackground(Color.WHITE);
scroll = new JScrollPane();
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setViewportView(textarea);
frame.getContentPane().add(scroll);
scroll.setBounds(10,16,740,290);
capture.setBackground(Color.RED);
capture.setForeground(Color.GREEN);
frame.getContentPane().add(capture);
handler ob = new handler();
capture.addActionListener(ob);
capture.setBounds(100, 400, 90, 25);
frame.setVisible(true);
}
public class handler implements ActionListener{
public void actionPerformed(ActionEvent Event){
class Print implements PacketReceiver{
public void receivePacket(Packet packet){
String info = packet.toString();
textarea.append(info);
//System.out.println(packet.toString());
}
}
}
}
My code which is supposed to capture the network traffic AND display it on textarea but it ain't doing it.Please have a look at the code and check if there is any correction to be made.
Well for one, your handler's actionPerformed method doesn't really do anything. It defines an inner class but creates no objects of this class and does nothing with it:
public class handler implements ActionListener {
public void actionPerformed(ActionEvent Event) {
class Print implements PacketReceiver {
public void receivePacket(Packet packet) {
String info = packet.toString();
textarea.append(info); // System.out.println(packet.toString());
}
}
}
}
Consider creating an object of your Print class (a terrible name for a class since there already is a Print class that is part of the core Java libraries) and having this Print object do something useful, perhaps receive packets (however it's supposed to do that). Be careful not to do long-running processes in the main Swing thread, the EDT though, or you'll freeze your Swing GUI.
Edit
e.g.,
// note that class names such as "Handler" should begin with a capital letter.
public class Handler implements ActionListener {
public void actionPerformed(ActionEvent Event) {
class Print implements PacketReceiver {
public void receivePacket(Packet packet) {
String info = packet.toString();
textarea.append(info); // System.out.println(packet.toString());
}
}
// create a Print instance so that it can do something:
final Print myPrint = new Print();
// do something with myPrint here so that it gets packets and displays them
// I suspect that you'll likely want to do this in a background thread
// using a SwingWorker
}
}
as Hovercraft said you are doing nothing but defining an inner class. what you are missing here is the call to processPacket() or loopPacket() from your JPCAP class. so what i suggess is the folowing :
public class handler implements ActionListener{
public void actionPerformed(ActionEvent Event){
class Print implements PacketReceiver{
public void receivePacket(Packet packet){
String info = packet.toString();
textarea.append(info);
}
}
// this captures 10 packets .
JPCAP.processPacket(10,new Print());
}
}

A simple question regarding ActionListener and 'enter' key

I am working on an assignment, and I need to enter an SQL Query in a textfield. The user can either press the custom 'execute query' button, or they can press the enter key. When either of these are used, it is to trigger an ActionListener (no other listener is allowed). Is it as simple as writing:
if (e.getSource()=='querybutton' || e.getSource=='enter')
Or is there more to it than this?
As I said, it is a simple question (I know).
edit:
I would write this bit in my ActionPerformed as:
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==gui.executeQueryButton || e.getSource()==gui.enter)
{
String query = gui.queryText.getText();
//more code to follow
}
}
e.getSource() actually returns the object responsible for firing the event (not the name of the variable you used when creating the control). In this case, your button. You could in principle compare e.getSource() with the actual button instances. However, are you actually adding this action listener to buttons other than those two? Presumably you'd only have to add this listener to the two buttons for which you want this behavior -- in which case you wouldn't have to have this if check.
" Is it as simple as writing:
if (e.getSource()=='querybutton' || e.getSource=='enter')"
It's not simple to write this, but rather it is wrong to write it.
For one you don't want to compare Strings with ==, for another, you don't declare Strings with single quotes, and for a third, the enter key is not obtained in this way, but rather by adding the appropriate ActionListener object to the JTextField itself, and finally there should be in a single ActionListener class that handles this action, so the if block is completely unnecessary. This can probably be best done with a small inner private ActionListener class. You'd then create one object of this class and add it as an ActionListener for the querybutton and for the JTextField.
edit 1:
A more complete example of what I mean is shown below, a demo class that has a private inner handler class:
import java.awt.event.*;
import javax.swing.*;
public class ActionListenerEg extends JPanel {
private JButton queryButton = new JButton("Query");
private JTextField textField = new JTextField("hello", 20);
public ActionListenerEg() {
QueryListener qListener = new QueryListener();
queryButton.addActionListener(qListener);
textField.addActionListener(qListener);
add(queryButton);
add(textField);
}
private class QueryListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String textInField = textField.getText();
System.out.println("Use text in field, \"" + textInField + "\" to call SQL query in a background SwingWorker thread.");
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("ActionListenerEg");
frame.getContentPane().add(new ActionListenerEg());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
The ActionListener is fired either by pressing the button or by pressing enter from within the JTextField. I'd then have in my control class, code that is called inside of the actinoPerformed method.
edit 2: Having most handler or "control" code in its own Handler or Control class can be a good idea, but it doesn't have to implement ActionListener interface itself, but rather just have the code that will be called from within the ActionListener codes. For example, here I try to put all the handler code in its own class. It will have different methods that are called for various situations. e.g.,
import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;
public class ActionListenerEg extends JPanel {
private ActionListenerHandler handler;
private JButton queryButton = new JButton("Query");
private JButton displayButton = new JButton("Display");
private JTextField textField = new JTextField("hello", 20);
// pass in handler or handler
public ActionListenerEg(final ActionListenerHandler handler) {
this.handler = handler;
QueryListener qListener = new QueryListener();
queryButton.addActionListener(qListener);
textField.addActionListener(qListener);
displayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (handler != null) {
handler.displayActionPerformed(e);
}
}
});
add(queryButton);
add(textField);
add(displayButton);
}
private class QueryListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (handler != null) {
String textInField = textField.getText();
handler.doQueryAction(e, textInField);
}
}
}
private static void createAndShowUI() {
ActionListenerHandler handler = new ActionListenerHandler();
JFrame frame = new JFrame("ActionListenerEg");
frame.getContentPane().add(new ActionListenerEg(handler));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class ActionListenerHandler {
public void displayActionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog((Component) e.getSource(), "Display things!");
}
public void doQueryAction(ActionEvent e, String textInField) {
String text = "We will use \"" + textInField + "\" to help create and run the SQL Query";
JOptionPane.showMessageDialog((Component) e.getSource(), text);
}
}
Please ask questions if it's clear as mudd, or if anything is wrong.

Categories