Question

m trying to add a Jslider and a Jlabel to PAGE_END beside each other, i can add each of them on their on but adding .add("components name".PAGE_END) to both it on lets one exist there?

so basicly i want to create my slider and a jlabel beside it to the right, could someone please help in anyway, thanks.

package assignment;

//import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MyControlPanel extends javax.swing.JPanel {

JSlider slider;
JLabel sliderLabel;
JLabel blank;

public MyControlPanel() {


slider = new JSlider();
slider.setValue(50);
slider.addChangeListener(new MyChangeAction());
slider.setMajorTickSpacing(10); 
slider.setPaintLabels(true); 
slider.setPaintTicks(true); 
slider.setBounds(300, 50, 100, 50);
sliderLabel = new JLabel("50");
blank = new JLabel("     ");

JTextField boundary_length = new JTextField("Boundary Length");
JTextField area = new JTextField("Area");

setLayout(new BorderLayout());


this.add(slider, BorderLayout.PAGE_END);
this.add(sliderLabel, BorderLayout.LINE_END);
this.add(area);
this.add(boundary_length);
this.add(blank, BorderLayout.LINE_START);
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>                        
// Variables declaration - do not modify                     
// End of variables declaration                   

public class MyChangeAction implements ChangeListener{

//complete code here

public void stateChanged(ChangeEvent ce) 
{ 
int value = slider.getValue(); 
String str = Integer.toString(value); 
sliderLabel.setText(str); 


}

} // end class
Was it helpful?

Solution

Instead of using BorderLayout to place both the slider and the label, create a panel containing both components, then use BorderLayout to place the panel.

Replace

this.add(slider, BorderLayout.PAGE_END);
this.add(sliderLabel, BorderLayout.LINE_END);

With

JPanel sliderPanel = new JPanel();
sliderPanel.setLayout( new FlowLayout(FlowLayout.TRAILING));
sliderPanel.add(slider);
sliderPanel.add(sliderLabel);
this.add(sliderPanel, BorderLayout.PAGE_END);

This is based on code I used to place an OK and Cancel button at the bottom of a dialog. This might not compile - but you get the idea. Play around with the arguments to FlowLayout and change the order of adding to the slider panel until you get the look you want.

BTW - since you are doing the layout yourself - not using a GUI builder - you might as well get rid of the initComponents method and the surrounding comments. I'm guessing that you are using Netbeans and created a panel that was initially configured to use GroupLayout, and Netbeans injected that code. Now it's just getting in the way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top