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;
}
}