#Java gui question

1 messages · Page 1 of 1 (latest)

pine tide
#

I have an assignment thats about a book reccomendation system but my question is just is there a way to display stuff through like a popup scroll type thing java gui?

paper bobcatBOT
#

<@&987246717831381062> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

Yes, both JavaFX and Swing provide components that can be used to display content in a popup scrollable window.

In JavaFX, you can use the ScrollPane class to create a scrollable area. You can add any content you want to the ScrollPane, such as labels, buttons, or even other containers like VBox or GridPane. Here's an example:

#
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class PopupScrollExample extends Application {
    @Override
    public void start(Stage primaryStage) {
        ScrollPane scrollPane = new ScrollPane();
        VBox content = new VBox();
        
        // Add some labels to the VBox
        for (int i = 1; i <= 20; i++) {
            Label label = new Label("Label " + i);
            content.getChildren().add(label);
        }
        
        scrollPane.setContent(content);
        
        Scene scene = new Scene(scrollPane, 300, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
#

In Swing, you can use the JScrollPane class along with other components like JPanel, JLabel, etc. Here's an example:

#
import javax.swing.*;
import java.awt.*;

public class PopupScrollExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Popup Scroll Example");
        
        JPanel content = new JPanel();
        
        // Add some labels to the panel
        for (int i = 1; i <= 20; i++) {
            JLabel label = new JLabel("Label " + i);
            content.add(label);
        }
        
        JScrollPane scrollPane = new JScrollPane(content);
        
        frame.getContentPane().add(scrollPane);
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
#

Both examples will create a window with a scrollable area that displays a series of labels. You can customize the content and appearance according to your requirements.

pine tide
#

i have this

#

to show all my books

#

but it runs out of space so

pine tide
finite spindle
#

so you want a scrollable text window?

pine tide
#

yes !

finite spindle
#

using javafx or swing?

pine tide
#

to show all the books i have in my arraylist i guess

#

um

#

swing

finite spindle
pine tide
#

well the scrollable window i want it to have all the books, so the user can just look at all of them

finite spindle
#

ah its not the texts its displaying but instead the name of the books?

pine tide
#

i Think so yea

#

because theres like an infinite amount of books that could be added so

#

idk how to display all of it

finite spindle
#

I think a scrollable text window is not a good idea for this though

#

but instead a scrollable list

pine tide
#

Ok maybe that

#

Idk my teacher showed a little snippet so Yeah i dont really know what i am looking for

finite spindle
#

in that case you want a JScrollPane which holds a JList

pine tide
#

Can you help a little with implementing it

finite spindle
#

your books have a dedicated class?

pine tide
#

i do have a book class but i cant say i found much use for it

#

i have all my books in an arraylist

#

the books are all in a file that was provided, so i read it and put the information in an arraylist

#

using this

finite spindle
#
List<Book> books = new ArrayList<>();
// ...

DefaultListModel<Book> bookModel = new DefaultListModel<>();
bookModel.addAll(books);
JList<Book> bookList = new JList<>(bookModel);

JScrollPane pane = new JScrollPane(bookList);
// add to parent view
finite spindle
# pine tide

why using io for reading files? why not using nio, which is used nowadays?

pine tide
#

idk it's what my teacher uses

#

i have no clue what io or nio means

finite spindle
#

io -> input output, its the package that provides FileReader, BufferedReader etc

#

nio -> new io, its a new package which provides easier and better ways of reading files and stuff

finite spindle
#

Java 21 exist

#

there is a lot of new stuff

pine tide
#

LOL okay

finite spindle
#

lmao

finite spindle
# pine tide

also this looks like static abuse to me, why is the method not returning an arraylist? but instead using strLineBooks? which looks like being a static field

#

also handle the IOException

#

and use try with resource

#

instead of manually closing

pine tide
#

its just up there

finite spindle
#

yeah thats most likely static abuse

pine tide
#

what does that mean

finite spindle
#

you shouldnt use static for everything

#

thats bad

#

especially in an OOP world

#

also you dont need to specify the second generic in java

pine tide
#

why is that

finite spindle
#
List<String> list = new ArrayList<>();
pine tide
#

oh i see

pine tide
finite spindle
#

as soon as your codebase gets bigger and has multiple classes (using OOP practices) will run into desing issues

#

I am not sure how deep you are into the topic OOP

#

but you shouldnt use static like this

#

abusing static for ease of access instead of creating proper flow and data management

pine tide
#

so if i were to redo again

#

how would u read the files

#

and store them in a data structure

#

this is just what the file contains

finite spindle
#
public static void main(String[] args) {
    List<String> lines = readLines("...");
    // continue using them in your project, if a different class needs the list pass it through with constructor
}

private static List<String> readLines(String path) {
    // implementation
}
#

also in this example you would simply use Files.lines()

#

here your List is a variable inside your main method

#

which makes it a local variable instead of public static

#

and then you can continue like this:

// using records here is better
public class Book {
    // fields
    private final String name;
    private final String author;
    // ...

    public Book(String line) {
        // parsing the input line into the different fields
    }
}

and then:

public class Main {
    
    public static void main(String[] args) {
        List<Book> books = Files.lines(Path.of("...")).map(Book::new).toList();
    }
}
pine tide
#

Ok i dont really know what final is, havent learned map either or path.of

#

i might be missing out on alot

finite spindle
#

those are just the modern ways of doing this

pine tide
#

Oh i see

finite spindle
#

those include the concepts of:

  • classes
  • access modifier
  • final
  • nio package
  • Streams
  • lambdas
#

if interested

#

also you should check your class names

#

they should follow convention

paper bobcatBOT
#

The naming convention in Java is as follows:

Classes:
PascalCase. Example: GoldMiner, FoodDestroyer, DispenserBuilder

Methods / Fields / Variables:
camelCase. Example: foodAmount, integerValue, goodBoyAsADog

Packages:
All lowercase. Preferably the domain name backwards. Example: "google.com" ==> com.google, "ialistannen.me" ==> me.ialistannen
Followed by the project name. Example: Project "Builder" ==> com.google.builder

Constants/Enum entries:
UPPER_SNAKE_CASE. Where words are separated by underscores. Example: RED_DOG, I_AM_THE_BEST, CONQUER_WORLD

pine tide
#

Oh ok yeah thanks

finite spindle
#

anyways lets get back to your task, you still got any questions?

pine tide
#

No sir thank u

pine tide
#

So im working on having the user add a rating

#

each button just means a different number

#

but

#

i need to add the rating for a specific member

#

the rating, for the book, and for which user

#

but im not sure how to logged in users, user

#

because where i ask for is it in the login gui

#

and then this is a different gui

#

so i was like yea i probably need to use my member class

#

so in my method where i add a user

#

i decided to instantiate my member class

#

but like how do i give it a unique name?

#

because i was going to use this get method in my member class

#

to access the logged in users, user

#

ok yea idk im a little confused

#

this is the only time i get the users name

#

is there anyway i can access "strPass"

#

ok well i tried this

#

is this static abusing 😓

#

actually i put this into my login

#

and i did this

#

pretty awesome..

#

but static abusing 😓

finite spindle
#

might want to share the whole codebase

#

so I can tell you what you would do differently

#

in favor of OOP

#

@pine tide

pine tide
#

Hello

#

well i kind of figured it out i think

#

it works ... afaik

finite spindle
#

so I can tell you what you would do differently
in favor of OOP

pine tide
#

Ok

pine tide
finite spindle
#

you got a github repo?

#

otherwise just send them here

#

our bot will take care of it

#

you shouldnt use zip files for sharing

pine tide
#

ok i just downloaded it

#

from replit

#

do i download the files seperately

finite spindle
#

just send them

#

it doesnt matter

#

the .java

paper bobcatBOT
finite spindle
#

you would call Books.java -> Book.java instead

#

because an instance represents one book

#

what is the prefix frm?

pine tide
#

frame i think

finite spindle
#

like frmBook.java

pine tide
#

frame

#

its for my guis

#

all frms are guis

finite spindle
#

call it differently

#

like BookView or smth

pine tide
#

Ok

finite spindle
#

also these are way to many comments:

  //Declaration of all widgets goes here
  //declaring the title lable
  JLabel lblTitle;
  //declaring the name label
  JLabel lblISBN;
  //declaring the password label
  JLabel lblBookTitle;
  //declaring the password label
  JLabel lblYear;
  //declaring the password label
  JLabel lblAuthor;
  //declaring text field
  JTextField txtISBN;
  //declaring text field
  JTextField txtBookTitle;
  //declaring text field
  JTextField txtYear;
  //declaring text field
  JTextField txtAuthor;
pine tide
#

oh yea i know i just copypasted some and was going to clean most of it up later

#

but my teacher does insist to comment every line

finite spindle
pine tide
#

for the marks pepe_sad

finite spindle
#

ok lets start in the Main.java

#
  public static void main(String[] args) {
    Map<String, Integer> myMap = new HashMap<String, Integer>();
    frmLogin myFrame = new frmLogin();
    //we also going to say close the frame when the X button ism pressed
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setSize(500,500);//set the frame size
    myFrame.setVisible((true));
    //load books
    loadBooks("books.txt");
    //load ratings
    loadRatings("ratings.txt");  

    
    //create usernames and passwords for the existing users
    // for(int i = 0; i <= strListRatings.size()-1; i += 2){
    //   addUser(strListRatings.get(i),strListRatings.get(i));
    // }

  }
#

what is myMap I think its not used

#

remove it in that case

#

frmLogin should be called LoginView or smth

#

the variable shouldnt be called myFrame

#

instead call it like login or smth which represents this variable

pine tide
#

oh yea mymap was a thing my teacher was trying to teach

#

but i didnt really know how to use it so yea i did not use it..

finite spindle
#
myFrame.setVisible((true));

what is the use of the extra parenthesis? remove them

pine tide
finite spindle
#

you have a lot of methods like addBook, addRating , getUserRatings, addUser which are all static and inside Main.java

#

they shouldnt be there

#

there should get their own class

#

and all should be non static

#

create a class like Library.java or smth

#

also what does strListRatings store?

pine tide
#

loadRatings

#

uses it

#

to store ratings

finite spindle
#

ratings of what?

pine tide
#

then strListRatings is used in the other methods

finite spindle
#

ratings of a book?

pine tide
#

yea

finite spindle
#

then store them in the related Book instance

#

as well as

public static ArrayList<String> strListBooks = new ArrayList<String>();

what does this store?

#

the names of the books?

#

the whole information of a book?

#

why not having a List<Book> instead?

#

also the class Main should be public

pine tide
#

and keeps a list of the author,bookname

pine tide
finite spindle
#

how that?

#

what is the use of Books.java then?

pine tide
#

i use it in addBook

finite spindle
#

this is bad design

#

create a dedicated class for managing your books

#

like Library or anything

pine tide
#

Oh ok

finite spindle
#

it will improve your projects structure by a lot

pine tide
#

Okay