#Java gui question
1 messages · Page 1 of 1 (latest)
<@&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.
how do i implement the information i want it to show
so you want a scrollable text window?
yes !
using javafx or swing?
you want to show all book texts at the same time?
well the scrollable window i want it to have all the books, so the user can just look at all of them
ah its not the texts its displaying but instead the name of the books?
i Think so yea
because theres like an infinite amount of books that could be added so
idk how to display all of it
I think a scrollable text window is not a good idea for this though
but instead a scrollable list
Ok maybe that
Idk my teacher showed a little snippet so Yeah i dont really know what i am looking for
in that case you want a JScrollPane which holds a JList
Can you help a little with implementing it
your books have a dedicated class?
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
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
why using io for reading files? why not using nio, which is used nowadays?
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
ok, tell him he should look past Java 8 pls
Java 21 exist
there is a lot of new stuff
LOL okay
lmao
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
um because i have the array that i used in the method like
its just up there
yeah thats most likely static abuse
what does that mean
you shouldnt use static for everything
thats bad
especially in an OOP world
also you dont need to specify the second generic in java
why is that
List<String> list = new ArrayList<>();
oh i see
ok i cant say i will rewrite everything because its due tmrw but whats the difference
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
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
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();
}
}
Ok i dont really know what final is, havent learned map either or path.of
i might be missing out on alot
those are just the modern ways of doing this
Oh i see
those include the concepts of:
- classes
- access modifier
- final
niopackage- Streams
- lambdas
if interested
also you should check your class names
they should follow convention
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
Oh ok yeah thanks
anyways lets get back to your task, you still got any questions?
No sir thank u
Hello i am requesting assistance about something else related to my assignment
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 😓
might want to share the whole codebase
so I can tell you what you would do differently
in favor of OOP
@pine tide
so I can tell you what you would do differently
in favor of OOP
Ok
do i send u the files to it all or
you got a github repo?
otherwise just send them here
our bot will take care of it
you shouldnt use zip files for sharing
you would call Books.java -> Book.java instead
because an instance represents one book
what is the prefix frm?
frame i think
like frmBook.java
Ok
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;
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
this is the dumbest thing I heard in a long while ngl 
for the marks 
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
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..
myFrame.setVisible((true));
what is the use of the extra parenthesis? remove them
copypaste from his code i dont know 
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?
ratings of what?
then strListRatings is used in the other methods
ratings of a book?
yea
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
reads the books from the file
and keeps a list of the author,bookname
Umm idk this was just easier to do naturally
i use it in addBook
this is bad design
create a dedicated class for managing your books
like Library or anything
Oh ok
it will improve your projects structure by a lot
Okay