473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with a layout/gui issue

blazedaces
284 100+
Hey, so here's an example of what I want to do:

This "window" if you will, or simple gui, will accept an ArrayList<String> and in turn spit out for every one of them a checkbox and a text field where you can rename it to whatever name you chose (later I'll add something to check and make sure none of the names are the same... but for now it doesn't matter).

I'm not very familiar with GUI in java so I don't know the best way of doing this. I'm not asking for code, but simply some help in the right direction. I've downloading all of java swing's tutorial demos and examples and have looked at just about every component and layout example in there. Still I don't feel like I know what I'm doing and you'll see in my code soon what I mean.

Before I do I want to show you guys what I want the gui to look like in a sense:

Expand|Select|Wrap|Line Numbers
  1. ArrayList<String> temp = new ArrayList<String>(2);
  2.                 temp.add("First Tag\\");
  3.                 temp.add("Second Tag\\");
  4.  
  5. If above is the arraylist I would want the gui to look something like this:
  6.  
  7. Note: ---- represents the border of the window, [] represents a checkbox, text represents non-editable text and "text" represents editable text
  8.  
  9. Well I didn't mention this earlier but the point of the checkboxes is to not include those strings, when I return an arraylist later, but that's not important now.  The gui showing up is coming first...
  10. ----------------------------------------------
  11. -  []First Tag: "First Tag"                  -
  12. -  []Second Tag: "Second Tag"                -
  13. ----------------------------------------------
  14.  
  15. Of course with a title and minimize/maximize/close buttons there too, but they come automatically...
  16.  
Here's what I've got so far with the code, I tried to put stuff into JPanels and then add them to the gui later:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5.  
  6. public class TagChoosingWindow implements ItemListener {
  7.     JTextField[] textFields;
  8.        JCheckBox[] checkBoxes;
  9.        JPanel[] panels;
  10.  
  11.        short[] included;
  12.  
  13.        ArrayList<String> tags;
  14.  
  15.     public TagChoosingWindow(ArrayList<String> al) {
  16.         tags = al;
  17.         int size = tags.size();
  18.  
  19.         included = new short[size];
  20.  
  21.         textFields = new JTextField[size];
  22.            checkBoxes = new JCheckBox[size];
  23.            panels = new JPanel[size];
  24.  
  25.         for (int i = 0; i < size; i++) {
  26.             checkBoxes[i] = new JCheckBox(tags.get(i) + " : ");
  27.             checkBoxes[i].setSelected(true);
  28.             checkBoxes[i].addItemListener(this);
  29.  
  30.             textFields[i] = new JTextField(tags.get(i));
  31.  
  32.             included[i] = 1;
  33.  
  34.             panels[i] = new JPanel(new BoxLayout(panels[i], BoxLayout.LINE_AXIS));
  35.             panels[i].add(checkBoxes[i]);
  36.             panels[i].add(textFields[i]);
  37.         }
  38.  
  39.  
  40.     }
  41.  
  42.     private void buildUI(Container container) {
  43.         container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
  44.  
  45.         for (int i = 0; i < panels.length; i++) {
  46.             container.add(panels[i]);
  47.         }
  48.     }
  49.  
  50.     public void itemStateChanged(ItemEvent e) {
  51.         Object source = e.getItemSelectable();
  52.         int index = -1;
  53.         for (int i = 0; i < checkBoxes.length; i++) {
  54.             if (source == checkBoxes[i]) {
  55.                 index = i;
  56.                 break;
  57.             }
  58.         }
  59.  
  60.         if (e.getStateChange() == ItemEvent.SELECTED) {
  61.             textFields[index].setEnabled(true);
  62.             included[index] = 1;
  63.         } else {
  64.             textFields[index].setEnabled(false);
  65.             included[index] = 0;
  66.         }
  67.     }
  68.  
  69.     private void createAndShowGUI() {
  70.         //Create and set up the window.
  71.         JFrame frame = new JFrame("Choose What Data Tags You Want and Name Them");
  72.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  73.  
  74.         //Set up the content pane.
  75.         TagChoosingWindow tagchoosingwindow = new TagChoosingWindow(tags);
  76.         tagchoosingwindow.buildUI(frame.getContentPane());
  77.  
  78.         //Display the window.
  79.         frame.pack();
  80.         frame.setVisible(true);
  81.     }
  82.  
  83.     public static void main(String args[]) {
  84.         //Schedule a job for the event-dispatching thread:
  85.         //creating and showing this application's GUI.
  86.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  87.             public void run() {
  88.                 ArrayList<String> temp = new ArrayList<String>(2);
  89.                 temp.add("First Tag\\");
  90.                 temp.add("Second Tag\\");
  91.                 TagChoosingWindow tagchoosingwindow = new TagChoosingWindow(temp);
  92.                 tagchoosingwindow.createAndShowGUI(); 
  93.             }
  94.         });
  95.     }
  96. }
  97.  
Oh and thank you for all help, it's much appreciated,

-blazed
Jul 19 '07 #1
6 1922
JosAH
11,448 Expert 8TB
Have a look at the JTable component with its TableModel compadre. The first
column could consists of JCheckBoxes and the second column can be a
JTextField. The TableModel receives the List at construction time so it is able
to set up the row dimension of the JTable (there are just two columns).

Of course you have to build your own TableModel for this (check the API docs).
You can build a 'collect' method in your model that collects all the selected
Strings from the JTable.

kind regards,

Jos
Jul 19 '07 #2
blazedaces
284 100+
Have a look at the JTable component with its TableModel compadre. The first
column could consists of JCheckBoxes and the second column can be a
JTextField. The TableModel receives the List at construction time so it is able
to set up the row dimension of the JTable (there are just two columns).

Of course you have to build your own TableModel for this (check the API docs).
You can build a 'collect' method in your model that collects all the selected
Strings from the JTable.

kind regards,

Jos
A good idea. I was considering a JTable at first, but didn't like the look it gave. What you suggested I think might solve all my problems though because if I had my own built TableModel I'm guessing I could manipulate those lines in between the cells to not appear along with other look-and-feel parts of the table.

Thanks,

-blazed

Well, I took a look at it, that's not exactly what I thought TableModel was... but alright. Maybe there's another way to avoid that ... if not, it doesn't matter I guess...
Jul 19 '07 #3
JosAH
11,448 Expert 8TB
A good idea. I was considering a JTable at first, but didn't like the look it gave. What you suggested I think might solve all my problems though because if I had my own built TableModel I'm guessing I could manipulate those lines in between the cells to not appear along with other look-and-feel parts of the table.

Thanks,

-blazed

Well, I took a look at it, that's not exactly what I thought TableModel was... but alright. Maybe there's another way to avoid that ... if not, it doesn't matter I guess...
You have to read a bit further: there's an AbstractTableModel class that does
the bulk of the work for you; you only have to implement three or four methods:
row and column sizes, get a value and set a value. That's all there is to it. The
AbstractModel class does the rest for you. For most of the 'complicated'
interfaces there's an Abstract class implementation for you (e.g. AbstractList,
AbstractTableModel etc.) so you don't have to (re)implement all the boring
bookkeeping stuff yourself.

kind regards,

Jos
Jul 19 '07 #4
blazedaces
284 100+
You have to read a bit further: there's an AbstractTable model that does the bulk
of the work for you; you only have to implement three or four methods: row and
column sizes, get a value and set a value. That's all there is to it. The Abstract
thing does the rest for you. For most of the 'complicated' interfaces there's an
Abstract implementation for you (e.g. AbstractList, AbstractTableModel etc.)
so you don't have to (re)implement all the boring bookkeeping stuff yourself.

kind regards,

Jos
Well, I did take a look at that, but it doesn't seem to be able to do what I want:

mainpulate the table so there are no borders between the individual cells or specific cells (like a border between the columns, but not the rows or vice versa)

I may be wrong. I will have to investigate further... I do notice in the "How to use tables" tutorial you can change the background, which is sweet. I never like looking into a lightbulb ;)

-blazed
Jul 19 '07 #5
JosAH
11,448 Expert 8TB
Well, I did take a look at that, but it doesn't seem to be able to do what I want:

mainpulate the table so there are no borders between the individual cells or specific cells (like a border between the columns, but not the rows or vice versa)

I may be wrong. I will have to investigate further... I do notice in the "How to use tables" tutorial you can change the background, which is sweet. I never like looking into a lightbulb ;)

-blazed
Swing components don't work that way: the Model takes care of the data.
the visual component (the JTable) takes care of the rendering; Hava a look at the
setInterCellSpacing() method for example.

kind regards,

Jos
Jul 19 '07 #6
blazedaces
284 100+
Swing components don't work that way: the Model takes care of the data.
the visual component (the JTable) takes care of the rendering; Hava a look at the
setInterCellSpacing() method for example.

kind regards,

Jos
I think I found a solution to my problem. You can use the setBackground with setGridColor to set them as the same color and poof... no more grid! hehe!

-blazed
Jul 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Anthony Williams | last post by:
Good morning, I'm currently designing a site, using CSS, and wish to create a variable width two-column layout, with header and footer, and one fixed-width column on the left. Previously, I...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
5
by: Matt Bostock | last post by:
Hi, I'm a bit of a CSS newbie so please accept my apologies if this post is inappropriate. I can't get the 'latest news' title to move across to the right column where it belongs. Here's a...
20
by: Tammy | last post by:
What would be a good alternative to using frames? I need something that will section my webpage into two halves and can change both frames on a single click. Thanks in Advance, Tammy
3
by: Samuel Shulman | last post by:
I am looking for good guidance for positioning controls on the form.document, it is absolute nightmare and I don't know where to begin Thank you, Samuel Shulman
1
by: dlogan | last post by:
Since this is becoming a complete hack-job, I think its about time I asked for some help. I am trying to make a calendar using AJAX to allow for the main calendar to display, then load in the events...
0
by: magicofureyes | last post by:
Hello Guys im a just a new user and i dnt knw much abt Xml i want to upload a new template in Blogger so got some free coding but when i save this code in Blogger template it say '''' Your...
7
Chrisjc
by: Chrisjc | last post by:
Okay so here is the break down. I have 3 pages. Index.php conversionTable.php convertPost.php Now my issue is the following. Everything works just fine however
10
by: hzgt9b | last post by:
Using VS2005 (.NET 2.0), VB.NET, I have a windows forms application. I have a StatusStrip containing a ToolStripStatusLabal and a ToolStripProgressBar. I want to force the status label to behave...
1
by: fauxanadu | last post by:
I have a new document open in print layout. Then I attempt to insert an autoshape into the document. As soon as I click on the shape I want to input, word swaps into web layout and does nothing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.