0

Why would the order of Tabs in a JTabbedPane affect if the content of the tab works as expected?

I am writing my first advanced application and am having some trouble with my JTabbedPane. Here's what I have:

public ProjectTracker() {
    initialize();
    newJobTab();
    newUpdateTab();
    newReportsTab();
}

newJobTab(), newUpdateTab() and newReportsTab() are placed into a JTabbed pane within the initialize() method. Each create an instance of a GUI class that I made. It basically has a bunch of Text fields, and comboboxes, etc. and they interact with a database to either populate the fields or collect info from the fields.

The functionality of the buttons on the tab are the main difference between the three. Individually, each tab works as I would expect it to. When I place them in the Tabbed pane, only the third tab works properly. If I switch around the order, it is the same deal. Whichever one is the third tab is the only one that functions the way I want.

Here is my revision to my original post...now with code.

public class SampleTracker {

private JFrame frmProjectTracker;
private JTabbedPane tabbedPane;
public String Title;

SampleTJV newJobPanel;
SampleTJV updatePanel;

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

/**
 * Create the application.
 */
public SampleTracker() {
    initialize();
    newJobTab();
    newUpdateTab();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmProjectTracker = new JFrame();
    frmProjectTracker.setBounds(100, 100, 662, 461);
    frmProjectTracker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmProjectTracker.getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
            ColumnSpec.decode("662px"),},
        new RowSpec[] {
            RowSpec.decode("50px"),
            RowSpec.decode("389px"),}));

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    frmProjectTracker.getContentPane().add(tabbedPane, "1, 2, fill, fill");
}


private void newJobTab(){
    newJobPanel = new SampleTJV();
    newJobPanel.UpdateButton.setText("Enter Job");
    tabbedPane.addTab("Enter New Job", null, newJobPanel, null);
    newJobPanel.UpdateButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
                newJobPanel.collectInfo();
                Title = newJobPanel.Title;
                //Here the connection to DB is made and the Title is written to DB
                newJobPanel.newJobField.setText(Title);
            }
    }); 
}

private void newUpdateTab(){
    updatePanel = new SampleTJV();
    newJobPanel.UpdateButton.setText("Update Job");
    tabbedPane.addTab("Update Job", null, updatePanel, null);
    updatePanel.UpdateButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            updatePanel.collectInfo();
            Title = updatePanel.Title;
            updatePanel.updateJobField.setText(Title);
        }
    }); 
}

}

public class SampleTJV extends JPanel {

private static final long serialVersionUID = 1L;
public static JTextField TitleField;

public String Title;
public JButton UpdateButton;
public JTextField newJobField;
public JTextField updateJobField;
/**
 * Create the panel.
 */
public SampleTJV() {
    setLayout(null);
    TitleField = new JTextField();

    TitleField.setColumns(10);
    TitleField.setBounds(109, 6, 134, 28);
    add(TitleField);

    newJobField = new JTextField();
    newJobField.setBounds(171, 79, 134, 28);
    add(newJobField);
    newJobField.setColumns(10);

    UpdateButton = new JButton("Update Job");
    UpdateButton.setBounds(267, 7, 112, 29);
    add(UpdateButton);

    JLabel lblNewJobResult = new JLabel("New Job Result");
    lblNewJobResult.setBounds(47, 85, 112, 16);
    add(lblNewJobResult);

    JLabel lblUpdateJobResult = new JLabel("Update Job Result");
    lblUpdateJobResult.setBounds(47, 125, 112, 16);
    add(lblUpdateJobResult);

    updateJobField = new JTextField();
    updateJobField.setColumns(10);
    updateJobField.setBounds(171, 119, 134, 28);
    add(updateJobField);

}

public void collectInfo(){  
    Title = TitleField.getText();
}

}

phischer
  • 1
  • 1
  • 2
    You'll have to post code. My guess is that you have static variables and methods which should be instance variables and methods. Also make sure to define "doesn't work". What happens precisely? Any exception? what? – JB Nizet May 12 '13 at 07:33
  • how are you adding these tabs in JTabbedPane? some code...? – vishal_aim May 12 '13 at 07:34
  • 1
    1) *"I'm not sure how I could post more code without posting a ton of code."* For better help sooner, post an **[SSCCE](http://sscce.org/).** 2) Always copy/paste error & exception output. – Andrew Thompson May 12 '13 at 07:35
  • Here's a short [example](http://stackoverflow.com/a/11949899/230513) for reference. – trashgod May 12 '13 at 12:28
  • 1
    "I'm not sure how I could post more code without posting a ton of code." You can do it by cloning your program to a small(er) example that creates and adds the tabbed panes the same way you do now. There's a good chance you will discover the problem yourself when you do that; if not, you will have something small enough to post, and the code will be reduced to just the question you need to ask. If you'll read over what you have posted so far from the standpoint of someone who has no context, you will likely see we have no chance of identifying the problem(s). – arcy May 12 '13 at 12:36
  • I posted some code that essentially has the same issue that I am talking about. This code opens a user interface where you enter a string into the top text field and hit the button. When on the first tab, the string should copy into the second text field. When on the second tab, the string should copy into the 3rd text field. The first tab does not work, but the second tab does. I can switch the order of which tab is added to the TabbedPane and the same thing happens. The first tab does not work. – phischer May 12 '13 at 20:16
  • also, if I have anything in the first text field of the second tab and hit the enter button on the first tab, the text is copied from the first field of the second tab. I'm sure it is scope of the variables mistake somewhere, but I am at a loss right now. Thanks in advance for your help. – phischer May 12 '13 at 20:19

2 Answers2

1

The following is a copy error:

private void newUpdateTab(){
    updatePanel = new SampleTJV();
    newJobPanel.UpdateButton.setText("Update Job");

newJobPanel there is probably not intended.


Also wrong is a static GUI field:

static JTextField TitleField;
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

The problem was exactly what JB Nizet guessed earlier. It was static methods and variables which should have been instance variables. In my sample code, SampleTJV, if you remove the word static from public static JTextField TitleField; the program works exactly as intended.

phischer
  • 1
  • 1