-1

I have a frame that contains a mainPanel. This last will add other commandPanels (each one contains a button and a textField) Dynamically. the problem is that the JScrollPane does not appear to let me use the unseen commandPanels even if the mainPanel is full. The below picture shows my case.

enter image description here

To initialize the window I wrote below code:

frame = new JFrame();
frame.setBounds(100, 100, 962, 639);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); 

mainPanel = new JPanel();
mainPanel.setBounds(264, 6, 692, 500);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));    

scroll = new JScrollPane();
scroll.getViewport().add(mainPanel);
frame.getContentPane().add(scroll); 

and the method that add dynamically the new commandPanels is:

public void loadCommandPanel(String commandName)
{
    CommandPanel newCommandPanel = new CommandPanel();
    newCommandPanel.getCommandBtn().setText(commandName);   
    mainPanel.add(newCommandPanel);

    scroll.getViewport().add( newCommandPanel );
    mainPanel.add( scroll, BorderLayout.EAST);
    frame.add( mainPanel);

    ...
}

Any help to get the scrollPane, will be much more than appreciated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Guizmoo03
  • 660
  • 1
  • 7
  • 23
  • I'd recommend avoiding the user of `null` layouts and the `setBounds` method. Try that and see if it works out. Also, why are you adding the panel to the viewport, instead of using `scroll = new JScrollPane(mainPanel);`? – Obicere Feb 15 '15 at 07:05
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Feb 15 '15 at 07:22
  • 1
    *"(each one contains a button and a textField)"* Consider instead using a `JList` to hold instances an object that has a `label` and a `command` attribute. Use a rendererer to show the `label` in a `JLabel` and the `command` in a `JTextField`.. – Andrew Thompson Feb 15 '15 at 07:25
  • 1
    Or use a `JList` or `JTable`... – MadProgrammer Feb 15 '15 at 08:29

2 Answers2

1

scroll.getViewport().add(mainPanel); is not how you use JViewport or JScrollPane; instead you should using something like this: scroll.getViewport().setView(newCommandPanel);

or

scroll.setViewportView(newCommandPanel);

Take a look at How to Use Scroll Panes for more details.

Note also, this doesn't makes sense:

CommandPanel newCommandPanel = new CommandPanel();
newCommandPanel.getCommandBtn().setText(commandName);   
mainPanel.add(newCommandPanel);

scroll.getViewport().add( newCommandPanel );

You add newCommandPanel to mainPanel, then promptly add it to another container (albeit incorrectly).

A component can only reside on a single parent; the moment you add it to another container, it is automatically removed from the previous container.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

I have made some changes and it works perfectly now. For those who want the same thing here's my code:

import ...

public class mainUserInterface {

private JFrame frame;
private JPanel mainPanel;
private  JPanel commandsPanel;
private JScrollPane commandsScrollPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                mainUserInterface window = new mainUserInterface();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public mainUserInterface() {
    initialize();
}

private void initialize() {

    frame = new JFrame("CommandsExecutor");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(1000, 700));

    mainPanel = new JPanel(new BorderLayout(5,5));
    mainPanel.setBorder( new TitledBorder("") );


    commandsPanel = new JPanel();
    commandsPanel.setLayout(new BoxLayout(commandsPanel, BoxLayout.Y_AXIS));

    for(int i=0; i<15;i++){

        commandsPanel.add(new CommandPanel());
    }

    commandsScrollPane = new JScrollPane(commandsPanel);
    mainPanel.add(commandsScrollPane,BorderLayout.CENTER);


    frame.setContentPane(mainPanel);
    frame.pack();
    frame.setVisible(true);
}
}

and Here's the commandPanel class:

import ...
public class CommandPanel extends JPanel {

private JTextField commandResult;
private JButton commandBtn;

public CommandPanel()
{
    this.setLayout( new BorderLayout(10,10));
    this.setBorder( new TitledBorder("Command:") );
    this.setMaximumSize(new Dimension(692,60));
    this.setMinimumSize(new Dimension(692,60));


    commandBtn = new JButton("Execute");
    commandBtn.setMaximumSize(new Dimension(137, 34));
    commandBtn.setMinimumSize(new Dimension(137, 34));
    this.add(commandBtn, BorderLayout.WEST);

    commandResult = new JTextField();
    commandResult.setMaximumSize(new Dimension(518, 34));
    commandResult.setMinimumSize(new Dimension(518, 34));
    this.add(commandResult, BorderLayout.CENTER);
}

public JTextField getCommandResult() {
    return commandResult;
}

public JButton getCommandBtn() {
    return commandBtn;
}

public void setCommandResult(JTextField commandResult) {
    this.commandResult = commandResult;
}

public void setCommandBtn(JButton commandBtn) {
    this.commandBtn = commandBtn;
}
}

Thanks for all who answered my question, it really helped.

Guizmoo03
  • 660
  • 1
  • 7
  • 23