0

First post, and tried to research this as much as possible but finally gave up and decided to ask.

I am building a recipe book program, and on one page it has the list of recipes. When you select a recipe it will appear on the left side as labels. The issue is on my page I have two JPanels. One hosts the list of recipes, the other is labels for displaying the information. On the side that has the labels I cant get them to move, or alter or anything inside the JPanel.

I feel this has a simple solution but I am too inexperienced to see. Anyway thanks for the help in advance.

////Form Panel Class

    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;


    public class FormPanel extends JPanel {

private JScrollPane pane;
private JPanel pane2;

// viewing of information
private JLabel nameLabel1;
private JLabel nameLabel2;
private JLabel nameLabel3;


public FormPanel() {

    nameLabel1 = new JLabel("Label1");
    nameLabel2 = new JLabel("Recipe Name: ");
    nameLabel3 = new JLabel("Tag Lines: ");

    pane = new JScrollPane();
    pane2 = new JPanel();

    layoutComponents();

    pane.setPreferredSize(new Dimension(200, 450));
    pane2.setBorder(BorderFactory.createTitledBorder("Recipes"));
    pane2.setPreferredSize(new Dimension(375, 450));

}

public void layoutComponents() {
    setLayout(new GridBagLayout());

    GridBagConstraints gc = new GridBagConstraints();

    ///////////////////////////////////////
    // First Row
    gc.gridy = 0;

    gc.weightx = 1;
    gc.weighty = 1;

    gc.gridx = 1;

    gc.anchor = GridBagConstraints.FIRST_LINE_START;

    add(pane2, gc);
    ////////////////////////////////////////////

    ///////////////////////////////////////
    // First Row
    gc.gridy = 0;

    gc.weightx = 2;
    gc.weighty = 1;

    gc.gridx = 1;

    gc.anchor = GridBagConstraints.FIRST_LINE_END;
    gc.insets = new Insets(5, 5, 5, 5);
    add(pane, gc);
    ////////////////////////////////////////////

    GridBagConstraints gc2 = new GridBagConstraints();
    //

    gc2.gridy = 0;

    gc2.weightx = 1;
    gc2.weighty = 1;

    gc2.gridx = 0;

    gc2.fill = GridBagConstraints.PAGE_START;

    pane2.add(nameLabel2, gc2);

    ////////////////////////////////////////////
    gc2.gridy = 1;

    gc2.weightx = 1;
    gc2.weighty = 1;

    gc2.gridx = 0;

    gc2.fill = GridBagConstraints.CENTER;

    pane2.add(nameLabel3, gc2);

    ////////////////////////////////////////////
    gc2.gridy = 2;

    gc2.weightx = 1;
    gc2.weighty = 1;

    gc2.gridx = 0;

    gc2.fill = GridBagConstraints.SOUTH;

    pane2.add(nameLabel1, gc2);

}
}

//Mainframe Class

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class Mainframe extends JFrame {

private FormPanel formPanel;
private JTabbedPane tabPane;

public Mainframe() {
    super("My Digital Cookbook");

    setLayout(new BorderLayout());
    formPanel = new FormPanel();
    tabPane = new JTabbedPane();

    add(tabPane, BorderLayout.PAGE_START);

    tabPane.addTab("New Recipe", formPanel);

    setMinimumSize(new Dimension(500, 600));
    setSize(600, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}
}

//Application Class

import javax.swing.SwingUtilities;

public class Application {
    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() { 

            public void run(){
                            System.out.println(SwingUtilities.isEventDispatchThread());
                new Mainframe();

            }
        });

    }
}

The lower three labels might have wonkey weights and what not just due to trying to get them to move but they all just stay in a straight line near the top of the Jpanel.

Additionally I don't think I needed to re-declare my gridbagconstraints but I decided to try for debugging attempts.

**Update -I now have posted a much simpler version of my program with the error still existing. I apologize that I don't have it smaller due to lack of flexible skills.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
C. Skjerdal
  • 2,750
  • 3
  • 25
  • 50
  • 3
    For us to fully understand your problem, and in order to try to help you find a best solution, we would have to create our own [mcve] programs. But since we're all volunteers, and you are the one asking for free advice, it would be much better, and much to your advantage to create your own [mcve] first and post it with your question. This would make our work much easier to do, and would likely help generate a decent answer for you quickest. – Hovercraft Full Of Eels Apr 10 '16 at 16:23
  • Of course that makes sense, I will get on that right now – C. Skjerdal Apr 10 '16 at 16:41
  • Great, note that I'm not your down-voter but **will** be your up-voter if you follow through on this. – Hovercraft Full Of Eels Apr 10 '16 at 16:51
  • Why don't you use a JTextArea in a JScrollPane in order to show the recipe information in your right-side panel? – Costis Aivalis Apr 10 '16 at 17:10
  • Code is updated and simplified. Sorry I cant get this down to one class, though if that's the way it should be I can delete this post and work on that today. - Thanks! - @Costis, that is a possible option, though I figure this system makes it easier to eventually have the fields editable and update-able. Additionally they will be easier for future sorting functions/algorithms. – C. Skjerdal Apr 10 '16 at 17:12
  • *"Sorry I cant get this down to one class,"* An MCVE can have more than one class. E.G. [example 1](http://stackoverflow.com/questions/6118737/how-to-draw-in-jpanel-swing-graphics-java/6118902#6118902) (inner class), [example 2](http://stackoverflow.com/questions/15025092/border-with-rounded-corners-transparency/16909994#16909994) (default access class), [example 3](http://stackoverflow.com/a/7616206/418556) (both examples have a default access class) .. – Andrew Thompson Apr 11 '16 at 00:17
  • Thanks for the help everyone, in the end I have switched to just a a JTextArea as Costis has suggested. Also looking at JTextPane. – C. Skjerdal Apr 11 '16 at 22:53

0 Answers0