Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Trouble with Swing workflow

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am looking for someone to help me with a few different issues I've had when using swing across a few editors.


1)
When using only raw code I can not figure out how to get a jList to initialize to a set size when empty. it always renders at 0px.
I've tried setMinimumSize, SetPreferedSize and SetSize, I've also tried putting the jList in a Jpanel with a set Size. This kind of works but still looks wrong.


2) When trying to use IntelliJs visual editor for swing, GUIForm. It seems impossible for me to lay things out in any constructive manner. Things go all over the place, and changing the layout mangers doesn't seem to do anything for me. There is so much unwanted behavior I cant even understand why this is even a feature. I hired a tutor who said she had exp with swing in IntelliJ and even she couldnt figure it out.

3) The tutor recommended I try NetBeans. I am now on here. Its going a bit better but I am having trouble editing anything in my jLists. It starts with 5 items in the design editor. I am trying to clear those on launch with :



Yet no changes take place.
I've also added a listener for a right click on a selected Item in the list to show a popup menu and that also is not working?





Here is a rough idea of what I am trying to make ( i started in C++ but gave up because C++ is rough)


I could really use some hands-on help solving this stuff. I tried Chegg and it was unsuccessful so if you are willing to help me, or know a place I can go please provide me a link.
Thank you so much!
 
Rancher
Posts: 3232
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't recommend using an IDE to generate your code. You spend time learning the IDE and not learning Swing.

The key to layout design is to logically group components onto a panel and then use an appropriate layout manager to display the components.

The trick is that you can nest panels with different layout managers as necessary.

Maybe start with the working code found in the Swing tutorial on How to Use Lists,

The tutorial also has a section on "Layout Manager" which contains working examples of each layout manager on its own.

I'm not sure why you show the bottom row with 3 columns, since there are no components being displayed.

So assuming you really only need a layout with 3 columns I might suggest you start with:

1. use the default BorderLayout of the frame
2. Add your JScrollPane containing the JList to the BorderLayout.LINE_START
3. Create a JPanel with a GridLayout and add it to the BorderLayout.CENTER
4. Create a panel for the "Add From File" components and add components to the panel and add the panel to the grid layout panel
5. Create another panel for the "Class Name" components and add the panel to the grid layout panel

so the key to to start with a top level logical design for each panel and then repeat the process as you get into more detail for each panel.

You will eventually learn which layout manager to use in a particular situation.
 
Steve Datz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help and suggestion.

I've actually made a GUI from only code before.

It looks like this with a bit more functionality than I can show in 1 screenshot




So i have some experience.

The main thing holding me back from doing it all through code is the jlist starts off at 0px if its empty.
I want to have a set space for it on start, but I cant figure that out.

i asked my teacher about this and she recommended I use these visual editors, which has led me down this path

If you recommend only using code, could you perhaps speculate on a way to set the size of a jList on start to not be 0px?
I skimmed the jlist link you sent, but it didnt see anything?

Thanks!
 
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been looking in Swing code that I've written which uses JList, to see what I did to make it actually appear in the GUI. And the answer is... I didn't do anything. However, I only have one case where the JList starts out empty, and it's convenient for my design that it isn't visible in that case.

So that leads me to a different question: what would you expect to see when you display an empty JList? Seems to me that seeing nothing isn't that bad of a design.
 
Steve Datz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks for looking!

I would want them to appear as a full size text box, with no data or an empty first entry.

Like seen circled in red in this image from a C++ version I tried to make. That is the equivalent of a jList in C++. And using VSs visual editor I was able to set its dimensions

 
Rob Camick
Rancher
Posts: 3232
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The main thing holding me back from doing it all through code is the jlist starts off at 0px if its empty.



Not sure why you say that.

The JList has a default size when I run:


I can then control the size by adding:


The above code example is called a SSCCE.

Every question should contain a SSCCE so we can verify the code you are using.
 
Steve Datz
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help ,

I finally got it via this:



I will keep your way in mind as well.

Thanks so much for your help everyone. I am going to go back to writing this via code only and give up on the visual stuff
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, if you want a border around the empty list then a JScrollPane would do. And if you expect the list to expand to an unknown size then you'd want a JScrollPane anyway.

I think probably any old JPanel would do too, although I haven't tried that.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually... I feel from experience what you think you have found will be as much trouble at this point as ..."you do know that to restore honour to your family means having  sex with the Black eagle"... Alike programming the words and meanings didn't mesh well - YouTube "sexually confused ninja.

From experience (before this gets ugly that will probly work for a while )

JScrollpane has a part to insert called a JViewport
The viewport is for the JScrollpane to hold , the viewport holds the list or a panel sized the same as the JScrollpane holder panel.

To cause the JScrollpane to sit in the correct size expanded it is placed into a jpanel with a "borderlayout."
Borderlayout  of the jpanel causes the internal of it to pull open to its borders the component placed in it - the JScrollpane.

To set the size of a swing component requires four COMPULSORY settings
First setpreferredsize
Setminimumsize
Setmaximumsize
now setSize

Unless ALL four of these are present called on the component (for most is always the jpanel containing something needing a layout and separation) then a size cannot be caused for it in swing.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samuel Marchant wrote:To set the size of a swing component requires four COMPULSORY settings
First setpreferredsize
Setminimumsize
Setmaximumsize
now setSize

Unless ALL four of these are present called on the component (for most is always the jpanel containing something needing a layout and separation) then a size cannot be caused for it in swing.



And yet... have a look at TextFieldDemo.jara, from Oracle's tutorial about text fields. Nowhere in that code are any of those four methods called.

Also, the setSize() method sets the actual size of a component, whereas the other three methods just provide suggestions to layout managers about how they should display the component. So the idea that you have to call all four methods is also incorrect.
 
Samuel Marchant
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

And yet... have a look at TextFieldDemo.jara, from Oracle's tutorial about text fields. Nowhere in that code are any of those four methods called.

Also, the setSize() method sets the actual size of a component, whereas the other three methods just provide suggestions to layout managers about how they should display the component. So the idea that you have to call all four methods is also incorrect.



With something that simple I don't give it any point of parity, textfields tend to default to default font size if they have had their number of columns set.

When you finally cannot get that to occur reliably if it ever does (rarely but sometimes in swing when using setSize) do the above.
NB some layouts override sizing calls.

and gridbaglayout sizes in ratio with the size of the jframe.
 
Samuel Marchant
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just remembered, the main one is setpreferredsize in swing, because a component "needs" a size but layout and higher level stacking components can interfere with it.
setSize only commits that for a flash of a moment, other actions on the interface thereafter could cause it to lose that hold of the sizing.
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samuel Marchant wrote:With something that simple I don't give it any point of parity, textfields tend to default to default font size if they have had their number of columns set.



Well, exactly. So suggesting to people in CAPITAL LETTERS that they must set the size of Swing components is less than helpful.
 
Samuel Marchant
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Well, exactly. So suggesting to people in CAPITAL LETTERS that they must set the size of Swing components is less than helpful.



Trying to setSize by continually reapplying the same old cracked record setSize won't cause it to operate either.
setPreferredSize has to be set on
swing components if they are in the direct layout of  a component that is not a layout that controls the size of the next direct component e.g. borderlayout boxlayout and gridbaglayout.
setSize may work temporarily with flowlayout because it only controls position in it but setSize is only an action like repaint() after it occurs anything changing it by effect in the VM operations can and that can occur immediately or before it is rendered to the screen, it's an action on it not a setting setPreferredSize is closer to a setting .
It's not that setSize does not do that, it happens in a moment just like repaint does it comitted like repaint does but the effect of the action has no guarantee it will be there after the method is finished with it in nonoseconds.

I think you will find (and in my 20 000 lines of code in 2 swing programs) you want to do what I said and use at least the three method and setSize is is just an il perfecto contentness if the containing layout allows sizing calls.

 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right about setSize() -- it's better to use setPreferredSize() so that the layout managers have more flexibility to manage the size of components.

But I just scanned my Swing applications -- I don't have lines-of-code counts but there are about 700 classes in them -- to see where I used setPreferredSize(). There are just 10 instances, and all of them set the preferred size of a JPanel or a JScrollPane, mostly to a standard size. Otherwise the layout managers just fit components into the panels in a reasonable way which works reasonably well. I'm using FlowLayout, BoxLayout, and occasionally GridLayout but only to split a panel into two equal parts. No GridBagLayout, it just gave me a headache looking at the examples.
 
Samuel Marchant
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setSize is inherited from java.awt the other three are part of swing. To use setSize requires the layout manager not committing any sizing on the component you want to size, but since that can be a contradiction in a flowlayout inside it's panel if the programmer sets sizes that do not fit for the internal component it needs to know what it may be doing or what it can rationally show or have used.
One of the most real aspects is jframe resizing is not the final problem to fit internal components, it's if the same binaries are used in the user computer at a different resolution. Accounting size generally also requires adding some space on borders of the layout between components.
 
Saloon Keeper
Posts: 23440
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless overridden by an absolute size, sizes on layout-based GUI systems such as Swing are computed from the outside in.

That is, you have a Window (usually). That was allotted a certain amount of screen real estate either in terms of the screen that contains it, a virtual screen space in the case of multiple monitors or whatever. You then set a parent layout manager in that window's client area to contain the user-defined controls. The parent layout manager will sub-divide the client area according to its nature. For example, a grid layout will divide the client space into equal row/column cells unless you provide a weighting factor. Within equal sub-area you would then place a control which could either be a primitive control, a compound control (for GUI systems that allow that), or you would place a sub-layout, in which case the process recurses within the sub-layout, The lowest level of control typically has a preferred minimum size built into it, but that depends on the specific control type. The layout manager then attempts to size and place its children in what it considers an optimal way based on what type of layout it is, subject to hints like "gravity" and "weight".

That's a lot more complex than the old-fashioned system of absolute co-ordinates and sizes, but it's necessary because display sizes and resolutions can vary so much between devices.

Actually, having an empty dropdown list control dimension itself as 0x0 would not be surprising. It's actually not recommended practice to have a truly empty drop-down list. Most commonly you'd put a dummy entry in to visually indicate to the user that the list was empty, and in fact, depending on your preferences and requiremends, that entry might simply read "-- empty --" or "-- Add an item --". In the same manner, if you can have a list that assumes no pre-selected value, the first entry on a list might be "-- Select --".

Also, an empty selection list control would generally be disabled, to re-inforce the idea that there was nothing there to select.

I would like to distinguish a selection list from a dropdown combox box control. A selection list is a primitive control with only one operating mode, A dropdown combo box is actually 2 separate controls operating as an aggregate. One is a texbox (input text) conrol, the other is a dropdown list. A third control (a graphic button) may also exist to toggle the display of the list control. For a combo box the rendering of an empty selection list may vary. Some GUIs (such as HTML) don't even have a native combo box control.
 
Saloon Keeper
Posts: 12825
279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve, you don't have to set any sizes. You just need to learn how to use the correct layout managers.

I quickly hacked something together. Is this close to what you want?
School-Class-Editor.png
[Thumbnail for School-Class-Editor.png]
 
Are you here to take over the surface world? Because this tiny ad will stop you!
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic