0

I have the following code to create GUI. The issue i am having is that the JTextField appears to expand as the GUI is resized.

I want the JTextField to stay the same size as the GUI is enlarged. The JTextField also seems to reside at the bottom of the control panel rather than at the top. How do i have control of this?

Can someone provide some helo:

public class HtmlDemo extends JPanel
                      implements ActionListener {
    JLabel theLabel;
    JTextArea htmlTextArea;
    private boolean bLastName = false;
    private boolean bNickName = false;
    private boolean bMarks = false;
    private JFreeChart chart;
    private ChartPanel chartPanel;    
    private JLabel date1Label, date2Label,date3Label,date4Label;
    private JTextField date1Field, date2Field,date3Field,date4Field;
    private JButton button,button1;
    private Container window;
  //  public createChart stackedChart = new createChart();
    public XYPlot plot1;
    private JPanel panel, panel1,panel3,panel4;

    public HtmlDemo() {
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

      chart = createChart(null);
        chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(1060, 370));
        chartPanel.setMouseZoomable(true, false);
        //panel4.add(chartPanel);

        htmlTextArea = new JTextArea(10, 20);
        //htmlTextArea.setText(initialText);
        JScrollPane scrollPane = new JScrollPane(chartPanel);


        theLabel = new JLabel() {
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
            public Dimension getMinimumSize() {
                return new Dimension(200, 200);
            }
            public Dimension getMaximumSize() {
                return new Dimension(200, 200);
            }
        };
        theLabel.setVerticalAlignment(SwingConstants.CENTER);
        theLabel.setHorizontalAlignment(SwingConstants.CENTER);

        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
        leftPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder(
                    "Rolling Demand"),
                BorderFactory.createEmptyBorder(10,10,10,10)));
        leftPanel.add(scrollPane);
        leftPanel.add(Box.createRigidArea(new Dimension(0,10)));
        //leftPanel.add(changeTheLabel);

        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
        rightPanel.setBorder(BorderFactory.createCompoundBorder(
                        BorderFactory.createTitledBorder("Controls"),
                        BorderFactory.createEmptyBorder(10,10,10,10)));
        rightPanel.add(theLabel);


        button = new JButton();
        date1Label = new JLabel("Enter Start Date");
       rightPanel.add(date1Label);
        date1Field = new JTextField(5);
  rightPanel.add(date1Field);
        date2Label = new JLabel("Enter End Date");
      rightPanel.add(date2Label);
        date2Field = new JTextField(5);

      rightPanel.add(date2Field);

       panel1 = new JPanel();
        date3Label = new JLabel("Enter comparator Start Date");
        rightPanel.add(date3Label);
        date3Field = new JTextField(5);
        rightPanel.add(date3Field);
        date4Label = new JLabel("Enter comparator End Date");
        rightPanel.add(date4Label);
        date4Field = new JTextField(5);
        rightPanel.add(date4Field);





      button.setText("Get Data");

        rightPanel.add(button);



        setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        add(leftPanel);
        add(Box.createRigidArea(new Dimension(10,0)));
        add(rightPanel);
    }


     private JFreeChart createChart(final XYDataset dataset)
    {
       // System.out.println("hello");
        return ChartFactory.createTimeSeriesChart(
                "Test",
                "Seconds",
                "Value",
                dataset,
                false,
                false,
                false);
    }

    //React to the user pushing the Change button.
    public void actionPerformed(ActionEvent e) {
        theLabel.setText(htmlTextArea.getText());
    }

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

        //Add content to the window.
        frame.add(new HtmlDemo());

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

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            createAndShowGUI();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
inky
  • 137
  • 7
  • 1
    *I want the JTextField to stay the same size as the GUI is enlarged.* - you have multiple text fields in the code. *The JTextField also seems to reside at the bottom of the control panel rather than at the top.* - components are displayed based on the rules of the layout manager and the order in which you add the components to the panel. The code you posted is NOT an [mre] since you are using non-standard JDK classes, so we can't see the problems you describe. Post a proper "MRE" demonstrating your problem. If you concern is the text field then the JFreeChart components should not matter. – camickr Jun 14 '20 at 20:06
  • Sorry i wasnt clear in my question. All my JTextFields have the same behaviour. I want them all to stay the same size (a typical JTextField seen in online forms) – inky Jun 15 '20 at 13:40
  • The question was clear. Maybe my answer was not clear. Your question is about layout managers, but you still have not posted your [mre] showing how you are using the layout managers. We can not copy/paste/compile and test the code posted. Since your question is only about the text fields, the "MRE" should remove the 3rd party components and demonstrate the problem with your text fields. – camickr Jun 15 '20 at 13:50
  • I would like upload a snapshot of the issue. Does stackexchange allow .jpeg uploads – inky Jun 15 '20 at 19:40
  • We are not interested in a snapshot. The code is causing the problem. We need the code. You have made a statement that you have a problem with text fields. So post an [mre] demonstrating the problem. It will take you 5 minutes to create the "MRE". If you are not interested in spending 5 minutes to create the MRE, then I am not interested in helping. If you would have posted a proper MRE with your question I am certain you would have had an answer already. – camickr Jun 15 '20 at 23:12

1 Answers1

-1

A Layout controls what gets put where depending on the size. As long as you have a layout, it will always resize. Most of the time, you want this behavior to happen, but to disable simply put

parentContainer.setLayout(null);

This is called a null layout. Note I put parentContainer because you have a bunch of text fields so I don't know which one you want.

Then you can use the textField.setBounds(x, y, width, height) to specify location.

But I do recommend you don't use null layouts.

Higigig
  • 1,175
  • 1
  • 13
  • 25
  • (1-) You should NOT use a null layout. That is not the solution. The solution is to use the layout manager properly. *As long as you have a layout, it will always resize* - that is NOT true. Whether a component resizes or not is based on the rules of the layout manager. – camickr Jun 14 '20 at 20:10