HELP ME !

Dear people !

I faced with a "strange" problem !

Here is a code :

public class Test implements ActionListener{

public JTextPane picture;

public JPanel leftHalf;

JSplitPane splitPane;

/** Creates a new instance of Test */

public Test() {

leftHalf = new JPanel();

JScrollPane listScrollPane = new JScrollPane(leftHalf);

leftHalf.add(createButton());

picture = new JTextPane();

JScrollPane pictureScrollPane = new JScrollPane(picture);

picture.setText("This is a TEXT");

splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,listScrollPane, pictureScrollPane);

splitPane.setPreferredSize(new Dimension(600, 450));

splitPane.setDividerLocation(300);

Dimension minimumSize = new Dimension(100, 50);

listScrollPane.setMinimumSize(minimumSize);

pictureScrollPane.setMinimumSize(minimumSize);

}

public JSplitPane getSplitPane() {

return splitPane;

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

final Test myTest = new Test();

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

myTest.createAndShowGUI();

}

});

}

public void createAndShowGUI() {

//Create and set up the window.

JFrame frame = new JFrame("TITLE");

//Create Menu

JMenuBar menuBar = new JMenuBar();

frame.setJMenuBar(menuBar);

JMenu menu = new JMenu("Optiuni");

menu.setMnemonic(KeyEvent.VK_O);

menuBar.add(menu);

//Create Submenu

JMenuItem menuItem;

menuItem = menu.add("Calculation");

menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.ALT_MASK));

menuItem.setMnemonic(KeyEvent.VK_C);

menuItem.setActionCommand("calculatie");

menuItem.setActionCommand("compute");

menuItem.addActionListener(this);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Test Test = new Test();

frame.add(Test.getSplitPane());

//Display the window.

frame.pack();

frame.setVisible(true);

}

//setting the action

public void actionPerformed(ActionEvent e) {

if("compute".equals(e.getActionCommand()))

updatePane();

if("write".equals(e.getActionCommand()))

updatePane();

}

//action to perform

private void updatePane() {

String text = picture.getText();

picture.setText("! This is another TEXT !");

System.out.println(text);

}

//create a button

private Component createButton() {

JPanel panel = new JPanel();

JButton button = new JButton("COMPUTE");

button.addActionListener(this);

button.setActionCommand("write");

panel.add(button);

return panel;

}

}

The problem is:

- actioning the left pane button, the rightpane is updated !

- actioning the "menu" the right panel isn't updated !

Please HELP ME to have the same action with both methods mentioned above !

[3111 byte] By [VLADCOa] at [2007-11-26 18:22:21]
# 1
Repost your code between [code]...[/code] tags, along with a better description of the problem.
CaptainMorgan08a at 2007-7-9 5:56:17 > top of Java-index,Java Essentials,New To Java...
# 2

public class Test implements ActionListener{

public JTextPane picture;

public JPanel leftHalf;

JSplitPane splitPane;

/** Creates a new instance of Test */

public Test() {

leftHalf = new JPanel();

JScrollPane listScrollPane = new JScrollPane(leftHalf);

leftHalf.add(createButton());

picture = new JTextPane();

JScrollPane pictureScrollPane = new JScrollPane(picture);

picture.setText("This is a TEXT");

splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,listScrollPane, pictureScrollPane);

splitPane.setPreferredSize(new Dimension(600, 450));

splitPane.setDividerLocation(300);

Dimension minimumSize = new Dimension(100, 50);

listScrollPane.setMinimumSize(minimumSize);

pictureScrollPane.setMinimumSize(minimumSize);

}

public JSplitPane getSplitPane() {

return splitPane;

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

final Test myTest = new Test();

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

myTest.createAndShowGUI();

}

});

}

public void createAndShowGUI() {

//Create and set up the window.

JFrame frame = new JFrame("TITLE");

//Create Menu

JMenuBar menuBar = new JMenuBar();

frame.setJMenuBar(menuBar);

JMenu menu = new JMenu("Optiuni");

menu.setMnemonic(KeyEvent.VK_O);

menuBar.add(menu);

//Create Submenu

JMenuItem menuItem;

menuItem = menu.add("Calculation");

menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.ALT_MASK));

menuItem.setMnemonic(KeyEvent.VK_C);

menuItem.setActionCommand("calculatie");

menuItem.setActionCommand("compute");

menuItem.addActionListener(this);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Test Test = new Test();

frame.add(Test.getSplitPane());

//Display the window.

frame.pack();

frame.setVisible(true);

}

//setting the action

public void actionPerformed(ActionEvent e) {

if("compute".equals(e.getActionCommand()))

updatePane();

if("write".equals(e.getActionCommand()))

updatePane();

}

//action to perform

private void updatePane() {

String text = picture.getText();

picture.setText("! This is another TEXT !");

System.out.println(text);

}

//create a button

private Component createButton() {

JPanel panel = new JPanel();

JButton button = new JButton("COMPUTE");

button.addActionListener(this);

button.setActionCommand("write");

panel.add(button);

return panel;

}

}

with this program I am trying to change the right panel content , actioning in two different ways: first way with a menu (which I prefer), another way with a leftpanel button !

First method doesn't work ! Why ? Second method work (very nice, but in need the first method) !

Please help me to change the right panel using the menu !

VLADCOa at 2007-7-9 5:56:17 > top of Java-index,Java Essentials,New To Java...
# 3
menuItem.setActionCommand("calculatie");menuItem.setActionCommand("compute");Why are you setting the ActionCommand twice?Test Test = new Test();What is the point of this?
CaptainMorgan08a at 2007-7-9 5:56:17 > top of Java-index,Java Essentials,New To Java...
# 4
Because in the next sentence frame.add(Test.getSplitPane());which is "the frame",the method fetSPlitPane cannot be referenced from a static context (main method)!
VLADCOa at 2007-7-9 5:56:17 > top of Java-index,Java Essentials,New To Java...
# 5
> the method fetSPlitPane cannot be referenced from a> static context (main method) !Then you did it wrong. Did you call the method on a Test object? I seriously recommend you take that bit of code out.
CaptainMorgan08a at 2007-7-9 5:56:17 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks !It works !
VLADCOa at 2007-7-9 5:56:17 > top of Java-index,Java Essentials,New To Java...