2

I just want to go from one frame to the other. I made a list where you could choose from and then when you would click the button confirm it should show a different window. Sadly it doens't.

This is the class from my first Jframe, so my first window with that confirm button:

package view;

import java.awt.Button;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.factories.DefaultComponentFactory;

public class Scherm1pursco extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Scherm1pursco frame = new Scherm1pursco();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Scherm1pursco() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblSelectCategory = DefaultComponentFactory.getInstance()
            .createTitle("Select Category");
    lblSelectCategory.setFont(new Font("Tahoma", Font.BOLD, 16));
    lblSelectCategory.setBounds(141, 49, 149, 16);
    contentPane.add(lblSelectCategory);

    ArrayList<String> purposeCategories = new ArrayList<String>();
    purposeCategories.add("Behavioral");
    purposeCategories.add("Structural");
    purposeCategories.add("Creational");
    JList list_purpose = new JList(purposeCategories.toArray());
    list_purpose
            .setToolTipText("Choose at least one categorie of purpose.");
    list_purpose.setBounds(50, 121, 121, 63);
    contentPane.add(list_purpose);

    ArrayList<String> scopeCategories = new ArrayList<String>();
    scopeCategories.add("Class");
    scopeCategories.add("Object");
    JList list_Scope = new JList(scopeCategories.toArray());
    list_Scope.setToolTipText("Choose at least one categorie of Scope");
    list_Scope.setBounds(244, 121, 121, 63);
    contentPane.add(list_Scope);

    JLabel lblPurpose = new JLabel("Purpose categories:");
    lblPurpose.setBounds(50, 92, 121, 16);
    contentPane.add(lblPurpose);

    JLabel lblScopCategories = new JLabel("Scope categories:");
    lblScopCategories.setBounds(244, 92, 121, 16);
    contentPane.add(lblScopCategories);

    Button button = new Button("Confirm>>");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (list_Scope.getSelectedValue().equals("Class")
                    && (list_purpose.getSelectedValue()
                            .equals("Structural"))) {
                Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
                S2FP.setVisible(true);;
            } else {
                System.out.println("error");
            }
            if (list_Scope.getSelectedValue().equals("Class")
                    && (list_purpose.getSelectedValue()
                            .equals("Behavioral"))) {
                Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
                S2FP.setVisible(true);
            } else {
                System.out.println("error");
            }
            if (list_Scope.getSelectedValue().equals("Class")
                    && (list_purpose.getSelectedValue()
                            .equals("Creational"))) {
                Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
                S2FP.setVisible(true);
            } else {
                System.out.println("error");
            }
            if (list_Scope.getSelectedValue().equals("Object")
                    && (list_purpose.getSelectedValue()
                            .equals("Structural"))) {
                Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
                S2FP.setVisible(true);
            } else {
                System.out.println("error");
            }
            if (list_Scope.getSelectedValue().equals("Object")
                    && (list_purpose.getSelectedValue()
                            .equals("Creational"))) {
                Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
                S2FP.setVisible(true);
            } else {
                System.out.println("error");
            }
            if (list_Scope.getSelectedValue().equals("Class")
                    && (list_purpose.getSelectedValue()
                            .equals("Behavioral"))) {
                Scherm2FilterdPatterns S2FP = new Scherm2FilterdPatterns();
                S2FP.setVisible(true);
            } else {
                System.out.println("error");
            }
            if (list_Scope.getSelectedValue().equals(null)
                    && (list_purpose.getSelectedValue()
                            .equals(null))) {
                System.out.println("error");
            }
        }
    });
    button.setBounds(169, 203, 79, 24);
    contentPane.add(button);
}
    }

This is my second Jframe, this the class where it should be referred to:

     package view;

     import java.awt.Dimension;

     import javax.swing.BoxLayout;
     import javax.swing.JFrame;
     import javax.swing.JLabel;
    import javax.swing.JPanel;  
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
     import javax.swing.ListSelectionModel;
    import javax.swing.RowFilter;
    import javax.swing.SpringLayout;
    import javax.swing.SwingConstants;
    import javax.swing.event.DocumentEvent;
   import javax.swing.event.DocumentListener;
   import javax.swing.event.ListSelectionEvent; 
   import javax.swing.event.ListSelectionListener;
    import javax.swing.table.AbstractTableModel;
   import javax.swing.table.TableRowSorter;

    public class Scherm2FilterdPatterns extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTable table;
private boolean DEBUG = false;

private JTextField filterText;
private JTextField statusText;
private TableRowSorter<MyTableModel> sorter;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

/**
 * Create the frame.
 */
public Scherm2FilterdPatterns() {

    super();
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    // Create a table with a sorter.
    MyTableModel model = new MyTableModel();
    sorter = new TableRowSorter<MyTableModel>(model);
    table = new JTable(model);
    table.setRowSorter(sorter);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    // For the purposes of this example, better to have a single
    // selection.
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    // When selection changes, provide user with row numbers for
    // both view and model.
    table.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent event) {
                    int viewRow = table.getSelectedRow();
                    if (viewRow == 0) {
                        // Selection got filtered away.
                        statusText.setText("Diagram is");
                    } else {
                        System.out.print("THIS IS THE SOLUTION");
                        if (viewRow == 1) {
                            statusText.setText("diagram2 is");
                        } else {
                            int modelRow = table
                                    .convertRowIndexToModel(viewRow);
                            statusText.setText(String.format("poep",
                                    viewRow, modelRow));
                            if (viewRow == 2) {
                                statusText.setText("diagram3 is");
                            } else {
                                System.out.println("THIS IS THE SOLUTION");
                            }
                        }
                    }
                }
            });

    // Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    // Add the scroll pane to this panel.
    add(scrollPane);

    // Create a separate form for filterText and statusText
    JPanel form = new JPanel(new SpringLayout());
    JLabel l1 = new JLabel("Filter on problem or consequences:",
            SwingConstants.TRAILING);
    form.add(l1);
    filterText = new JTextField();
    // Whenever filterText changes, invoke newFilter.
    filterText.getDocument().addDocumentListener(new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
            newFilter();
        }

        public void insertUpdate(DocumentEvent e) {
            newFilter();
        }

        public void removeUpdate(DocumentEvent e) {
            newFilter();
        }
    });
    l1.setLabelFor(filterText);
    form.add(filterText);
    JLabel l2 = new JLabel("Select pattern for more info:",
            SwingConstants.TRAILING);
    form.add(l2);
    statusText = new JTextField();
    l2.setLabelFor(statusText);
    form.add(statusText);
    SpringUtilities.makeCompactGrid(form, 2, 2, 6, 6, 6, 6);
    add(form);
}

/**
 * Update the row filter regular expression from the expression in the text
 * box.
 */

// filter on problem and consequences
private void newFilter() {
    RowFilter<MyTableModel, Object> rf = null;
    // If current expression doesn't parse, don't update.
    try {
        rf = RowFilter.regexFilter(filterText.getText(), 1, 2);
    } catch (java.util.regex.PatternSyntaxException e) {
        return;
    }
    sorter.setRowFilter(rf);
}

class MyTableModel extends AbstractTableModel {
    private String[] columnNames = { "Pattern Name:", "Problem",
            "Consequences", };

    private Object[][] data = { { "Interpreter", "Smith", "Snowboarding" },
            { "Template Method", "Doe", "Rowing" },
            { "Chain of Responsibility", "Black", "Knitting", },
            { "Command", "White", "Speed reading", },
            { "Iterator", "Brown", "Pool" },
            { "Mediator", "Brown", "Pool", },
            { "Memento", "Brown", "Pool", },
            { "Observer", "Brown", "Pool", },
            { "State", "Brown", "Pool", },
            { "Strategy", "Brown", "Pool", },
            { "Visitor", "Brown", "Pool", }, };

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    /*
     * JTable uses this method to determine the default renderer/ editor for
     * each cell. If we didn't implement this method, then the last column
     * would contain text ("true"/"false"), rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's editable.
     */
    public boolean isCellEditable(int row, int col) {
        // Note that the data/cell address is constant,
        // no matter where the cell appears onscreen.
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * Don't need to implement this method unless your table's data can
     * change.
     */
    public void setValueAt(Object value, int row, int col) {
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                    + " to " + value + " (an instance of "
                    + value.getClass() + ")");
        }

        data[row][col] = value;
        fireTableCellUpdated(row, col);

        if (DEBUG) {
            System.out.println("New value of data:");
            printDebugData();
        }
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i = 0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j = 0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 */
private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("Filterd Patterns");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    TableFilterDemo newContentPane = new TableFilterDemo();
    newContentPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(newContentPane);

    // Display the window.
    frame.pack();
    frame.setVisible(true);
}

  }

I am really stuck here, please help! Thank you for your time:) Sorry for my English

user3599415
  • 283
  • 3
  • 6
  • 18
  • 1
    Could you shorten your code by removing the irrelevant parts and showing us just the relevant parts? – Spikatrix Dec 27 '14 at 15:18
  • 1
    1. Don't use "Button". With Swing you should be using `JButton`. 2. Don't create a new frame. You should use a `JDialog` for your second window. 3. Basic code looks reasonable so add some debug statement to your code to make the "if statements" that create and display the second window are actually executed. 4. If you need more help then post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. To test your ActionListener all you need is a couple of lines of code to create and display and empty JDialog. All the components are irrelevant to your actual question. – camickr Dec 27 '14 at 16:22
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Dec 28 '14 at 00:04

1 Answers1

1

Your code seems too broad and unnecessary to demonstrate whereas the minimal code needed is very small.

Even then, I'm writing a brief answer to your question. Considering that you have to exit jframe1 and move to jframe2, In the action performed event of confirm button, just add the following lines :-

public static void jButton1 ActionPerformed......{
jFrame1.dispose();
jFrame2.setVisible(true);
}
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73