B
Bob Rivers
Hi,
I'm having a weird problem. I don't know if I am doing a basic
mistake, or it's a problem.
Basically, I want to share a variable value between to methods. I have
tried many differents ways to do it. The last one, that I posted
bellow, I am trying to do it by using a set/get methods.
My problem is that I usually do programs using servlets. I never user
awt/swing, so maybe I am doing something wrong.
I receive a value through the class constructor. This value will be
used on another method. I am using some System.out in order to check
if the value is properly received and passed.
The constructor and set methods are ok. They show the value. But the
get and paint prints nothing.
I am posting two classes. The first one that has the problem. The
second one is just for peiple that wants to compile and see the
problem.
TIA,
Bob
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
public class mjlDisplayPanel extends JPanel {
public String lines = null;
public mjlDisplayPanel (String srcCode) {
System.out.println("constructor -> " + srcCode);
setLines(srcCode);
}
public void paintComponent(Graphics g) {
String sourceCode = getLines();
String[] result =
sourceCode.split(DefaultEditorKit.EndOfLineStringProperty);
for (int x = 0; x < result.length; x++)
System.out.println("paint -> " + result[x]);
super.paintComponent(g);
g.drawLine(0, 0, 9, 9);
}
public void setLines(String args){
System.out.println("set -> " + args);
lines = args;
}
public String getLines(){
System.out.println("get -> " + lines);
return lines;
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class myJLogo implements ActionListener {
JPanel mainPanel;
JScrollPane sourcePanel;
mjlDisplayPanel displayPanel;
JTextField textField = null;
JTextArea textArea = null;
public myJLogo() {
textArea = new JTextArea(20, 40);
textArea.setEditable(true);
sourcePanel = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
displayPanel = new mjlDisplayPanel(textArea.getText());
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0,2,5,5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
JButton button = new JButton("Desenhar");
button.setMnemonic(KeyEvent.VK_I);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayPanel = new mjlDisplayPanel(textArea.getText());
}
});
mainPanel.add(sourcePanel);
mainPanel.add(displayPanel);
mainPanel.add(button);
}
public void actionPerformed(ActionEvent event) {
// tbd
}
public static void main(String[] args) {
myJLogo mjl = new myJLogo();
JFrame mjlFrame = new JFrame("myJLogo");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {}
mjlFrame.setContentPane(mjl.mainPanel);
mjlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mjlFrame.pack();
mjlFrame.setVisible(true);
}
}
I'm having a weird problem. I don't know if I am doing a basic
mistake, or it's a problem.
Basically, I want to share a variable value between to methods. I have
tried many differents ways to do it. The last one, that I posted
bellow, I am trying to do it by using a set/get methods.
My problem is that I usually do programs using servlets. I never user
awt/swing, so maybe I am doing something wrong.
I receive a value through the class constructor. This value will be
used on another method. I am using some System.out in order to check
if the value is properly received and passed.
The constructor and set methods are ok. They show the value. But the
get and paint prints nothing.
I am posting two classes. The first one that has the problem. The
second one is just for peiple that wants to compile and see the
problem.
TIA,
Bob
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
public class mjlDisplayPanel extends JPanel {
public String lines = null;
public mjlDisplayPanel (String srcCode) {
System.out.println("constructor -> " + srcCode);
setLines(srcCode);
}
public void paintComponent(Graphics g) {
String sourceCode = getLines();
String[] result =
sourceCode.split(DefaultEditorKit.EndOfLineStringProperty);
for (int x = 0; x < result.length; x++)
System.out.println("paint -> " + result[x]);
super.paintComponent(g);
g.drawLine(0, 0, 9, 9);
}
public void setLines(String args){
System.out.println("set -> " + args);
lines = args;
}
public String getLines(){
System.out.println("get -> " + lines);
return lines;
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class myJLogo implements ActionListener {
JPanel mainPanel;
JScrollPane sourcePanel;
mjlDisplayPanel displayPanel;
JTextField textField = null;
JTextArea textArea = null;
public myJLogo() {
textArea = new JTextArea(20, 40);
textArea.setEditable(true);
sourcePanel = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
displayPanel = new mjlDisplayPanel(textArea.getText());
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0,2,5,5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
JButton button = new JButton("Desenhar");
button.setMnemonic(KeyEvent.VK_I);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayPanel = new mjlDisplayPanel(textArea.getText());
}
});
mainPanel.add(sourcePanel);
mainPanel.add(displayPanel);
mainPanel.add(button);
}
public void actionPerformed(ActionEvent event) {
// tbd
}
public static void main(String[] args) {
myJLogo mjl = new myJLogo();
JFrame mjlFrame = new JFrame("myJLogo");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {}
mjlFrame.setContentPane(mjl.mainPanel);
mjlFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mjlFrame.pack();
mjlFrame.setVisible(true);
}
}