Related Topics

JAVA Programing
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftComponent, rightComponent);
splitPane.setDividerLocation(200);
In this example, we create a horizontal split pane with two components: leftComponent
and rightComponent
. We set the initial divider location to 200 pixels from the left or top of the split pane. The JSplitPane.HORIZONTAL_SPLIT
constant specifies the orientation of the split pane.
You can also customize the appearance and behavior of the JSplitPane
by setting various properties such as the divider size, border, and layout orientation.
// Using a JSplitPane to display two components side by side
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftComponent, rightComponent);
// Using a JTabbedPane to display multiple components in separate tabs
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Tab 1", component1);
tabbedPane.addTab("Tab 2", component2);
tabbedPane.addTab("Tab 3", component3);
In this example, the JSplitPane
displays two components (leftComponent
and rightComponent
) side by side. The JTabbedPane
displays three components (component1
, component2
, and component3
) in separate tabs.
In summary, the main difference between JSplitPane
and JTabbedPane
is in the way they display components: JSplitPane
displays two components side-by-side or on top of each other with a divider, while JTabbedPane
displays multiple components in separate tabs.
JTabbedPane tabbedPane = new JTabbedPane();
// Create a panel for the first tab
JPanel panel1 = new JPanel();
panel1.add(new JLabel("This is the first tab"));
// Add the first tab to the tabbed pane
tabbedPane.addTab("Tab 1", panel1);
// Create a panel for the second tab
JPanel panel2 = new JPanel();
panel2.add(new JLabel("This is the second tab"));
// Add the second tab to the tabbed pane
tabbedPane.addTab("Tab 2", panel2);
// Add the tabbed pane to a container and display it
JFrame frame = new JFrame("Tabbed Pane Example");
frame.add(tabbedPane);
frame.pack();
frame.setVisible(true);
In this example, we create a JTabbedPane
and add two panels to it as tabs. We then add the JTabbedPane
to a JFrame
and display it on the screen. When the user clicks on the tabs, the corresponding panels are displayed, allowing the user to switch between the two views.
import javax.swing.*;
public class ProgressBarExample extends JFrame {
private JProgressBar progressBar;
public ProgressBarExample() {
progressBar = new JProgressBar(0, 100); // set the minimum and maximum values for the progress bar
progressBar.setValue(0); // set the initial value
// add the progress bar to the frame
getContentPane().add(progressBar);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 100);
setVisible(true);
// update the progress bar's value periodically
for (int i = 0; i <= 100; i++) {
final int progress = i;
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
progressBar.setValue(progress);
}
});
Thread.sleep(100); // simulate some work being done
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ProgressBarExample();
}
}
In this example, we create an instance of JProgressBar
with a minimum value of 0 and a maximum value of 100. We set the initial value of the progress bar to 0, and add it to the content pane of the JFrame
. We then set the JFrame
to be visible, and update the value of the progress bar periodically using a loop.
Note that the progress bar is updated on the event dispatch thread using SwingUtilities.invokeLater()
, which ensures that the update is done on the correct thread. In addition, we use Thread.sleep()
to simulate some work being done in between updates, since the progress bar is often used to show the progress of long-running tasks.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.