ShoGun
asked on
help in Layout
hi
I have 2 lists and 2 buttons
---------------------- -----------------------
Add
-------->
Del
<--------
---------------------- ------------------------
How do I lay them out on screen?
I tried GridLayout overall for Panle and putting the 2 buttons in a sub Panel with BorderLayout.north and south but is not giving nice look
any better suggestion ?
thanks you!
I have 2 lists and 2 buttons
---------------------- -----------------------
Add
-------->
Del
<--------
---------------------- ------------------------
How do I lay them out on screen?
I tried GridLayout overall for Panle and putting the 2 buttons in a sub Panel with BorderLayout.north and south but is not giving nice look
any better suggestion ?
thanks you!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
////////////////////////// ////////
//THE SwingStart PROGRAM.//
////////////////////////// ////////
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.* ;
import com.sun.java.swing.border. *;
public class SwingStart extends Frame {
public static int WIDTH = 450;
public static int HEIGHT = 450;
public static String TITLE = "SwingStart";
// Swing components
JTabbedPane tabbedPane = new JTabbedPane();
JPanel buttonPanel = new JPanel();
JPanel barPanel = new JPanel();
JPanel listPanel = new JPanel();
JPanel tablePanel = new JPanel();
JPanel[] panels = {buttonPanel,barPanel,list Panel,tabl ePanel};
Icon worldIcon = new ImageIcon("world.gif");
Icon printerIcon = new ImageIcon("printer.gif");
Icon leaf1Icon = new ImageIcon("leaf1.gif");
Icon leaf2Icon = new ImageIcon("leaf2.gif");
Icon leaf3Icon = new ImageIcon("leaf3.gif");
Icon[] leaves = {leaf1Icon, leaf2Icon, leaf3Icon};
JButton printerButton = new JButton("Print",printerIco n);
JToggleButton worldButton = new JToggleButton("Connect",wo rldIcon,tr ue);
JList leafList = new JList(leaves);
JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 60);
JProgressBar progressBar = new JProgressBar();
String[] columns = {"Product ID","Description","Price"} ;
Object[][] cells = {columns,{"zvga-1234","Vid eo Card","$50"},
{"56m-11","56K Modem","$315"},
{"dc-10","Net Card","$499"}};
JTable table = new JTable(cells,columns);
{
super(TITLE);
addWindowListener(new WindowHandler());
buildGUI();
setSize(WIDTH,HEIGHT);
setBackground(Color.darkGr ay);
show();
}
void buildGUI() {
// Set up tabbed pane
String[] tabs = {"Buttons","Bars","Lists", "Table"};
String[] tabTips = {"A Button and a Toggle Button",
"A Slider and a Progress Bar",
"An Icon List",
"A Cost Table"};
for(int i=0;i<tabs.length;++i) {
panels[i].setBackground(Co lor.lightG ray);
panels[i].setBorder(new TitledBorder(tabTips[i]));
tabbedPane.addTab(tabs[i], null,panel s[i],tabTi ps[i]);
}
addComponentsToTabs();
add("Center",tabbedPane);
}
void addComponentsToTabs() {
setupButtonPanel();
setupBarPanel();
setupListPanel();
setupTablePanel();
}
void setupButtonPanel() {
printerButton.setBackgroun d(Color.wh ite);
worldButton.setBackground( Color.whit e);
buttonPanel.add(printerBut ton);
buttonPanel.add(worldButto n);
}
void setupBarPanel() {
slider.setMajorTickSpacing (10);
slider.setMinorTickSpacing (5);
slider.setPaintTicks(true) ;
slider.addChangeListener(n ew SliderHandler());
progressBar.setOrientation (JProgress Bar.HORIZO NTAL);
progressBar.setMinimum(0);
progressBar.setMaximum(100 );
progressBar.setValue(60);
progressBar.setBorderPaint ed(true);
barPanel.add(new JLabel("Slider"));
barPanel.add(slider);
barPanel.add(new JLabel("Progress Bar"));
barPanel.add(progressBar);
}
void setupListPanel() {
leafList.setFixedCellHeigh t(123);
listPanel.add(leafList);
}
void setupTablePanel() {
tablePanel.add(table);
}
public static void main(String[] args) {
SwingStart app = new SwingStart();
}
public class WindowHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public class SliderHandler implements ChangeListener {
public void stateChanged(ChangeEvent e) {
progressBar.setValue(slide r.getValue ());
}
}
}
//THE SwingStart PROGRAM.//
//////////////////////////
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*
import com.sun.java.swing.border.
public class SwingStart extends Frame {
public static int WIDTH = 450;
public static int HEIGHT = 450;
public static String TITLE = "SwingStart";
// Swing components
JTabbedPane tabbedPane = new JTabbedPane();
JPanel buttonPanel = new JPanel();
JPanel barPanel = new JPanel();
JPanel listPanel = new JPanel();
JPanel tablePanel = new JPanel();
JPanel[] panels = {buttonPanel,barPanel,list
Icon worldIcon = new ImageIcon("world.gif");
Icon printerIcon = new ImageIcon("printer.gif");
Icon leaf1Icon = new ImageIcon("leaf1.gif");
Icon leaf2Icon = new ImageIcon("leaf2.gif");
Icon leaf3Icon = new ImageIcon("leaf3.gif");
Icon[] leaves = {leaf1Icon, leaf2Icon, leaf3Icon};
JButton printerButton = new JButton("Print",printerIco
JToggleButton worldButton = new JToggleButton("Connect",wo
JList leafList = new JList(leaves);
JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 60);
JProgressBar progressBar = new JProgressBar();
String[] columns = {"Product ID","Description","Price"}
Object[][] cells = {columns,{"zvga-1234","Vid
{"56m-11","56K Modem","$315"},
{"dc-10","Net Card","$499"}};
JTable table = new JTable(cells,columns);
{
super(TITLE);
addWindowListener(new WindowHandler());
buildGUI();
setSize(WIDTH,HEIGHT);
setBackground(Color.darkGr
show();
}
void buildGUI() {
// Set up tabbed pane
String[] tabs = {"Buttons","Bars","Lists",
String[] tabTips = {"A Button and a Toggle Button",
"A Slider and a Progress Bar",
"An Icon List",
"A Cost Table"};
for(int i=0;i<tabs.length;++i) {
panels[i].setBackground(Co
panels[i].setBorder(new TitledBorder(tabTips[i]));
tabbedPane.addTab(tabs[i],
}
addComponentsToTabs();
add("Center",tabbedPane);
}
void addComponentsToTabs() {
setupButtonPanel();
setupBarPanel();
setupListPanel();
setupTablePanel();
}
void setupButtonPanel() {
printerButton.setBackgroun
worldButton.setBackground(
buttonPanel.add(printerBut
buttonPanel.add(worldButto
}
void setupBarPanel() {
slider.setMajorTickSpacing
slider.setMinorTickSpacing
slider.setPaintTicks(true)
slider.addChangeListener(n
progressBar.setOrientation
progressBar.setMinimum(0);
progressBar.setMaximum(100
progressBar.setValue(60);
progressBar.setBorderPaint
barPanel.add(new JLabel("Slider"));
barPanel.add(slider);
barPanel.add(new JLabel("Progress Bar"));
barPanel.add(progressBar);
}
void setupListPanel() {
leafList.setFixedCellHeigh
listPanel.add(leafList);
}
void setupTablePanel() {
tablePanel.add(table);
}
public static void main(String[] args) {
SwingStart app = new SwingStart();
}
public class WindowHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public class SliderHandler implements ChangeListener {
public void stateChanged(ChangeEvent e) {
progressBar.setValue(slide
}
}
}
ASKER
Adjusted points from 95 to 155
ASKER
Jerson,
I will go with gridBagLayout and thnaks for that!!! it is very teachable ,I will try not to ask you question but can you help me if i need?
Thanks you!
I will go with gridBagLayout and thnaks for that!!! it is very teachable ,I will try not to ask you question but can you help me if i need?
Thanks you!
Sure... no problem... also check this one
http://pandonia.canberra.edu.au/java/xadvisor/gridbag/gridbag.html
best regards...
Jerson
http://pandonia.canberra.edu.au/java/xadvisor/gridbag/gridbag.html
best regards...
Jerson
Sure... no problem... also check this one
http://pandonia.canberra.edu.au/java/xadvisor/gridbag/gridbag.html
best regards...
Jerson
http://pandonia.canberra.edu.au/java/xadvisor/gridbag/gridbag.html
best regards...
Jerson
ASKER
thanks i finished that !wow that link is superb
yeah.. I read it while I was preparing for the SCJP exam. :)
Jerson
Jerson
ASKER
I printed and raed the whole article and did another layout in guess what ? 25 mins while it took me 2 days to get the first one working nicely.
but did you know that there is a bug for GridBagLayout and it's removed only in latest 2 versions f JDK?
Thanks you!
but did you know that there is a bug for GridBagLayout and it's removed only in latest 2 versions f JDK?
Thanks you!
:)
ASKER
Panel buttons = new Panel(new BorderLayout());
buttons.add(add,BorderLayo
buttons.add(del,BorderLayo
Panel top = new Panel(new GridLayout(1,3)); top.add(List1);
top.add(buttons);
top.add(List2);
add(top);
.....