I have two JFrames.One Jframe has one Jtabel , when i selectedrow and press the JButton, second Jframe set string acd get text from jframe1
code in jframe1
int i = tblschdule.getSelectedRow() ;
String a = (String) tblschdule.getValueAt(i, 0) ;
String b = (String) tblschdule.getValueAt(i, 1) ;
Fisimonitoring form1 = new Fisimonitoring();
form1.acd=a;
form1.setVisible(true);
jframe2
public String acd;
code form1.acd=a; not work, String acd always null. how to solve it ?
your question is very incomplete bt here is an example to send data to another
frame
//a jframe
public class Aframe extends javax.swing.JFrame {
private Bframe bframe;
private void sendActionPerformed(java.awt.event.ActionEvent evt) {
if (bframe==null) {
bframe = new Bframe();
}
String text = jTable1.getValueAt(jTable1.getSelectedRow(),jTable1.getSelectedColumn()).toString();
bframe.setText(text);
bframe.setVisible(true);
}
}
//b jframe
public class Bframe extends javax.swing.JFrame {
public void setText(String text){
this.textField.setText(text);
}
}
in your case if you have already opened Bjframe you should make sure that same instance of Bjframe is shared by Ajframe.
It's quiet late for this answer but the solution for your problem is pretty simple.
You Need to send the value across the frame's.
Like if you are on jFrame1 and on click you are calling JFrame2
function actionPerformedOnButtonClick(){
String text = valueOfTheLabel;
new YourFrameName(text).setVisible(true);
}
And on JFrame2, You need to write a parameterized contructor like this..
public JFrame2(String textFromFrame1) {
initComponents();
}
And You can Access It on Frame2.
Related
I'm having a problem with something I don't understand. I have a java class that is building up my program and another class full of functions called by action listeners.
I'm hoping to use the the child's class functions to alter the contents of a JLabel (just an example) using a setter function which doesn't seem to be doing anything and not crashing.
My main class has
public class Parent extends JFrame {
static JPanel dummyPanel = new JPanel();
static JLabel dummyLabel = new JLabel("label");
static JButton dummyButton = new JButton("button");
static Child child = new Child();
public Parent() {
// main setup goes here
// add elements goes here
ActionListener alterLabel = new ActionListener() {
public void actionPerformed(ActionEvent dummyButton) {
child.childFunc();
}
}
dummyButton.addActionListener(alterLabel);
}
// main function goes here
public void newText() {
dummyLabel.setText("altered");
System.out.println("new text function has been executed");
}
}
Then the child I am using to call the function to change the text contains
public class Child {
public void childFunc() {
Parent parent = new Parent();
parent.newText();
}
}
When I click the button it runs and I see it outputs the string which I expect but it does not seem to change the label.
Why is this and is there a way to fix it?
New to java and i am unable to see why my action listener is not working on the jcombobox. I think i have followed the other examples on the net to getSelectedItem, but nothing is happening.
FYI, my project is a unit converter (using MVC..hopefully, but that is not my priority).
Any assistance is greatly appreciated.
Thanks, Simon.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class UnitConverterView extends JFrame{
//variables and components
private static final long serialVersionUID = -4673040337179571462L;
private JComboBox<String> unitCategory;
private JTextField fromValue = new JTextField(7);
private JComboBox<String> convertFrom;
private JLabel equalsLabel = new JLabel(" = ");
private JTextField toValue = new JTextField(7);
private JComboBox<String> convertTo;
//constructor
UnitConverterView(){
//set up the view and components
JPanel unitPanel = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,300);
String[] categories = {"Length","Weight","Speed","Temperature"};
unitCategory = new JComboBox<>(categories);
String[] tofromValues = {" "};
convertFrom = new JComboBox<>(tofromValues);
convertTo = new JComboBox<>(tofromValues);
unitPanel.add(unitCategory);
unitPanel.add(fromValue);
unitPanel.add(convertFrom);
unitPanel.add(equalsLabel);
unitPanel.add(toValue);
unitPanel.add(convertTo);
this.add(unitPanel);
}
//get value to convert from
public int getMeasurement() {
return Integer.parseInt(fromValue.getText());
}
//listen for unitCategory to be selected
void addUnitCategoryListener(ActionListener listenForUnitCategory) {
unitCategory.addActionListener(listenForUnitCategory);
}
class UnitCatListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
/*String unitSelected = (String) unitCategory.getSelectedItem();
if (e.getSource() == unitCategory) {
String unitName = (String) unitCategory.getSelectedItem();
System.out.println("UnitName = " + unitName);
changeText(unitName);
}*/
JComboBox cb = (JComboBox)e.getSource();
String unitName = (String) cb.getSelectedItem();
System.out.println("UnitName = " + unitName);
}
void changeText(String name) {
toValue.setText(name);
}
}
}
You have declared a method addUnitCategoryListener() for registering listener to the combobox, but you never called this method. That's why the listener is never registered.
Add the below line at the end of your constructor, then you should be fine:
addUnitCategoryListener(new UnitCatListener());
To simply solve your problem, call the method you created to register the listener on the component. Add this to your constructor:
addUnitCategoryListener(new UnitCatListener());
However, there are a few things you'll want to know:
An ItemListener will usually do a better job than an ActionListener for a JComboBox. The previous one does not fire events if the user selects the already selected item (basically, does nothing). Usually there is nothing you need to do in these cases.
You don't need an extra method just to register the listener, you can directly add to your constructor the line
unitCategory.addActionListener(new UnitCatListener());
and remove your custom method.
The methods changeText and getMeasurement are never used.
Use parametrized types: instead of JComboBox use JComboBox<String>.
You don't need the equalsLabel as a field - a local variable will do - since you do not need to reference it anywhere later (unless you plan on changing a property of the label at runtime).
I am able to expose a private JTextField by doing this:
public void setTextField(String value) {
someTF.setText(value);
}
It would be a lot of work if I have a lot of JTextFields. I tried doing this but failed. No error it's just not setting the right value on specified JTextField.
public class SomeView {
private JTextField someTF = new JTextField(10);
...
public void initComponents() {
...
}
public void setTextField(JTextField jTF, String value) {
jTF.setText(value);
}
}
public class SomeViewTable implements ...{
public void mousePressed(MouseEvent e) {
if (e.getSource() == someButton) {
JTextField someTF = new JTextField(10);
String value = "Some Value";
SomeView sv = new SomeView();
sv.initComponents();
sv.setTextField(someTF, value);
}
}
}
Im expecting this to happen in SomeView class when I called method sameTextField
someTF.setText("Some Value");
Is this possible, what rules in java am I breaking here?
In your listener, you are creating a local variable:
JTextField someTF = new JTextField(10);
...
sv.setTextField(someTF, value);
But what you want is to set the text field of SomeView. So remove the first line, and replace the second with:
sv.setTextField(sv.someTF, value);
Now, to answer the more global question of how to expose many private JTextFields through one method, one possibility could be to assign a string ID to each of them, and store them all in a HashMap:
Map<String,JTextField> map = new HashMap<String,JTextField>();
map.put("field 1", textField1);
...
map.put("field n", textFieldn);
public void setTextField(String id, String value) {
map.get(id).setText(value);
}
Or you could simply generate getters automatically for all your fields (most IDE do that painlessly)...
I have an error for the code below. Sorry if this is too basic as I am new to java.
Basically, I cannot retrieve the String "44418" from the class CityChange.
I know the reason is because I created a new instance cc in the class MainPanel.
However I do not know any other way of doing it.
public class CityChange extends JPanel {
public CityChange() {
JButton btn1 = new JButton("London")
this.add(btn1);
btn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
//London Yahoo Weather Code 44418
setCitySelected("44418");
}
});
}
public void setCitySelected(String citySelected) {
this.citySelected = citySelected;
}
public String getCitySelected() {
return citySelected;
}
private String citySelected;
}
public class MainPanel extends JPanel {
public MainPanel() {
CityChange cc = new CityChange();
System.out.println(cc.getCitySelected()); //returns null instead of 44418
}
}
Please give some advice. Thank you.
For timing reasons, the value has no choice but to be null.
What happens here "immediately" (at init time) is that a new CityChange object is created and its citySelected is gotten and printed. As nobody set it otherwise, it is null.
Only after firing the event (clicking the button), it gets a value, and if you print the value then, you'll see the new value.
The code setCitySelected("44418"); is only executed when you call the method public void actionPerformed(ActionEvent evt) which is not happening at the moment. This Method is only called via a Button in a GUI so you first need at least a simple Window with a Button. Here is a good example http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
Hi I'm making an app but, I found a problem. I'm using netbeans gui builder to build my gui.
So, the first gui class has a lot of button(every button does the same function) that have an actionlistener that looks like this:
public class Guipanel extends JPanel {
private void jbtTTActionPerformed(java.awt.event.ActionEvent evt) {
if(mb.getlevel() > 16){
if(ttp != 20 && mb.getpoints() != 0){
point();
ttp++;
jbtTT.setText(""+ttp);
}
}
}
private void point(){
mb.reducepoints();
}
int ttp;
Base mb = new Base();
JButton jbtTT = new JButton();
}
The Base Class has a lot of method but the one that is related to this problem looks like this:
public class Base extends JFrame {
//point decrement method
public void reducepoints(){
points--;
jlbPoints.setText("Points Available: "+points);
}
//return point value
public int getpoints(){
return this.points;
}
//return level value
public int getlevel(){
return this.level;
}
private static int level = 1;
private static int points = 20;
private JLabel jlbPoints = new JLabel("Points Available: "+points);
}
So the problem is this, when I pressed the jbtTT the points variable will decrement so the value will change from 20 to 19. I used System.out.println to verify that. As you can see, the reducepoints() method should update the jlbPoints text which it doesnt. I have tried making the Base mb = new Base() to static Base mb = new Base() but it still doesn't work. Am I doing this wrong? Anyone have a suggestion?
EDIT: I've tried to do System.out.println(jlbPoints.getText()); and the text did changed. The only one that didn't change is the text that the user can see. Even repaint() and revalidate didn't work.
EDIT2: I finally found the answer to this question. I have to pass the 'Base' class object to the 'Guipanel' class since I created the 'Base' class object in a main class(I don't want to use this class as a main class). I passed it with the following method:
public void passObj(Base mb){
this.mb = mb;
}
and changing the constructor of 'Guipanel' class like this:
public Guipanel(Base mb) {
initComponents();
this.mb = mb;
}
also changing this Base mb = new Base(); to Base mb;
I wanted to thank everyone that have tried to answer this question.
Try using SwingUtilities.invokeLater() to ensure you are changing the text on the event dispatch thread, as you are supposed to. All we can do is guess given the current information.