-1

I have a simple swing App, that can manage a specific type of project with multiple JButton and that print the project tree on the bottom, see the screenshoot bellow when a project is opened in the App : App screenshoot with project opened

The thing is when no project is opened I get something like this : App screenshoot without project opened

The HMI is simple and looks like this :

public class Desktop extends JFrame implements ActionListener {
  public Desktop() {

    JButton newProject, generate, quit, bAddToClassPath, openProject, saveProject;
    JPanel mainPanel;
    JScrollPane jscrollpane;

    super("MainWindow");
    setLookAndFeel();
    setSize(330, 440);
    ParamMainPanel();
    setVisible(true); 
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
       new Desktop();
      }
    });

  }

  public static void ParamMainPanel() {
    mainPanel = new JPanel(new FlowLayout());

    // BUTTONS PARAM
    newProject = new JButton("Nouveau projet");
    generate = new JButton("Générer...");
    quit = new JButton("Quitter");
    openProject = new JButton ("Ouvrir projet");
    saveProject = new JButton ("Sauvegarder");

    bAddToClassPath = UIUtil.iconButton();
    bAddToClassPath.setActionCommand("setCP");
    bAddToClassPath.addActionListener(this);
    mainPanel.add(bAddToClassPath);

    newProject.addActionListener(this);
    newProject.setActionCommand("newP");
    generate.addActionListener(this);
    generate.setActionCommand("gen");
    quit.addActionListener(this);
    quit.setActionCommand("qui");
    openProject.addActionListener(this);
    openProject.setActionCommand("openP");
    saveProject.addActionListener(this);
    saveProject.setActionCommand("save");

    mainPanel.add(newProject);
    mainPanel.add(generate);
    mainPanel.add(openProject);
    mainPanel.add(saveProject);
    mainPanel.add(quit);

    // PROJECT TREE 
    jscrollpane = new JScrollPane(new JTree());
    jscrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    jscrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    jscrollpane.setMinimumSize(new Dimension(50, 50));
    jscrollpane.setLocation(4,61);
    jscrollpane.setSize(306,322);
    mainPanel.add(jscrollpane);
  }

}

So what I want is at the App launch, instead of having the bad looking display JTree (into the jscrollpane) without project opened, having the same display with a project opened (white bloc) but without the project tree inside. I can't figure how to do it, any ideas ?

Logan Wlv
  • 3,274
  • 5
  • 32
  • 54
  • 1
    1) `jscrollpane.setHorizontalScrollBarPolicy(30);` Don't use 'magic numbers' when there are defined constants for the task. The constants will benefit from compile-time checking, and are often more human readable. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 18 '17 at 09:39
  • Thanks for the advice, I did some edits for the 'magic numbers', since it it just a display problem I thought it was not needed to explain the whole 'project specific type' which is quite huge. – Logan Wlv May 18 '17 at 09:56
  • Where's the MCVE / SSCCE? – Andrew Thompson May 18 '17 at 10:31
  • Not sure if it really answer fully to MCVE, but here's some edits, even though I finally found a way to display it correctly. – Logan Wlv May 22 '17 at 14:37
  • *"Not sure if it really answer fully to MCVE"* Can you copy/paste it into an editor, compile it **without changes**, then run it (again without changes)? That is the 'complete' or 'self contained' part. Does it include components or code unrelated to the problem? Then it's not minimal or short.. Really, it isn't rocket science. – Andrew Thompson May 22 '17 at 16:18
  • Yes got it, the thing that I'm working on is quite huge, and doing a clear simple example would take me a long time since I don't even understand the whole thing myself, my fault being lazy. But I will take in count all you said for the next one, thank you for taking the time to give me advices. – Logan Wlv May 23 '17 at 09:02

1 Answers1

-1

Here the answer I found to resolve this display problem :

The FlowLayout used on my main panel somehow was preventing me from resizing my jscrollpane directly using setSize() So I decided to have a secondary panel on my MainFrame secondMainPanel without specific layout using new JPanel(null);

I did add jscrollpaneon it, then I could resize it without problems to have a correct display.

I think there might be better way to fix it, but this one works.

Logan Wlv
  • 3,274
  • 5
  • 32
  • 54
  • *"So I decided to have a secondary panel on my `MainFrame` `secondMainPanel` without specific layout using `new JPanel(null);`"* That isn't a solution so much as an hack that will result in further problems. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. 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). – Andrew Thompson May 22 '17 at 16:38