Results 1 to 4 of 4
  1. #1
    marceldupont is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default refresh a different window

    Hello,
    I need your advice on the GUI i'm writing. Basically i want to open different frames, one of which displays data. The other ones are toolboxes.
    Right now I have one overhead class that opens or closes the frames. Each frame has it's own class and displays different buttons... Data is transited through public static variables of the main class. However I am experiencing difficulty in activating a frame from another one. For example, when I click in one frame, it should do something and display the result in the other frame, but I don't know how to order the other frame to react.
    Should I change the whole structure of my code?
    Thanks for your help...
    Marcel

  2. #2
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Well, I can't tell you whether you should change the structure of your code (because I haven't seen it), but I can tell you it would be a good idea to use one JFrame, and use JDialog instances for the toolboxes. After that, you should implement ActionListener and add the method actionPerformed(ActionEvent e) so you can catch actions with something like a JButton and display it in another JDialog (I used a frame in my example, but it's basically the same), that has something like a JLabel or JTextArea. By the way, all the JButtons, JDialogs, JLabels, etc. that you use for this (should you decide to do this) should be fields/global variables so that you can access them from any point in your code. To make things easier, I'll post an example.

    Edit: I made mistakes in my code, after testing it and making sure it works, I posted the working code below.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ExampleGUI implements ActionListener {
    
    private JFrame frame;
    private JDialog toolboxOne;
    private JButton buttonOne, buttonTwo, buttonThree;
    private JLabel textLabel;
    private JTextField field;
    
    public ExampleGUI() {
    frame = new JFrame("ExampleGUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    toolboxOne = new JDialog(frame, "Tool Box");
    Dimension b = new Dimension(150, 40);
    buttonOne = new JButton("Button one");
    buttonOne.setActionCommand("one");
    buttonOne.addActionListener(this);
    buttonOne.setPreferredSize(b);
    buttonOne.setMinimumSize(b);
    buttonOne.setMaximumSize(b);
    buttonTwo = new JButton("Button two");
    buttonTwo.setActionCommand("two");
    buttonTwo.addActionListener(this);
    buttonTwo.setPreferredSize(b);
    buttonTwo.setMinimumSize(b);
    buttonTwo.setMaximumSize(b);
    buttonThree = new JButton("Button three");
    buttonThree.setActionCommand("three");
    buttonThree.addActionListener(this);
    buttonThree.setPreferredSize(b);
    buttonThree.setMinimumSize(b);
    buttonThree.setMaximumSize(b);
    textLabel = new JLabel("Button clicked: ");
    field = new JTextField(10);
    field.setText("none");
    field.setEditable(false);
    }
    
    public void actionPerformed(ActionEvent e) {
    if ("one".equals(e.getActionCommand()))
    field.setText("Button one");
    else if ("two".equals(e.getActionCommand()))
    field.setText("Button two");
    else if ("three".equals(e.getActionCommand()))
    field.setText("Button three");
    }
    
    public void createAndShowGUI() {
    JPanel labelPanel = new JPanel();
    labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.X_AXIS));
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
    labelPanel.add(textLabel);
    labelPanel.add(field);
    frame.getContentPane().add(labelPanel);
    frame.pack();
    frame.setVisible(true);
    buttonPanel.add(buttonOne);
    buttonPanel.add(buttonTwo);
    buttonPanel.add(buttonThree);
    toolboxOne.getContentPane().add(buttonPanel);
    toolboxOne.pack();
    toolboxOne.setVisible(true);
    }
    
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new ExampleGUI().createAndShowGUI();
    }
    });
    }
    
    }
    I hope this helps give you an idea.
    Last edited by Bluefox815; 03-22-2008 at 05:26 AM. Reason: Mistakes in code

  3. #3
    marceldupont is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default

    Thanks!
    This looks a lot easier...
    I'm not used to program something as involved as Java (used to use Python)...
    I'll try this out and see,
    Cheers,
    Marcel

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.*;
    
    public class Communication implements ActionListener {
        JDialog topDialog;
        JDialog bottomDialog;
        Random seed = new Random();
        JLabel label;
        String[] strs = {
            "another string", "controled from", "top dialog"
        };
        int strIndex = 0;
    
        public Communication(JFrame f) {
            ActionListener al = launchTopDialog(f);
            launchBottomDialog(f, al);
        }
    
        public void actionPerformed(ActionEvent e) {
            String ac = e.getActionCommand();
            if(ac.equals("TEXT")) {
                label.setText(strs[strIndex]);
                strIndex = (strIndex+1) % strs.length;
            }
            if(ac.equals("TOGGLE")) {
                toggleButtons();
            }
        }
    
        private ActionListener launchTopDialog(JFrame f) {
            final JPanel panel = new JPanel();
            ActionListener al = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    panel.setBackground(getColor());
                    panel.repaint();
                }
            };
            JButton button = new JButton("change bottom text");
            button.setActionCommand("TEXT");
            button.addActionListener(this);
            JPanel south = new JPanel();
            south.add(button);
            topDialog = new JDialog(f, "top dialog", false);
            topDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            topDialog.add(panel);
            topDialog.add(south, "Last");
            topDialog.setSize(360,240);
            topDialog.setLocation(650,200);
            topDialog.setVisible(true);
            return al;
        }
    
        private void launchBottomDialog(JFrame f, ActionListener al) {
            label = new JLabel();
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setPreferredSize(new Dimension(100,30));
            JButton button = new JButton("change top color");
            button.addActionListener(al);
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(button, new GridBagConstraints());
            bottomDialog = new JDialog(f, "bottom dialog", false);
            bottomDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            bottomDialog.add(label, "First");
            bottomDialog.add(panel);
            bottomDialog.setSize(360,240);
            bottomDialog.setLocation(650,465);
            bottomDialog.setVisible(true);
        }
    
        private void toggleButtons() {
            JButton button = getButton(topDialog);
            boolean enabled = button.isEnabled();
            button.setEnabled(!enabled);
            getButton(bottomDialog).setEnabled(!enabled);
        }
    
        private JButton getButton(Container parent) {
            Component[] c = parent.getComponents();
            for(int i = 0; i < c.length; i++) {
                if(c[i] instanceof JButton)
                    return (JButton)c[i];
                if(((Container)c[i]).getComponentCount() > 0)
                    return getButton((Container)c[i]);
            }
            return null;
        }
    
        private Color getColor() {
            return new Color(seed.nextInt(0xffffff));
        }
    
        private JPanel getContent() {
            JButton button = new JButton("toggle enabled");
            button.setActionCommand("TOGGLE");
            button.addActionListener(this);
            JPanel south = new JPanel();
            south.add(button);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(south, "Last");
            return panel;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame("frame");
            Communication test = new Communication(f);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getContent());
            f.setSize(400,150);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

Similar Threads

  1. refresh JPanel
    By olesja in forum AWT / Swing
    Replies: 1
    Last Post: 04-16-2008, 03:58 PM
  2. My Simple Refresh Problem
    By pmcastillo in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 07:59 AM
  3. How to Refresh the same page by JSP
    By AbuAziz in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-10-2007, 04:06 AM
  4. Refresh session
    By cachi in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2007, 07:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •