Reputation: 73
I have just started learning swing in Java. I'm having problems with JSplitPane in swing library. I have made an app that has four classes Frame(), TablePanel(), TextPanel(), FormPanel()
Frame is the main class which extends JFrame.The following image describes these classes more clearly.
now my problem is that I want the spliter to be between TablePanel
class and TextPanel
class. I was wondering if there was a way that in TablePanel
I instantiate TextPanel
and set spliter between Table Panel and TextPanel
something like :
The following is just a pseudocode;
TablePanel() {
TextPanel textPanel = new TextPanel();
setLayout(new BorderLayout());
JSplitPane spliter = new JSplitPane(vertical, textPanel, this);
add(spliter);
}
Please if I'm wrong. Correct my mistake by suggesting a better way.
An helpful answer will be appreciated. Thank in advance!
Upvotes: 3
Views: 1208
Reputation: 168825
Use JSplitPane.setDividerLocation(double)
, but note the Java Docs:
Sets the divider location as a percentage of the
JSplitPane
's size.This method is implemented in terms of
setDividerLocation(int)
. This method immediately changes the size of the split pane based on its current size. If the split pane is not correctly realized and on screen, this method will have no effect (new divider location will become (current size * proportionalLocation) which is 0).
So it must be called after the GUI is visible.
To achieve that during the process of constructing a GUI, I'd use a single shot Swing Timer
that delays setting the divider location for around half a second, then start the timer at the end of creating the components.
Here is a simple implementation as an MCVE.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class SplitPaneDivider {
private JComponent ui = null;
SplitPaneDivider() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
final JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
new ColoredPanel(Color.GREEN),
new ColoredPanel(Color.YELLOW));
ui.add(sp);
ActionListener dividerListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sp.setDividerLocation(.7d);
}
};
Timer timer = new Timer(500, dividerListener);
timer.setRepeats(false);
timer.start();
}
class ColoredPanel extends JPanel {
ColoredPanel(Color color) {
setBackground(color);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 100);
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
SplitPaneDivider o = new SplitPaneDivider();
JFrame f = new JFrame("Split Pane Divider");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
Upvotes: 3
Reputation: 328
I suggest you study the following to properly understand the workings of JSplitPane, and to get some demo examples:
Oracle documentation on the use of JSplitPane
Upvotes: 0