JTabbedPane - How can I set properties when a new tab is selected?










0















I am new to Java and Swing if anyone could point me in a direction it'd be appreciated.



This is one class building my frame. I believe I have to use the ChangeListener you see added but have done nothing but copy a method from this site.
All I want to accomplish with my tabs is set them back to their default state if another tab is clicked.. simply meaning set the the text fields and list selection back to a state etc.



Will a method I need to make for this be in my classes for the panels or the frame? Will I need to change the scope of anything? I've been browsing for an answer but haven't found what I'm looking for.



public class GUIFrame extends JFrame implements ChangeListener {

JTabbedPane tabs;

public GUIFrame(String title)
JFrame frame = new JFrame(title);
Container c = frame.getContentPane();
pack();
buildGUI(c);
setFrameAttributes(frame);

private void buildGUI(Container c)
tabs = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
MedicalSystemIO medLogs = new MedicalSystemIO();
c.setLayout(new BorderLayout());
c.add("Center", tabs);
tabs.addTab("Specialty", new SpecialtyPanel(medLogs.getListOfSpecialties()));
tabs.addTab("Doctor", new DoctorPanel(medLogs.getListOfDoctors()));
tabs.addTab("Patient", new PatientPanel(medLogs.getListOfPatients()));
tabs.addTab("Treatment", new TreatmentPanel(medLogs.getListOfTreatments(), medLogs.getListOfPatients()));
tabs.addChangeListener(this);



@Override
public void stateChanged(ChangeEvent e)
// TODO Auto-generated method stub
if(e.getSource() instanceof JTabbedPane)
tabs = (JTabbedPane) e.getSource();
System.out.println("Selected paneNo : " + tabs.getSelectedIndex());



This is a method from a class "public TreatmentPanel(...)". I did not include most of the methods but some that are used are ActionListener and ListSelectionListener.



 private void buildPanel(ArrayList<Treatment> treatments, ArrayList<Patient> patients) 

setLayout(new BorderLayout());

model = new DefaultListModel<Treatment>();
list = new JList<Treatment>(model);
list.setModel(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSize(20,80);
list.setFixedCellWidth(150);
scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

this.patients = new ArrayList<Patient>(patients);
this.treatments = new ArrayList<Treatment>();

JPanel jpaInput = createInputPanel();
JPanel jpaProcess = createProcessPanel();
JPanel jpaOutput = createOutputPanel();

add("North", jpaInput);
add("South", jpaProcess);
add("Center", jpaOutput);










share|improve this question

















  • 1





    add("North", jpaInput); - don't use the form of the method. 1) you are using "magic" values. 2) that form of the method is obsolete. The API recommends you use the "add(component, constraint)" method. So in your case that would be: "add(jpaInput, BorderLayout.PAGE_START)"

    – camickr
    Nov 13 '18 at 19:16
















0















I am new to Java and Swing if anyone could point me in a direction it'd be appreciated.



This is one class building my frame. I believe I have to use the ChangeListener you see added but have done nothing but copy a method from this site.
All I want to accomplish with my tabs is set them back to their default state if another tab is clicked.. simply meaning set the the text fields and list selection back to a state etc.



Will a method I need to make for this be in my classes for the panels or the frame? Will I need to change the scope of anything? I've been browsing for an answer but haven't found what I'm looking for.



public class GUIFrame extends JFrame implements ChangeListener {

JTabbedPane tabs;

public GUIFrame(String title)
JFrame frame = new JFrame(title);
Container c = frame.getContentPane();
pack();
buildGUI(c);
setFrameAttributes(frame);

private void buildGUI(Container c)
tabs = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
MedicalSystemIO medLogs = new MedicalSystemIO();
c.setLayout(new BorderLayout());
c.add("Center", tabs);
tabs.addTab("Specialty", new SpecialtyPanel(medLogs.getListOfSpecialties()));
tabs.addTab("Doctor", new DoctorPanel(medLogs.getListOfDoctors()));
tabs.addTab("Patient", new PatientPanel(medLogs.getListOfPatients()));
tabs.addTab("Treatment", new TreatmentPanel(medLogs.getListOfTreatments(), medLogs.getListOfPatients()));
tabs.addChangeListener(this);



@Override
public void stateChanged(ChangeEvent e)
// TODO Auto-generated method stub
if(e.getSource() instanceof JTabbedPane)
tabs = (JTabbedPane) e.getSource();
System.out.println("Selected paneNo : " + tabs.getSelectedIndex());



This is a method from a class "public TreatmentPanel(...)". I did not include most of the methods but some that are used are ActionListener and ListSelectionListener.



 private void buildPanel(ArrayList<Treatment> treatments, ArrayList<Patient> patients) 

setLayout(new BorderLayout());

model = new DefaultListModel<Treatment>();
list = new JList<Treatment>(model);
list.setModel(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSize(20,80);
list.setFixedCellWidth(150);
scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

this.patients = new ArrayList<Patient>(patients);
this.treatments = new ArrayList<Treatment>();

JPanel jpaInput = createInputPanel();
JPanel jpaProcess = createProcessPanel();
JPanel jpaOutput = createOutputPanel();

add("North", jpaInput);
add("South", jpaProcess);
add("Center", jpaOutput);










share|improve this question

















  • 1





    add("North", jpaInput); - don't use the form of the method. 1) you are using "magic" values. 2) that form of the method is obsolete. The API recommends you use the "add(component, constraint)" method. So in your case that would be: "add(jpaInput, BorderLayout.PAGE_START)"

    – camickr
    Nov 13 '18 at 19:16














0












0








0








I am new to Java and Swing if anyone could point me in a direction it'd be appreciated.



This is one class building my frame. I believe I have to use the ChangeListener you see added but have done nothing but copy a method from this site.
All I want to accomplish with my tabs is set them back to their default state if another tab is clicked.. simply meaning set the the text fields and list selection back to a state etc.



Will a method I need to make for this be in my classes for the panels or the frame? Will I need to change the scope of anything? I've been browsing for an answer but haven't found what I'm looking for.



public class GUIFrame extends JFrame implements ChangeListener {

JTabbedPane tabs;

public GUIFrame(String title)
JFrame frame = new JFrame(title);
Container c = frame.getContentPane();
pack();
buildGUI(c);
setFrameAttributes(frame);

private void buildGUI(Container c)
tabs = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
MedicalSystemIO medLogs = new MedicalSystemIO();
c.setLayout(new BorderLayout());
c.add("Center", tabs);
tabs.addTab("Specialty", new SpecialtyPanel(medLogs.getListOfSpecialties()));
tabs.addTab("Doctor", new DoctorPanel(medLogs.getListOfDoctors()));
tabs.addTab("Patient", new PatientPanel(medLogs.getListOfPatients()));
tabs.addTab("Treatment", new TreatmentPanel(medLogs.getListOfTreatments(), medLogs.getListOfPatients()));
tabs.addChangeListener(this);



@Override
public void stateChanged(ChangeEvent e)
// TODO Auto-generated method stub
if(e.getSource() instanceof JTabbedPane)
tabs = (JTabbedPane) e.getSource();
System.out.println("Selected paneNo : " + tabs.getSelectedIndex());



This is a method from a class "public TreatmentPanel(...)". I did not include most of the methods but some that are used are ActionListener and ListSelectionListener.



 private void buildPanel(ArrayList<Treatment> treatments, ArrayList<Patient> patients) 

setLayout(new BorderLayout());

model = new DefaultListModel<Treatment>();
list = new JList<Treatment>(model);
list.setModel(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSize(20,80);
list.setFixedCellWidth(150);
scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

this.patients = new ArrayList<Patient>(patients);
this.treatments = new ArrayList<Treatment>();

JPanel jpaInput = createInputPanel();
JPanel jpaProcess = createProcessPanel();
JPanel jpaOutput = createOutputPanel();

add("North", jpaInput);
add("South", jpaProcess);
add("Center", jpaOutput);










share|improve this question














I am new to Java and Swing if anyone could point me in a direction it'd be appreciated.



This is one class building my frame. I believe I have to use the ChangeListener you see added but have done nothing but copy a method from this site.
All I want to accomplish with my tabs is set them back to their default state if another tab is clicked.. simply meaning set the the text fields and list selection back to a state etc.



Will a method I need to make for this be in my classes for the panels or the frame? Will I need to change the scope of anything? I've been browsing for an answer but haven't found what I'm looking for.



public class GUIFrame extends JFrame implements ChangeListener {

JTabbedPane tabs;

public GUIFrame(String title)
JFrame frame = new JFrame(title);
Container c = frame.getContentPane();
pack();
buildGUI(c);
setFrameAttributes(frame);

private void buildGUI(Container c)
tabs = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
MedicalSystemIO medLogs = new MedicalSystemIO();
c.setLayout(new BorderLayout());
c.add("Center", tabs);
tabs.addTab("Specialty", new SpecialtyPanel(medLogs.getListOfSpecialties()));
tabs.addTab("Doctor", new DoctorPanel(medLogs.getListOfDoctors()));
tabs.addTab("Patient", new PatientPanel(medLogs.getListOfPatients()));
tabs.addTab("Treatment", new TreatmentPanel(medLogs.getListOfTreatments(), medLogs.getListOfPatients()));
tabs.addChangeListener(this);



@Override
public void stateChanged(ChangeEvent e)
// TODO Auto-generated method stub
if(e.getSource() instanceof JTabbedPane)
tabs = (JTabbedPane) e.getSource();
System.out.println("Selected paneNo : " + tabs.getSelectedIndex());



This is a method from a class "public TreatmentPanel(...)". I did not include most of the methods but some that are used are ActionListener and ListSelectionListener.



 private void buildPanel(ArrayList<Treatment> treatments, ArrayList<Patient> patients) 

setLayout(new BorderLayout());

model = new DefaultListModel<Treatment>();
list = new JList<Treatment>(model);
list.setModel(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSize(20,80);
list.setFixedCellWidth(150);
scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

this.patients = new ArrayList<Patient>(patients);
this.treatments = new ArrayList<Treatment>();

JPanel jpaInput = createInputPanel();
JPanel jpaProcess = createProcessPanel();
JPanel jpaOutput = createOutputPanel();

add("North", jpaInput);
add("South", jpaProcess);
add("Center", jpaOutput);







java swing jtabbedpane






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 19:12









Brett HasbrouckBrett Hasbrouck

31




31







  • 1





    add("North", jpaInput); - don't use the form of the method. 1) you are using "magic" values. 2) that form of the method is obsolete. The API recommends you use the "add(component, constraint)" method. So in your case that would be: "add(jpaInput, BorderLayout.PAGE_START)"

    – camickr
    Nov 13 '18 at 19:16













  • 1





    add("North", jpaInput); - don't use the form of the method. 1) you are using "magic" values. 2) that form of the method is obsolete. The API recommends you use the "add(component, constraint)" method. So in your case that would be: "add(jpaInput, BorderLayout.PAGE_START)"

    – camickr
    Nov 13 '18 at 19:16








1




1





add("North", jpaInput); - don't use the form of the method. 1) you are using "magic" values. 2) that form of the method is obsolete. The API recommends you use the "add(component, constraint)" method. So in your case that would be: "add(jpaInput, BorderLayout.PAGE_START)"

– camickr
Nov 13 '18 at 19:16






add("North", jpaInput); - don't use the form of the method. 1) you are using "magic" values. 2) that form of the method is obsolete. The API recommends you use the "add(component, constraint)" method. So in your case that would be: "add(jpaInput, BorderLayout.PAGE_START)"

– camickr
Nov 13 '18 at 19:16













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287985%2fjtabbedpane-how-can-i-set-properties-when-a-new-tab-is-selected%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287985%2fjtabbedpane-how-can-i-set-properties-when-a-new-tab-is-selected%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto