0

I am trying to get my JTextArea to display under all other contents of the llpPanel. My code is below with a screenshot of what my code displays. In the code you will see that I have set my dimensions for the JTextArea to (50, 50). Then in the llpPanel I have added BorderLayout.PAGE_END. I have also tried to (instead of PAGE_END) put CENTER and SOUTH. When I put SOUTH, it shows a white line at the very bottom of the program but you cannot do anything with it.

enter image description here

    import java.awt.BorderLayout;
    import java.awt.ComponentOrientation;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JRadioButton;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;


    public class TestApplication implements ActionListener {

        public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setSize(1000, 1000);
        frame.setTitle("RBA Test Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        JButton initialize = new JButton("Initialize");         
            JButton connect = new JButton("Connect");
        JButton disconnect = new JButton("Disconnect"); 
        JButton shutdown = new JButton("Shut Down");
        JButton portsettings = new JButton("Port Settings");    
        JButton online = new JButton("Go Online");       
        JButton offline = new JButton("Go Offline");         
        JButton status = new JButton("Status"); 
        JButton reboot = new JButton("Reboot");      
        JButton account = new JButton("Account");
        JButton amount = new JButton("Amount");
        JButton reset = new JButton("Reset");
        JButton approvordecl = new JButton("Approve / Decline");

JTextArea logbox = new JTextArea(50, 50);

        JPanel testPanel = new JPanel();
        testPanel.add(button);
        testPanel.add(button2);
        testPanel.add(checkbox2);

        JPanel posPanel = new JPanel();
        posPanel.add(test);
        posPanel.add(testing);
        posPanel.add(checkbox);

        JPanel llpPanel = new JPanel();
        llpPanel.add(online);
        llpPanel.add(offline);
        llpPanel.add(status);
        llpPanel.add(reboot);
        llpPanel.add(account);
        llpPanel.add(amount);
        llpPanel.add(reset);
        llpPanel.add(approvordecl);
        llpPanel.add(logbox, BorderLayout.PAGE_END);

            JPanel buttonPanel = new JPanel();
        buttonPanel.add(initialize);
        buttonPanel.add(connect);
        buttonPanel.add(disconnect);
        buttonPanel.add(shutdown);
        buttonPanel.add(portsettings);
        frame.add(buttonPanel);
        frame.add(buttonPanel, BorderLayout.NORTH);

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
                tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
        tabbedPane.addTab("Test", null, testPanel, "Test");

        JPanel tabsPanel = new JPanel(new BorderLayout());
        tabsPanel.add(tabbedPane);
        frame.add(tabsPanel, BorderLayout.CENTER);  

        frame.pack();
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub      
    }
    }

Updated code with screenshot is below...

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class TestApplication implements ActionListener {

  public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setSize(1000, 1000);
    frame.setTitle("RBA Test Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);



    JTextArea logbox = new JTextArea(50, 50);




    JButton initialize = new JButton("Initialize");

    JButton connect = new JButton("Connect");

    JButton disconnect = new JButton("Disconnect");

    JButton shutdown = new JButton("Shut Down");


    JButton portsettings = new JButton("Port Settings");


    JButton online = new JButton("Go Online");

    JButton offline = new JButton("Go Offline");

    JButton status = new JButton("Status");

    JButton reboot = new JButton("Reboot");


    JButton account = new JButton("Account");


    JButton amount = new JButton("Amount");


    JButton reset = new JButton("Reset");


    JButton approvordecl = new JButton("Approve / Decline");

    JButton test = new JButton("Test Button #1");

    JButton testing = new JButton("Test Button #2");

    JRadioButton button = new JRadioButton("Radio Button");

    JRadioButton button2 = new JRadioButton("Radio Button");

    JCheckBox checkbox = new JCheckBox("Check Box");

    JCheckBox checkbox2 = new JCheckBox("Check Box");


    JPanel newButtonPanel = new JPanel();
    newButtonPanel.add(online);
    newButtonPanel.add(offline);
    newButtonPanel.add(status);
    newButtonPanel.add(reboot);
    newButtonPanel.add(account);
    newButtonPanel.add(amount);
    newButtonPanel.add(reset);
    newButtonPanel.add(approvordecl);


    JPanel testPanel = new JPanel();
    testPanel.add(button);
    testPanel.add(button2);
    testPanel.add(checkbox2);

    JPanel posPanel = new JPanel();
    posPanel.add(test);
    posPanel.add(testing);
    posPanel.add(checkbox);

    JPanel llpPanel = new JPanel();
    llpPanel.setLayout(new BorderLayout());
    llpPanel.add(newButtonPanel);
    llpPanel.add(logbox, BorderLayout.PAGE_END);

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(initialize);
    buttonPanel.add(connect);
    buttonPanel.add(disconnect);
    buttonPanel.add(shutdown);
    buttonPanel.add(portsettings);
    frame.add(buttonPanel);
    frame.add(buttonPanel, BorderLayout.NORTH);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
    tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
    tabbedPane.addTab("Test", null, testPanel, "Test");

    JPanel tabsPanel = new JPanel(new BorderLayout());
    tabsPanel.add(tabbedPane);
    frame.add(tabsPanel, BorderLayout.CENTER);


    frame.pack();



}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}






}

screenshot2

Kendra Cheatham
  • 117
  • 5
  • 16
  • 1
    My eyes are crossing. Can you simplify this code to a point that the error is more evident? – Lee Meador Jun 04 '13 at 18:07
  • Yes, 90% or more of your code is not relevant to your question. Also could you explain your problem better so that it is clearer to us. – Hovercraft Full Of Eels Jun 04 '13 at 18:09
  • If you look at the screenshot that is what my code displays..the big white space on the side of the buttons, i need it under the buttons. I have tried almost every borderlayout option to get it under the buttons but it won't work right. – Kendra Cheatham Jun 04 '13 at 18:16

2 Answers2

2

JPanels use FlowLayout by default so applying BorderLayout constraints such as PAGE_END will have no effect. You need to set the layout of the panel:

llpPanel.setLayout(new BorderLayout());

Then you will encounter the problem of components displacing themselves in the BorderLayout.CENTER position. The solution is to create another JPanel as a container for the components other than logbox on llpPanel.

JPanel newButtonPanel = new JPanel();
newButtonPanel.add(online);
...
llpPanel.add(newButtonPanel);
JScrollPane scrollPane = new JScrollPane(logbox) {
    @Override
    public java.awt.Dimension getPreferredSize() {
        return new Dimension(500, 500);
    };
};
llpPanel.add(scrollPane, BorderLayout.PAGE_END);

Use a JScrollPane rather than adding the JTextArea directly to the container.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • ok when i do that the buttons on my llpPanel are gone. Is that because of the dimensions that i set for the JTextArea? – Kendra Cheatham Jun 04 '13 at 18:20
  • Did you add all the buttons to `newButtonPanel` and then add it to `llpPanel` as above? – Reimeus Jun 04 '13 at 18:24
  • yes i did and the buttons are covered. I tried changing PAGE_END to CENTER also and it did the same thing – Kendra Cheatham Jun 04 '13 at 18:26
  • Defintely keep the `newButtonPanel` at `CENTER` and the `logbox` at `PAGE_END`. Buttons are displaying fine here can you attach the question with your updated code? – Reimeus Jun 04 '13 at 18:30
  • The problem is with how Mac displays the application. This displays fine on Windows. Try overriding `getPreferredSize` for `newButtonPanel` as shown above – Reimeus Jun 04 '13 at 18:48
  • it shows a green arrow on the side beside public java.awt.Dimension getPreferredSize() – Kendra Cheatham Jun 04 '13 at 18:59
  • You need to override `getPreferredSize` on your `JScrollPane` rather than the `JTextArea` otherwise scrollbars wont appear – Reimeus Jun 04 '13 at 22:02
  • @Reimeus I don't see the point in inline deriving from JScrollPane... just to set the preferred size property. How about instantiating the JScrollPane, and just set the property? `JScrollPane scrollPane = new JScrollPane( logbox ); scrollPane.setPreferredSize( new Dimension( 500, 500 ) );`. You're obfuscating the point IMO. – TT. Jun 06 '13 at 06:25
  • @TT See _[Should I avoid the use of setPreferredSize?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi)_ – Reimeus Jun 06 '13 at 10:43
  • @Reimeus My goodness what a whole lot of unsubstantiated BS, obviously written by people that do not have a Msc in Computer Sciences. Unreal man, unreal. – TT. Jun 06 '13 at 12:20
  • The general consensus in software development is: if you can avoid inheritance, avoid it. Yes, in OO languages too. In this case overriding just to set a property is the worst reason to ever inherit. – TT. Jun 06 '13 at 12:21
1

Set the component's preferred size property rather than its size, and add it to BorderLayout.SOUTH. For BorderLayout layouts, the container will try to use preferred sizes for the edges (north,south,east and west) and resize the center accordingly.

A short snipped example to illustrate. The view is a panel, which will have the text area at the bottom that is 50 high. This is done by adding the JTextArea component at BorderLayout.SOUTH and set the preferred size property at Dimension(0,50). The rest of the view is filled with a panel. This panel is placed at the BorderLayout.CENTER and will be resized by the layout manager.

JPanel view = new JPanel( );
view.setSize( 800, 600 );
view.setLayout( new BorderLayout( ) );

JPanel topArea = new JPanel( );
JTextArea textArea = new JTextArea( );
textArea.setPreferredSize( new Dimension( 0, 50 ) );

view.add( topArea, BorderLayout.CENTER );
view.add( textArea, BorderLayout.SOUTH );
TT.
  • 15,774
  • 6
  • 47
  • 88