0
votes

I am new to Swing and cannot find a page that helps me understand JTabbedPane. I cannot find a way to control the layout of components of the tabbed panels. I can layout each of my panels correctly as separate GUIs but not in a tabbed pane like I need to do. I would like to use the BorderLayout not FlowLayout.

Also, you can see I'm trying to use colors to keep track of my panels and their components. I cannot set the background of the JTabbedPane. It is still the default grey. Can someone tell me why this is?

Thank you for any advice you can give.

What I have so far appears to follow a 'flow layout' despite any changes I've tried

(Methods have been removed or nearly removed to keep code shorter)

public class GUIFrame extends JFrame {

public GUIFrame(String title) {
    JFrame frame = new JFrame(title);
    Container c = frame.getContentPane();
    buildGUI(c);
    setFrameAttributes(frame);
}
private void buildGUI(Container c) {
    c.setLayout(new BorderLayout());
    c.setBackground(Color.BLACK);
    JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
    tabs.setBackground(Color.YELLOW);
    c.add("Center", tabs);
    tabs.addTab("Specialty", new SpecialtyPanel());
    tabs.addTab("Treatment", new TreatmentPanel());
    tabs.addTab("Doctor", new DoctorPanel());
    tabs.addTab("Patient", new PatientPanel());
}
private void setFrameAttributes(JFrame f) {
    f.setSize(500, 500);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
    MedicalSystemIO test = new MedicalSystemIO();
    new GUIFrame("Tabbed Title");
}

public class SpecialtyPanel extends JPanel implements ActionListener {

JTextField jteInput = null; 
DefaultListModel<String> model = new DefaultListModel<String>();
JList<String> list = new JList(model);
JScrollPane pane = new JScrollPane(list);

public SpecialtyPanel() {
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.black));
    buildGUI(panel);    
}
private void buildGUI(JPanel panel) {       
    JPanel jpaInput = createInputPanel();
    JPanel jpaProcess = createProcessPanel();
    JPanel jpaOutput = createOutputPanel();
    //panel.setLayout(new BorderLayout());
    add("North", jpaInput);
    add("Center", jpaProcess);
    add("South", jpaOutput);
}
private JPanel createInputPanel() {
    JPanel jpaInput = new JPanel();
    jpaInput.setBackground(Color.RED);
    return jpaInput;
}
private JPanel createProcessPanel() {
    JPanel jpaProcess = new JPanel();
    jpaProcess.setBackground(Color.BLUE);
    return jpaProcess;
}
private JPanel createOutputPanel() {
    JPanel jpaOutput = new JPanel();
    jpaOutput.add(pane);
    return jpaOutput;
}
1

1 Answers

0
votes

The SpecialtyPanel is shown that way (flow layout) as you are putting the components on it in the wrong way:

  1. No need for passing a new panel into the buildGUI method as you want to put them directly on the SpecialtyPanel which already is a JPanel,
  2. you commented out the setting of the BorderLayout and
  3. you used the wrong notation of passing the layout constraints in the add methods.

Your constructor and build method should look like this:

public SpecialtyPanel() {
    buildGUI();
}

private void buildGUI() {
    setBorder(BorderFactory.createLineBorder(Color.black));
    JPanel jpaInput = createInputPanel();
    JPanel jpaProcess = createProcessPanel();
    JPanel jpaOutput = createOutputPanel();
    setLayout(new BorderLayout());
    add(jpaInput, BorderLayout.NORTH);
    add(jpaProcess, BorderLayout.CENTER);
    add(jpaOutput, BorderLayout.SOUTH);
}

To have the panel another color than gray you have to color the component that is put on the tabbed pane as it covers the whole space. Add the desired color to the buildGUI method, e.g.:

private void buildGUI(JPanel panel) {
    // ...
    setBackground(Color.YELLOW);
}

As a JPanel is opaque by default (that means not transparent), you need to set panels on top (except those which you colored explicitly) to be transparent. In case of SpecialtyPanel:

private JPanel createOutputPanel() {
    JPanel jpaOutput = new JPanel();
    jpaOutput.add(pane);
    jpaOutput.setOpaque(false); // panel transparent
    return jpaOutput;
}