Javaμ Swingμ μ΄μ©ν GUI νλ‘κ·Έλ¨μ μ μνκΈ° μν΄ κ³΅λΆνλ repoμ
λλ€.
This is a repo to study to make a GUI program using Java's Swing.
Please check the code for details.
I write this README.md in English to practice and improve my writing skills in English. If there's any problem with expression or grammar, let me know. Thank you :D
Youtube url : https://www.youtube.com/watch?v=aIdIXsi1qTU
Youtube url : https://www.youtube.com/watch?v=aIdIXsi1qTU
π FlowLayout
Layout Manager = Defines the natural layout for components within a container
μΌμͺ½μμ μ€λ₯Έμͺ½μΌλ‘ λ°°μΉλλ©° μ€λ₯Έμͺ½μ λ μ΄μ 곡κ°μ΄ μμΌλ©΄ λ€μ μ€λ‘ μλ λ°°μΉλλ κ² νΉμ§μ
λλ€. μΌλ°μ μΌλ‘ νλ‘μ° λ μ΄μμμ ν¨λμ λ²νΌμ λ°°μ΄νλ λ° μ¬μ©λλ©°, κΈ°λ³Έμ μΌλ‘ κ°μ΄λ° μ λ ¬μ
λλ€.
They are laid out from left to right and automatically move to the next line when there is no more space on the right.
A flow layout is generally used to arrange buttons in a panel, center aligned by default.
π GridLayout
Layout Manager = Defines the natural layout for components within a container
Each component takes all the available space within its cell, and each cell is the same size
π JLayeredPane
π JOptionPane
JOptionPane = pop up a standard dialog box that prompts users for a value or informs them of something
π checkBox
Set the Icon of the checkbox.
If checkbox is selected, the icon changes to the other. You can do so by using the method setSelectedSIcon()
.
π radio buttons
We can limit the choice selection to only one item by putting them within the same grouping.
To do so, use ButtonGroup
and add the items to it.
π combo boxes
You should use the wrapper class
if you need to store a permitted type.
π sliders
To change the text of label according to the sliders's value, implements the ChangeListener
and overrides the method statechanged(ChangeEvent e)
.
And you should add .addChangeListener
to item that you want to adjust.
π sliders
JSlider = GUI component that lets user enter a value by using an adjustable sliding knob on a track.
To change the text of label according to the sliders's value, implements the ChangeListener
and overrides the method statechanged(ChangeEvent e)
.
And you should add .addChangeListener
to item that you want to adjust.
π progress bar
If you want to simulate the progress bar increasing over time, use Thread.sleep
in try-catch.
Youtube url : [https://www.youtube.com/watch?v=aIdIXsi1qTU](https://www.youtube.com/watch?v=Kmgo00avvEw)
JFrame jFrame;
JScrollPane jScrollPane;
JTable jTable;
String[] col; // JTable col
Object[][] data; // data array to input to the table
You should read file, process the data, and put them into Object[][] data
Array.
We'll define this work with the method getData()
Get data using the method getData()
.
In Main Constructor, create a JFrame, JTable, JScrollaPane. You should create jScrollPane like this. jScrollPane = new JScrollPane(jTable);
because we want to add scroll to the dable.
Next, Add jScrollPane to jFrame, and add properties of jFrame as below. Note the use of pack();
Main() {
jFrame = new JFrame("JTable Demo");
col = new String[] {"Accounts", "Amount"};
data = getData();
jTable = new JTable(data, col);
jScrollPane = new JScrollPane(jTable);
jFrame.add(jScrollPane);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
Specify the path to the file and read it line by line using BufferReader and FileReader.
First, read line by line to the end of the file using a while statement and add it to the Arraylist.
After that, we use split
to count how many columns it has. Create an Object[][] with the size obtained above.
Looping through the for statement, each line is separated with split
, and put them.
Now eturn the data.
Object[][] getData () {
try {
String path = FileSystemView.getFileSystemView().getDefaultDirectory().getPath();
BufferedReader br = new BufferedReader(new FileReader(path + "/bills.csv"));
ArrayList<String> list = new ArrayList<>();
String str = "";
while ((str = br.readLine()) != null) {
list.add(str);
System.out.println(str);
}
int n = list.get(0).split(",").length; // how many clumns
Object[][] data = new Object[list.size()][n];
for (int i = 0; i < list.size(); i++) {
data[i] = list.get(i).split(",");
}
br.close(); // close file
return data;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}