Re: Help in Trim and putt
To: comp.lang.java.gui
On Oct 9, 2:29 pm, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
RedGrittyBrick wrote:
Sriram wrote:
The code I tried was as follows(PLEASE NOTE since my code is
complicated and big I cant put in this small area,too confusing...so
an example here):
We don't need you whole code but we do need something that compiles and
runs.
String line = {"word 1", "word 2","word 3","word 4"};
You can't assign an array of Strings to a String!
String[] headings = {"word 1", "word 2","word 3","word 4"};
At this point you don't need to split it. Just use it in a JTable
constructor.
System.out.println("Before trying to break " +line);
int pos=0;
while((pos = line.indexOf(" "))>-1){
line = line.substring(0,pos)+line.substring(pos+1=
);
}
System.out.println("After breakage\n" +line);
String delims=" +"; //(How can i try this - like RGB says wi=
th
knowing the embedded spaces. From what I see I have one embedded space
in between Valu=
e and 3 like in the
example...
String[] headerLabels=line.split(delims);
for (int i=0;i<headerLabels.length;i++){
System.out.println(headerLabels[i]);
}
Object[][] data={{}};
model=new DefaultTableModel(data,headerLabels);
table=new JTable(model);
Any ideas guys?
1)
String#split()
2)
---------------------------------------------8<--------------------------=
--=AD-----
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
/**
* @author: RedGrittyBrick
*/
public class HeadingSplitter {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new HeadingSplitter().createAndShowGUI=
();
}
});
}
private void createAndShowGUI() {
Integer[][] data = {{1,2,3,4},{5,6,7,8},{9,10,11,12}=
};
String line = "word 1 word 2 word 3 w=
ord 4";
String[] headings = {"","","",""};
int i = 0;
for (String item: line.split(" +")) {
int j = i++ / 2;
headings[j] = headings[j] + " " + item;
}
JTable table = new JTable(data, headings);
JPanel p = new JPanel();
p.add(new JScrollPane(table));
JFrame f = new JFrame("");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}}
---------------------------------------------8<--------------------------=
--=AD-----
But I don't like the value with which you initialize "line".
--
RGB- Hide quoted text -
- Show quoted text -
Well I know it is crazy but somewhere I had to modify an existing
complicated code written 5 years back by some engineers. What they use
is that they have something that is extracted as a StringBuffer using
getSetDescription() method to return the StringBuffer output and this
one is output as "Code Description Value 3 Value 4"
Normally the above code will just work and output Code Description in
the correct respective columns but Value and 3 will go to different
columns and so with Value 4 because there is a space there. I wish if
Java had a way of searching through dynamic column names with varying
spaces and outputting them in the correct respective columns in
JTable.
I know this is hard given there is no idea abt the number of space in
between ...
Anyways RGB thanks for your help. Btw if you come across varying
spaces a method to finding how to split and put the respective labels
in column then do let me know
thanks
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24