Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
How to add two buttons in each row?
Prakher Singhal
Greenhorn
Posts: 2
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am trying to build one GUI and want to add two buttons in each row of a panel.
I have added my source code. This
java
code will add buttons in Y-AXIS (one button in each row only).
Can somebuddy help me in it?
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class TextArea1 implements ActionListener { JTextArea text; JButton button_1 = new JButton("Just Click it"); JButton button_2 = new JButton("Clear"); JButton button_3 = new JButton("Temp_1"); JButton button_4 = new JButton("Temp_2"); JButton button_5 = new JButton("Temp_3"); JButton button_6 = new JButton("Temp_4"); JButton button_7 = new JButton("Temp_5"); JButton button_8 = new JButton("Temp_6"); public static void main (String [] args) { TextArea1 gui = new TextArea1(); gui.go(); } public void go() { JFrame frame = new JFrame(); JPanel panel = new JPanel(); JPanel panel_1 = new JPanel(); panel_1.setBackground(Color.darkGray); panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS)); button_1.addActionListener(this); button_2.addActionListener(this); button_3.addActionListener(this); button_4.addActionListener(this); button_5.addActionListener(this); button_6.addActionListener(this); button_7.addActionListener(this); button_8.addActionListener(this); text = new JTextArea(30,40); text.setLineWrap(true); JScrollPane scroller = new JScrollPane(text); scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); panel.add(scroller); frame.getContentPane().add(BorderLayout.WEST, panel); frame.getContentPane().add(BorderLayout.EAST, panel_1); panel_1.add(button_1); panel_1.add(button_2); panel_1.add(button_3); panel_1.add(button_4); panel_1.add(button_5); panel_1.add(button_6); panel_1.add(button_7); panel_1.add(button_8); //frame.getContentPane().add(BorderLayout.SOUTH, button); //frame.getContentPane().add(BorderLayout.NORTH, button1); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(700,500); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { if(event.getSource() == button_1){ //text.append("button clicked \n"); try { final JFileChooser fc = new JFileChooser(); //In response to a button click: int returnVal = fc.showOpenDialog(button_1); String fname = fc.getSelectedFile().getAbsolutePath(); File myFile = new File (fname); FileReader fileReader = new FileReader(myFile); BufferedReader reader = new BufferedReader(fileReader); String line = null; while ((line = reader.readLine()) != null) { text.append(line + "\n"); } reader.close(); }catch(Exception ex) { ex.printStackTrace(); } }else{ text.setText(""); } } }
Maneesh Godbole
Bartender
Posts: 11497
19
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Try out the
GridLayout
[
How to ask questions
] [
Donate a pint, save a life!
] [
Onff-turn it on!
]
Prakher Singhal
Greenhorn
Posts: 2
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thankyou Maneesh,
I just tried GridLayout and its working fine but the size of the buttons are too big. How can I control the size of the buttons?
Michael Dunn
Ranch Hand
Posts: 4632
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
> How can I control the size of the buttons?
if you added a button to a JFrame (size 800,600),
the button would be (almost) the same size.
if you don't know how to control the size of that button,
then you need to read up on the layoutManager tutorials.
spoon-feeding you the answer to your question will not help you.
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
implementing ActionListener and ItemListener
JList error
Trials and tribulations, trys and catches
selectAll() method on a JTextArea
JTextArea page 415 of Head First Java
More...