I'm trying to create a GUI that has a JXTaskPane
, within which an HTML editor pane is showing some lengthy text, then below that are two sliders. One should have the full window width divided 50/50 between the label and the slider, the next slider should have 1/3rd of the width for the label and 2/3 for the slider.
In order for this example to run, you need SwingX, download the jar here.
There are several problems with the code:
- On the first startup, the elements are not drawn correctly. The window needs to be resized, or the task pane closed and reopened. Can anyone recommend a way to fix this?
- Even though the
GridBagConstraint
fill parameter is set to use the full horizontal width, the components are only using up about half of the window width and are drawn in the center. How can I make them use the full width and height of theJXTaskPane
? - Even though
slider2
has itsgridwidth
property set to 2 (the label has it set to 1) it in fact is drawn narrower than its label! Why does it do that? I tried playing with theweightx
parameter, but that just changes the appearance randomly to a little wider or a little narrower, but in what seems to be a random and rather unpredictable fashion. How do I make this label 1/3rd the width of its slider?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.jdesktop.swingx.JXTaskPane;
class test {
public static void main(String[] arg) {
JFrame mainWindow = new JFrame();
JXTaskPane taskPane = new JXTaskPane();
JSlider slider1 = new JSlider(0,100,50);
JSlider slider2 = new JSlider(0,100,50);
taskPane.setCollapsed(false);
// slider 1 with its label in a simple GridLayout
JPanel pnlSlider1 = new JPanel();
pnlSlider1.setLayout(new GridLayout(1,1)); // 1 row, 1 column
pnlSlider1.add(new JLabel("Description for slider1"));
pnlSlider1.add(slider1);
// slider 2 with its label is in a GridBagLayout, the label should be 1/3 of the width of the slider
JPanel pnlSlider2 = new JPanel();
pnlSlider2.setLayout(new GridBagLayout());
GridBagConstraints c2 = new GridBagConstraints();
c2.fill = GridBagConstraints.HORIZONTAL;
c2.gridx = 0;
c2.gridy = 0;
c2.gridwidth = 1;
c2.weighty = 1;
c2.weightx = 1;
pnlSlider2.add(new JLabel("Description for slider2"), c2);
c2.gridx = 1;
c2.gridwidth = 2;
c2.weightx = 1;
pnlSlider2.add(slider2, c2);
// label should now be to the left of slider
String content = "<html>Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content ";
content += "Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content";
content += "Some rather long winded HTML content Some rather long winded HTML content Some rather long winded HTML content </html>";
JEditorPane ep = new JEditorPane("text/html", content);
// this is the main window panel
//JPanel panel = new JPanel();
taskPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
taskPane.add(ep, c);
c.gridy = 1;
taskPane.add(pnlSlider1, c);
c.gridy = 2;
c.insets = new Insets(10,0,0,0);
taskPane.add(pnlSlider2, c);
// tie it all together and display the window
mainWindow.setPreferredSize(new Dimension(600, 600));
mainWindow.setLocation(100, 100);
mainWindow.getContentPane().add(taskPane);
mainWindow.pack();
mainWindow.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
mainWindow.setVisible(true);
}
}
BorderLayout
is easier to design when compared toGridBagLayout
. Hope this is what you've asked for.I've used Netbeans to create the layout and then added it to theJXTaskPane
Output